Repository navigation
pr #27
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
+133
−3
Merged
pr #27
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type { Span } from "@opentelemetry/api"; | ||
| import posthog from "posthog-js"; | ||
|
|
||
| /** | ||
| * Logs emitted through this module carry the trace and span ids of the span the | ||
| * caller is working in, which is what Correlates a log record with its trace in | ||
| * PostHog. Console autocapture cannot do this: posthog-js wraps `console.*` and | ||
| * sends `{ level, body, attributes }` only, with no trace fields and no access | ||
| * to OpenTelemetry context. | ||
| */ | ||
|
|
||
| export type LogLevel = "info" | "warn" | "error"; | ||
|
|
||
| export type LogAttributeValue = string | number | boolean; | ||
|
|
||
| export interface LogContext { | ||
| span?: Span; | ||
| attributes?: Record<string, LogAttributeValue>; | ||
| } | ||
|
|
||
| export function captureLog( | ||
| level: LogLevel, | ||
| body: string, | ||
| { span, attributes = {} }: LogContext = {}, | ||
| ): void { | ||
| if (!span) { | ||
| posthog.captureLog({ body, level, attributes }); | ||
| return; | ||
| } | ||
|
|
||
| const { traceId, spanId, traceFlags } = span.spanContext(); | ||
|
|
||
| posthog.captureLog({ | ||
| body, | ||
| level, | ||
| attributes, | ||
| trace_id: traceId, | ||
| span_id: spanId, | ||
| trace_flags: traceFlags, | ||
| }); | ||
| } |
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,57 @@ | ||
| import { trace, type Attributes, type Span, type Tracer } from "@opentelemetry/api"; | ||
| import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; | ||
| import { resourceFromAttributes } from "@opentelemetry/resources"; | ||
| import { BasicTracerProvider, BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; | ||
|
|
||
| /** | ||
| * Browser tracing for PostHog. | ||
| * | ||
| * posthog-js does not create spans (see the web SDK docs, "Configuring traces"), | ||
| * so a log record emitted from the browser has no trace to point at unless this | ||
| * app produces one. Spans are exported to the same project as the logs, which | ||
| * lets PostHog resolve `trace_id` on a log record to its trace. | ||
| */ | ||
|
|
||
| const SERVICE_NAME = "lightcms-web"; | ||
|
|
||
| interface BrowserTracingConfig { | ||
| posthogKey: string; | ||
| posthogHost: string; | ||
| } | ||
|
|
||
| let tracer: Tracer | undefined; | ||
|
|
||
| export function initBrowserTracing({ posthogKey, posthogHost }: BrowserTracingConfig): void { | ||
| if (tracer) { | ||
| return; | ||
| } | ||
|
|
||
| const exporter = new OTLPTraceExporter({ | ||
| url: new URL("/i/v1/traces", posthogHost).toString(), | ||
| headers: { Authorization: `Bearer ${posthogKey}` }, | ||
| }); | ||
|
|
||
| const provider = new BasicTracerProvider({ | ||
| resource: resourceFromAttributes({ "service.name": SERVICE_NAME }), | ||
| spanProcessors: [new BatchSpanProcessor(exporter)], | ||
| }); | ||
|
|
||
| trace.setGlobalTracerProvider(provider); | ||
| tracer = provider.getTracer(SERVICE_NAME); | ||
| } | ||
|
|
||
| /** | ||
| * Starts a span, or returns undefined when tracing is not configured. Callers | ||
| * own the returned span and must end it. | ||
| */ | ||
| export function startSpan(name: string, attributes?: Attributes): Span | undefined { | ||
| if (!tracer) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (attributes) { | ||
| return tracer.startSpan(name, { attributes }); | ||
| } | ||
|
|
||
| return tracer.startSpan(name); | ||
| } |
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.
Confirm project-level console capture stays off, because
captureConsoleLogs: falsedoes not block it.PostHog enables console autocapture through either the
init()option or the project's Logs setting. The documentation states that neither switch overrides the other. A project-level enable can therefore capture console output despite thefalseat line 45. Those records carry no trace or span IDs, and console output can contain sensitive values.Code cannot enforce this setting. Verify that the Logs console capture setting is disabled for this project, and recheck it after remote configuration refreshes.