-
Notifications
You must be signed in to change notification settings - Fork 139
Minimal observability metrics (relay & client) #853
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
fcancela
wants to merge
13
commits into
moq-dev:main
Choose a base branch
from
qualabs:feat/observability
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
523fcc2
moq-lite: add optional Stats hooks; moq-native accept_with_stats
fcancela facf788
relay: basic session + app byte counters exposed at /metrics
fcancela 436263d
hang: metrics-only OTLP (connections + startup time)
fcancela 76302df
hang: drop @opentelemetry/resources dependency
fcancela 0082ade
chore: update bun.lock with new OpenTelemetry dependencies and protob…
fcancela 3ec2d84
Update js/hang/src/observability/index.ts
fcancela 0f617b0
observability: optional prom+grafana+otel compose for demo metrics
fcancela 5acf872
just: add observability helper recipes
fcancela cca8aaf
just: make observability run stack + dev
fcancela c20b708
chore: update dependencies for OpenTelemetry and clean up observabili…
fcancela 0f68bb2
fix: remove unnecessary newline in observability index and reorder im…
fcancela 2f7a19f
refactor: clean up session and stats imports, format function calls f…
fcancela 41786ff
Merge branch 'main' of https://github.com/qualabs/moq into feat/obser…
fcancela 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * Minimal OpenTelemetry metrics for MoQ client (browser). | ||
| * | ||
| * Kept intentionally small for reviewability: | ||
| * - `moq_client_connections_total{transport=...}` | ||
| * - `moq_client_startup_time_seconds{track_type=...}` | ||
| */ | ||
|
|
||
| import type { Counter, Histogram, Meter } from "@opentelemetry/api"; | ||
| import { metrics } from "@opentelemetry/api"; | ||
| import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http"; | ||
| import { MeterProvider, PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; | ||
|
|
||
| let initialized = false; | ||
| let sessionId: string | undefined; | ||
|
|
||
| export interface ObservabilityConfig { | ||
| /** OTLP endpoint URL (default: http://localhost:4318) */ | ||
| otlpEndpoint?: string; | ||
| /** Service name (default: moq-client) */ | ||
| serviceName?: string; | ||
| /** Enable observability (default: true if endpoint provided) */ | ||
| enabled?: boolean; | ||
| /** Per-player session id (defaults to a random UUID) */ | ||
| sessionId?: string; | ||
| } | ||
|
|
||
| function createSessionId(): string { | ||
| try { | ||
| return globalThis.crypto?.randomUUID?.() ?? `sid-${Math.random().toString(16).slice(2)}-${Date.now()}`; | ||
| } catch { | ||
| return `sid-${Math.random().toString(16).slice(2)}-${Date.now()}`; | ||
| } | ||
| } | ||
|
|
||
| export function initObservability(config: ObservabilityConfig = {}): void { | ||
| if (initialized) return; | ||
|
|
||
| const endpoint = config.otlpEndpoint || "http://localhost:4318"; | ||
| const serviceName = config.serviceName || "moq-client"; | ||
| const enabled = config.enabled ?? !!config.otlpEndpoint; | ||
| sessionId = config.sessionId || createSessionId(); | ||
|
|
||
| if (!enabled) return; | ||
|
|
||
| const exporterHeaders = { "Content-Type": "application/json" }; | ||
| const metricExporter = new OTLPMetricExporter({ | ||
| url: `${endpoint}/v1/metrics`, | ||
| headers: exporterHeaders, | ||
| }); | ||
|
|
||
| const reader = new PeriodicExportingMetricReader({ | ||
| exporter: metricExporter, | ||
| exportIntervalMillis: 10000, | ||
| }); | ||
|
|
||
| const meterProvider = new MeterProvider({ | ||
| readers: [reader], | ||
| }); | ||
|
|
||
| metrics.setGlobalMeterProvider(meterProvider); | ||
| const meter = metrics.getMeter(serviceName); | ||
|
|
||
| clientMetricsInstance = new ClientMetrics(meter); | ||
| setupConnectionTracking(); | ||
| initialized = true; | ||
| } | ||
|
|
||
| function setupConnectionTracking() { | ||
| // Dynamically import to avoid circular deps. | ||
| import("@moq/lite") | ||
| .then((Moq) => { | ||
| if (Moq.Connection?.onConnectionType) { | ||
| Moq.Connection.onConnectionType((type: "webtransport" | "websocket") => { | ||
| getClientMetrics()?.recordConnection(type); | ||
| }); | ||
| } | ||
| }) | ||
| .catch((error) => console.warn("Failed to set up connection tracking for observability:", error)); | ||
| } | ||
|
|
||
| export class ClientMetrics { | ||
| private connectionCounter?: Counter; | ||
| private startupTimeHistogram?: Histogram; | ||
|
|
||
| constructor(meter?: Meter) { | ||
| if (meter) { | ||
| this.connectionCounter = meter.createCounter("moq_client_connections_total", { | ||
| description: "Total client connections by transport type", | ||
| }); | ||
|
|
||
| this.startupTimeHistogram = meter.createHistogram("moq_client_startup_time_seconds", { | ||
| description: "Time to first audio/video frame in seconds", | ||
| unit: "s", | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| recordConnection(transportType: "webtransport" | "websocket"): void { | ||
| const attrs: Record<string, string> = { transport: transportType }; | ||
| if (sessionId) attrs["moq.player.session_id"] = sessionId; | ||
| this.connectionCounter?.add(1, attrs); | ||
| } | ||
|
|
||
| recordStartupTime(seconds: number, attributes?: Record<string, string>): void { | ||
| const attrs: Record<string, string> = { ...(attributes ?? {}) }; | ||
| if (sessionId) attrs["moq.player.session_id"] = sessionId; | ||
| this.startupTimeHistogram?.record(seconds, attrs); | ||
| } | ||
| } | ||
|
|
||
| let clientMetricsInstance: ClientMetrics | undefined; | ||
|
|
||
| export function getClientMetrics(): ClientMetrics | undefined { | ||
| if (!clientMetricsInstance) clientMetricsInstance = new ClientMetrics(); | ||
| return clientMetricsInstance; | ||
| } | ||
|
|
||
| export function recordMetric(fn: (metrics: ClientMetrics) => void): void { | ||
| const m = getClientMetrics(); | ||
| if (m) fn(m); | ||
| } | ||
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.
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.
?