-
Notifications
You must be signed in to change notification settings - Fork 0
add metrics #7
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
Merged
Merged
add metrics #7
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Telemetry configuration, parsed once from the environment. | ||
| * | ||
| * Telemetry is a **hard no-op** unless an OTLP endpoint is configured and the master | ||
| * switch is not turned off. This module depends on nothing but `process.env`, so importing | ||
| * it can never pull the OpenTelemetry SDK into the import graph ahead of the `--import` | ||
| * bootstrap - keeping it side-effect-free is what lets `metrics.ts` be imported from | ||
| * `index.ts` cheaply. | ||
| */ | ||
|
|
||
| export type TelemetryConfig = { | ||
| /** Master switch: an OTLP endpoint is set and TELEMETRY_ENABLED is not 'off'. */ | ||
| enabled: boolean | ||
| /** Why telemetry is off, for a single startup log line. `undefined` when enabled. */ | ||
| disabledReason?: string | ||
| endpoint?: string | ||
| serviceName: string | ||
| serviceVersion: string | ||
| environment: string | ||
| exportIntervalMs: number | ||
| /** `bootstrap` | `relay` - the node's ROLE, surfaced as `ocean.node.role`. */ | ||
| role: string | ||
| /** Optional operator tag (`OCEAN_NETWORK_LABEL`) to group fleets, as `ocean.network`. */ | ||
| networkLabel?: string | ||
| } | ||
|
|
||
| /** | ||
| * Positive **integer** milliseconds, or the fallback. | ||
| * | ||
| * `Number(env.X)` alone is not enough: `X=""` yields `0` and `X=abc` yields `NaN`, and both | ||
| * then reach the OTel export interval as a busy loop or an immediate throw. Integrality is | ||
| * part of the contract - `0.1` is finite and positive but a 0.1 ms interval is a busy loop. | ||
| */ | ||
| export function readPositiveInt(value: string | undefined, fallback: number): number { | ||
| if (value === undefined || value.trim() === '') return fallback | ||
| const parsed = Number(value) | ||
| if (!Number.isInteger(parsed) || parsed <= 0) return fallback | ||
| return parsed | ||
| } | ||
|
|
||
| let cached: TelemetryConfig | undefined | ||
|
|
||
| export function telemetryConfig(env: NodeJS.ProcessEnv = process.env): TelemetryConfig { | ||
| if (cached) return cached | ||
|
|
||
| const endpoint = env.OTEL_EXPORTER_OTLP_ENDPOINT?.trim() || undefined | ||
| const enabled = env.TELEMETRY_ENABLED !== 'off' && !!endpoint | ||
|
|
||
| cached = { | ||
| enabled, | ||
| endpoint, | ||
| serviceName: env.OTEL_SERVICE_NAME?.trim() || 'ocean-node-bootstrap', | ||
| serviceVersion: env.OTEL_SERVICE_VERSION?.trim() || env.npm_package_version || '0.0.0', | ||
| environment: env.DEPLOYMENT_ENVIRONMENT || env.NODE_ENV || 'development', | ||
| exportIntervalMs: readPositiveInt(env.OTEL_METRIC_EXPORT_INTERVAL, 60_000), | ||
| role: env.ROLE?.trim() || 'bootstrap', | ||
| networkLabel: env.OCEAN_NETWORK_LABEL?.trim() || undefined, | ||
| disabledReason: enabled | ||
| ? undefined | ||
| : 'OTEL_EXPORTER_OTLP_ENDPOINT unset or TELEMETRY_ENABLED=off' | ||
| } | ||
| return cached | ||
| } | ||
|
|
||
| /** Test-only: drop the memoized config so a test can re-parse a mutated environment. */ | ||
| export function resetTelemetryConfigForTest(): void { | ||
| cached = undefined | ||
| } | ||
Binary file not shown.
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,29 @@ | ||
| /** | ||
| * Telemetry diagnostics. | ||
| * | ||
| * The bootstrap logs one JSON object per line to the console (see `logEvent` in `index.ts`), | ||
| * so the telemetry bootstrap does the same rather than inventing its own format - the feed | ||
| * stays greppable and machine-readable. `logEvent` itself lives in `index.ts` and is not | ||
| * exported (importing it would pull the whole app graph in ahead of the `--import` SDK | ||
| * bootstrap), so the envelope shape is mirrored here instead. | ||
| * | ||
| * There is no stdout protocol channel to protect in this process, so writing to the console | ||
| * is safe - unlike on-mcp, where stdio is a JSON-RPC channel. | ||
| */ | ||
|
|
||
| export function telemetryLog(message: string, error?: unknown): void { | ||
| const line = JSON.stringify({ | ||
| ts: new Date().toISOString(), | ||
| level: error === undefined ? 'info' : 'error', | ||
| event: 'telemetry', | ||
| message, | ||
| ...(error === undefined | ||
| ? {} | ||
| : { err: error instanceof Error ? error.message : String(error) }) | ||
| }) | ||
| if (error === undefined) { | ||
| console.info(line) | ||
| } else { | ||
| console.error(line) | ||
| } | ||
| } |
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,77 @@ | ||
| /** | ||
| * Every OTel instrument the bootstrap emits, defined once at module load. | ||
| * | ||
| * This module depends on `@opentelemetry/api` **only** - never on the SDK. Without a | ||
| * registered provider the API returns no-op instruments, so `add()` calls and the observable | ||
| * gauges cost approximately nothing when telemetry is unconfigured. That is what lets the | ||
| * instrumentation be imported unconditionally from `index.ts`; the SDK is registered only by | ||
| * the `--import` bootstrap in `otel.ts`. | ||
| * | ||
| * Metric names are the P2P catalog from the metrics plan (§4). Labels are bounded enums only - | ||
| * never a peerId, multiaddr or IP (§9). The observable gauge *instruments* live here; their | ||
| * callbacks are attached from `gauges.ts`, which owns the libp2p handle. | ||
| */ | ||
| import { metrics, type Attributes } from '@opentelemetry/api' | ||
|
|
||
| const meter = metrics.getMeter('ocean-node-bootstrap', process.env.npm_package_version) | ||
|
|
||
| /* ── Counters ─────────────────────────────────────────────────────────────────── */ | ||
|
|
||
| export const p2pPeerConnect = meter.createCounter('ocean.p2p.peer.connect', { | ||
| description: 'libp2p peer:connect events observed', | ||
| unit: '{event}' | ||
| }) | ||
|
|
||
| export const p2pPeerDisconnect = meter.createCounter('ocean.p2p.peer.disconnect', { | ||
| description: 'libp2p peer:disconnect events observed', | ||
| unit: '{event}' | ||
| }) | ||
|
|
||
| export const p2pPeerDiscovery = meter.createCounter('ocean.p2p.peer.discovery', { | ||
| description: 'libp2p peer:discovery events observed', | ||
| unit: '{event}' | ||
| }) | ||
|
|
||
| export const rabbitmqPublished = meter.createCounter( | ||
| 'ocean.bootstrap.rabbitmq.published', | ||
| { | ||
| description: 'Peer-update messages accepted by the RabbitMQ discovery feed', | ||
| unit: '{message}' | ||
| } | ||
| ) | ||
|
|
||
| /* ── Observable gauges (callbacks attached in gauges.ts) ──────────────────────── */ | ||
|
|
||
| export const p2pConnections = meter.createObservableGauge('ocean.p2p.connections', { | ||
| description: 'Live libp2p connections, by direction and circuit-relay-limited flag', | ||
| unit: '{connection}' | ||
| }) | ||
|
|
||
| export const p2pRoutingTablePeers = meter.createObservableGauge( | ||
| 'ocean.p2p.dht.routing_table_peers', | ||
| { | ||
| description: 'Peers in the Kademlia DHT routing table', | ||
| unit: '{peer}' | ||
| } | ||
| ) | ||
|
|
||
| export const p2pDhtMode = meter.createObservableGauge('ocean.p2p.dht.mode', { | ||
| description: 'DHT mode: 1 = server, 0 = client (restores ocean_bootstrap_dht_mode)', | ||
| unit: '{mode}' | ||
| }) | ||
|
|
||
| export const p2pRelayReservations = meter.createObservableGauge( | ||
| 'ocean.p2p.relay_reservations', | ||
| { | ||
| description: 'Circuit-relay reservations (granted when relay server, else held)', | ||
| unit: '{reservation}' | ||
| } | ||
| ) | ||
|
|
||
| export const p2pDialQueue = meter.createObservableGauge('ocean.p2p.dial_queue', { | ||
| description: 'Pending entries in the libp2p dial queue, by status', | ||
| unit: '{dial}' | ||
| }) | ||
|
|
||
| /** Narrow alias so call sites cannot accidentally pass an unbounded value as an attribute. */ | ||
| export type MetricAttributes = Attributes |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: oceanprotocol/ocean-node-bootstrap
Length of output: 14896
🏁 Script executed:
Repository: oceanprotocol/ocean-node-bootstrap
Length of output: 17505
Separate
sendToQueue()flow control from publication accounting.rabbitChannelis a regular channel created withcreateChannel().sendToQueue()returnsfalsewhen its local write buffer is full and emits'drain'later; it does not report publication failure or broker rejection. The!publishedbranch therefore omits buffered messages fromrabbitmqPublished. Count local enqueue outcomes separately from backpressure, or use a confirm channel and count broker confirmations. Update the counter description to match the selected semantics.🤖 Prompt for AI Agents