-
Notifications
You must be signed in to change notification settings - Fork 120
feat(browser): OTel logs pipeline, MapleBrowser.logger and per-session trace sampling #1145
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
Changes from all commits
Commits
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.
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
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,11 @@ | ||
| // Everything that can start a moment after `init()` without losing data lives | ||
| // behind this chunk, so it stays off the eager bundle every page load pays for. | ||
| import type { ResolvedConfig } from "../config" | ||
| import { startLogs } from "./logs" | ||
|
|
||
| export function startDeferred(config: ResolvedConfig): () => Promise<void> { | ||
| const stops = [startLogs(config)] | ||
| return async () => { | ||
| await Promise.all(stops.map((stop) => stop())) | ||
| } | ||
| } |
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,73 @@ | ||
| import { consentAllowedSince, hasConsent, ingestHeaders, sdkHint } from "@maple/browser-session" | ||
| import { ROOT_CONTEXT, trace } from "@opentelemetry/api" | ||
| import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http" | ||
| import { resourceFromAttributes } from "@opentelemetry/resources" | ||
| import { | ||
| BatchLogRecordProcessor, | ||
| LoggerProvider, | ||
| type LogRecordExporter, | ||
| type ReadableLogRecord, | ||
| } from "@opentelemetry/sdk-logs" | ||
| import type { ResolvedConfig } from "../config" | ||
| import { attachLogSink, detachLogSink } from "../logs" | ||
| import { resourceAttributes } from "../tracing" | ||
| import { SDK_NAME, SDK_VERSION } from "../version" | ||
|
|
||
| /** Same rule as spans: drop records made while consent was absent, even if it is granted by flush time. */ | ||
| class ConsentLogExporter implements LogRecordExporter { | ||
| constructor(private readonly inner: LogRecordExporter) {} | ||
|
|
||
| export(logs: ReadableLogRecord[], callback: (result: { code: number; error?: Error }) => void): void { | ||
| const since = consentAllowedSince() | ||
| const eligible = | ||
| hasConsent() && Number.isFinite(since) | ||
| ? logs.filter((log) => log.hrTime[0] * 1_000 + log.hrTime[1] / 1_000_000 >= since) | ||
| : [] | ||
| if (eligible.length === 0) { | ||
| callback({ code: 0 }) | ||
| return | ||
| } | ||
| this.inner.export(eligible, callback) | ||
| } | ||
|
|
||
| forceFlush(): Promise<void> { | ||
| return this.inner.forceFlush?.() ?? Promise.resolve() | ||
| } | ||
|
|
||
| shutdown(): Promise<void> { | ||
| return this.inner.shutdown() | ||
| } | ||
| } | ||
|
|
||
| /** Start the OTel logs pipeline and drain the eager queue into it. Returns a shutdown. */ | ||
| export function startLogs(config: ResolvedConfig): () => Promise<void> { | ||
| const exporter = new ConsentLogExporter( | ||
| new OTLPLogExporter({ | ||
| url: `${config.endpoint}/v1/logs`, | ||
| headers: ingestHeaders({ ingestKey: config.ingestKey, sdk: sdkHint(SDK_NAME, SDK_VERSION) }), | ||
| }), | ||
| ) | ||
| // The browser processor flushes on `visibilitychange → hidden` and `pagehide` itself. | ||
| const provider = new LoggerProvider({ | ||
| resource: resourceFromAttributes(resourceAttributes(config)), | ||
| processors: [new BatchLogRecordProcessor({ exporter, scheduledDelayMillis: 2_000 })], | ||
| }) | ||
| const logger = provider.getLogger(SDK_NAME, SDK_VERSION) | ||
| attachLogSink((record) => | ||
| logger.emit({ | ||
| eventName: record.eventName, | ||
| severityNumber: record.severityNumber, | ||
| severityText: record.severityText, | ||
| body: record.body, | ||
| attributes: record.attributes, | ||
| timestamp: record.timestamp, | ||
| context: record.spanContext | ||
| ? trace.setSpanContext(ROOT_CONTEXT, record.spanContext) | ||
| : ROOT_CONTEXT, | ||
| }), | ||
| ) | ||
| return async () => { | ||
| detachLogSink() | ||
| await provider.shutdown() | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.