-
Notifications
You must be signed in to change notification settings - Fork 536
feat: add opened/closed counters to protocol stream metrics #3407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nkovaturient
wants to merge
13
commits into
libp2p:main
Choose a base branch
from
Nkovaturient:feat/track-protocol-stream-open-close-counters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
286f00b
feat: add protocol stream opened/closed counter metrics
Nkovaturient 6a2517e
chore: lint and polish for protocol stream metrics
Nkovaturient 7199895
Merge branch 'libp2p:main' into feat/track-protocol-stream-open-close…
Nkovaturient ca003f0
Resolve Merge conflicts
Nkovaturient 4bd36ef
Merge branch 'libp2p:main' into feat/track-protocol-stream-open-close…
Nkovaturient 786b1dc
chore(simple-metrics): add sinon-ts devDependency for dep-check
Nkovaturient 8f909e3
feat(metrics): protocol stream counters in OTEL; split clean vs error…
Nkovaturient 2d34678
Merge branch 'main' into feat/track-protocol-stream-open-close-counters
Nkovaturient f5c70ec
chore(metrics): cover protocol stream counters (simple, prometheus, O…
Nkovaturient b4bf5b4
Merge branch 'libp2p:main' into feat/track-protocol-stream-open-close…
Nkovaturient aab6ebd
Resolve merge conflicts
Nkovaturient 72c9c32
Merge branch 'main' into feat/track-protocol-stream-open-close-counters
Nkovaturient fa7d369
Merge branch 'main' into feat/track-protocol-stream-open-close-counters
Nkovaturient File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/metrics-opentelemetry/test/track-protocol-stream.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { StreamAbortEvent, TypedEventEmitter, StreamCloseEvent } from '@libp2p/interface' | ||
| import { defaultLogger } from '@libp2p/logger' | ||
| import { metrics as otelApi } from '@opentelemetry/api' | ||
| import { | ||
| AggregationTemporality, | ||
| DataPointType, | ||
| InMemoryMetricExporter, | ||
| MeterProvider, | ||
| PeriodicExportingMetricReader | ||
| } from '@opentelemetry/sdk-metrics' | ||
| import { expect } from 'aegir/chai' | ||
| import { openTelemetryMetrics } from '../src/index.js' | ||
| import type { MessageStreamEvents, Stream } from '@libp2p/interface' | ||
| import type { ResourceMetrics, ScopeMetrics } from '@opentelemetry/sdk-metrics' | ||
|
|
||
| function sumScopeMetrics (sm: ScopeMetrics, metricName: string, protocolValue: string): number { | ||
| let sub = 0 | ||
| for (const metric of sm.metrics) { | ||
| if (metric.descriptor.name !== metricName || metric.dataPointType !== DataPointType.SUM) { | ||
| continue | ||
| } | ||
| for (const dp of metric.dataPoints) { | ||
| if (dp.attributes.protocol === protocolValue) { | ||
| sub += dp.value as number | ||
| } | ||
| } | ||
| } | ||
| return sub | ||
| } | ||
|
|
||
| function sumForProtocol (batches: ResourceMetrics[], metricName: string, protocolValue: string): number { | ||
| let total = 0 | ||
| for (const batch of batches) { | ||
| for (const sm of batch.scopeMetrics) { | ||
| total += sumScopeMetrics(sm, metricName, protocolValue) | ||
| } | ||
| } | ||
| return total | ||
| } | ||
|
|
||
| describe('opentelemetry protocol stream counters', () => { | ||
| let previousProvider: ReturnType<typeof otelApi.getMeterProvider> | ||
| let reader: PeriodicExportingMetricReader | ||
| let exporter: InMemoryMetricExporter | ||
| let provider: MeterProvider | ||
| let meterId: number | ||
|
|
||
| before(() => { | ||
| previousProvider = otelApi.getMeterProvider() | ||
| exporter = new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE) | ||
| reader = new PeriodicExportingMetricReader({ | ||
| exporter, | ||
| exportIntervalMillis: 3_600_000 | ||
| }) | ||
| provider = new MeterProvider({ readers: [reader] }) | ||
| otelApi.setGlobalMeterProvider(provider) | ||
| }) | ||
|
|
||
| after(async () => { | ||
| await provider.shutdown() | ||
| otelApi.setGlobalMeterProvider(previousProvider) | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| meterId = Date.now() + Math.floor(Math.random() * 1e6) | ||
| exporter.reset() | ||
| }) | ||
|
|
||
| function makeStream (direction: 'inbound' | 'outbound', protocol: string): Stream { | ||
| const target = new TypedEventEmitter<MessageStreamEvents>() | ||
| return { | ||
| direction, | ||
| protocol, | ||
| log: defaultLogger().forComponent('stream'), | ||
| addEventListener: target.addEventListener.bind(target), | ||
| removeEventListener: target.removeEventListener.bind(target), | ||
| dispatchEvent: target.dispatchEvent.bind(target), | ||
| send: () => true | ||
| } as unknown as Stream | ||
| } | ||
|
|
||
| it('increments opened and clean closed counters', async () => { | ||
| const metrics = openTelemetryMetrics()({ | ||
| nodeInfo: { | ||
| name: `otel-ps-test-${meterId}`, | ||
| version: '1.0.0', | ||
| userAgent: 'test/1.0.0' | ||
| }, | ||
| logger: defaultLogger() | ||
| }) | ||
|
|
||
| const stream = makeStream('outbound', '/identify/1.0.0') | ||
| const label = `${stream.direction} ${stream.protocol}` | ||
|
|
||
| metrics.trackProtocolStream(stream) | ||
| stream.dispatchEvent(new StreamCloseEvent()) | ||
|
|
||
| await reader.forceFlush() | ||
| const batches = exporter.getMetrics() | ||
|
|
||
| expect(sumForProtocol(batches, 'libp2p_protocol_streams_opened_total', label)).to.equal(1) | ||
| expect(sumForProtocol(batches, 'libp2p_protocol_streams_closed_total', label)).to.equal(1) | ||
| expect(sumForProtocol(batches, 'libp2p_protocol_streams_close_errors_total', label)).to.equal(0) | ||
| }) | ||
|
|
||
| it('increments close-errors counter on StreamAbortEvent', async () => { | ||
| const metrics = openTelemetryMetrics()({ | ||
| nodeInfo: { | ||
| name: `otel-ps-test-${meterId}`, | ||
| version: '1.0.0', | ||
| userAgent: 'test/1.0.0' | ||
| }, | ||
| logger: defaultLogger() | ||
| }) | ||
|
|
||
| const stream = makeStream('inbound', '/ping/1.0.0') | ||
| const label = `${stream.direction} ${stream.protocol}` | ||
|
|
||
| metrics.trackProtocolStream(stream) | ||
| stream.dispatchEvent(new StreamAbortEvent(new Error('aborted'))) | ||
|
|
||
| await reader.forceFlush() | ||
| const batches = exporter.getMetrics() | ||
|
|
||
| expect(sumForProtocol(batches, 'libp2p_protocol_streams_opened_total', label)).to.equal(1) | ||
| expect(sumForProtocol(batches, 'libp2p_protocol_streams_closed_total', label)).to.equal(0) | ||
| expect(sumForProtocol(batches, 'libp2p_protocol_streams_close_errors_total', label)).to.equal(1) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.