-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ENG-9166 feat(otel): browser tracing plugin — traceparent per event, web vitals, render timing (3/3) #6901
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
FarhanAliRaza
wants to merge
5
commits into
farhan/eng-9166-reflex-otel-3
from
farhan/eng-9166-reflex-otel-4
Open
ENG-9166 feat(otel): browser tracing plugin — traceparent per event, web vitals, render timing (3/3) #6901
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e25ba66
feat(otel): browser tracing plugin with traceparent propagation, web …
FarhanAliRaza c366f11
docs(otel): observability reference page
FarhanAliRaza d3bfbe1
fix(otel): put the profiling alias inside the existing vite resolve b…
FarhanAliRaza 2c0c192
fix(otel): browser sample_rate, flush unload disconnect span, anchor …
FarhanAliRaza 4e1d604
fix(otel): reject out-of-range sample_rate at plugin construction
FarhanAliRaza 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Observability (OpenTelemetry) | ||
|
|
||
| Reflex has built-in [OpenTelemetry](https://opentelemetry.io) trace points and | ||
| metrics. They are inert (one boolean check) until you install the optional | ||
| `reflex-otel` package and turn them on. Any OpenTelemetry backend works: | ||
| Jaeger, Grafana Tempo, SigNoz, Honeycomb, Datadog, ... | ||
|
|
||
| ## Backend: install and enable | ||
|
|
||
| ```bash | ||
| pip install reflex-otel opentelemetry-sdk opentelemetry-exporter-otlp-proto-http | ||
| ``` | ||
|
|
||
| Configure the SDK as usual and enable the Reflex instrumentation once, at | ||
| import time of your app module: | ||
|
|
||
| ```python | ||
| from opentelemetry import trace | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
| from reflex_otel import ReflexInstrumentor | ||
|
|
||
| if not ReflexInstrumentor().is_instrumented_by_opentelemetry: | ||
| provider = TracerProvider() | ||
| provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) | ||
| trace.set_tracer_provider(provider) | ||
| ReflexInstrumentor().instrument(tracer_provider=provider) | ||
| ``` | ||
|
|
||
| Guard the setup as shown: Reflex hot reload re-imports the app module, and | ||
| `set_tracer_provider()` / `instrument()` warn when called twice. | ||
|
|
||
| `ReflexInstrumentor` also registers the standard `opentelemetry_instrumentor` | ||
| entry point, so `opentelemetry-instrument reflex run` enables it with no code. | ||
|
|
||
| ### What is traced | ||
|
|
||
| - One span per event handler run, named after the event, with | ||
| `reflex.event.name`, `reflex.event.txid`, `reflex.event.background`, | ||
| `session.id` and `code.function.name`. Exceptions are recorded on the span. | ||
| Events sent by the browser are `SERVER` spans: a new trace, or a child of the | ||
| browser span when the frontend plugin (below) is active. | ||
| - Events returned by a handler (chained events) are `INTERNAL` children of the | ||
| span that produced them. | ||
| - HTTP requests and the websocket connection get spans and `http.server.*` | ||
| metrics from the OpenTelemetry ASGI middleware. | ||
| - Each app compile is a `reflex.compile` span with the stages | ||
| (`reflex.compile.pages`, `reflex.compile.write`, ...) as children. | ||
|
|
||
| ### Metrics | ||
|
|
||
| | Instrument | Type | Unit | Attributes | | ||
| | --- | --- | --- | --- | | ||
| | `reflex.event.duration` | histogram | s | `reflex.event.name`, `reflex.event.background`, `error.type` | | ||
| | `reflex.state.acquire.duration` | histogram | s | `reflex.event.name` | | ||
| | `reflex.websocket.message.size` | histogram | By | `network.io.direction` | | ||
| | `reflex.websocket.connections` | up-down counter | `{connection}` | | | ||
|
|
||
| Pass `meter_provider=` to `instrument()` to export them (defaults to the global | ||
| meter provider). | ||
|
|
||
| ## Frontend: browser traces | ||
|
|
||
| Add the plugin to your config to trace the browser as well: | ||
|
|
||
| ```python | ||
| # rxconfig.py | ||
| import reflex as rx | ||
| from reflex_otel import OtelPlugin | ||
|
|
||
| config = rx.Config( | ||
| app_name="my_app", | ||
| plugins=[OtelPlugin(endpoint="https://collector.example.com/v1/traces")], | ||
| ) | ||
| ``` | ||
|
|
||
| The compiled frontend then | ||
|
|
||
| - sends a W3C `traceparent` with every event, so each user interaction is one | ||
| trace: browser span → backend event span → chained events; | ||
| - reports web vitals (`web_vital.LCP`, `CLS`, `INP`, `FCP`, `TTFB`) as spans; | ||
| - with `render_timing=True`, reports React commits as `react.render` spans | ||
| (one per commit; uses the `react-dom/profiling` build); | ||
| - records `socket.connect` / `socket.disconnect` spans. | ||
|
|
||
| `endpoint` defaults to `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`, then | ||
| `OTEL_EXPORTER_OTLP_ENDPOINT` + `/v1/traces`, then | ||
| `http://localhost:4318/v1/traces`; it must accept OTLP/HTTP from the browser | ||
| (CORS). `service_name` defaults to `<app_name>-frontend`. `headers` are | ||
| compiled into the public bundle, so never put secrets in them. `sample_rate` | ||
| (default `1.0`) samples browser traces at the root; a parent-based backend | ||
| sampler follows that decision, so it also bounds the backend event traces. |
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| Add inert OpenTelemetry trace points and metrics around event handler execution, state acquisition, socket messages and compile stages (`reflex_base.otel`); they cost one boolean check until the `reflex-otel` package enables them. | ||
| Add inert OpenTelemetry trace points and metrics around event handler execution, state acquisition, socket messages and compile stages (`reflex_base.otel`) and a `window.__reflex_otel` hook in the frontend event loop; they cost one boolean check until the `reflex-otel` package enables them. |
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| Add the `reflex-otel` package: an OpenTelemetry instrumentor that turns on the framework's built-in trace points and metrics (one span per event handler run, chained events parented under the enqueuing span, frontend `traceparent` propagation, event/state/websocket metrics, compile spans) and wraps the ASGI app in the OpenTelemetry ASGI middleware. | ||
| Add the `reflex-otel` package: an OpenTelemetry instrumentor that turns on the framework's built-in trace points and metrics (one span per event handler run, chained events parented under the enqueuing span, frontend `traceparent` propagation, event/state/websocket metrics, compile spans) and wraps the ASGI app in the OpenTelemetry ASGI middleware. `OtelPlugin` adds browser tracing (traceparent per event, web vitals, React render timing) to the compiled frontend. |
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,151 @@ | ||
| /** | ||
| * Browser-side OpenTelemetry for Reflex apps, installed by reflex_otel.OtelPlugin. | ||
| * | ||
| * - Every event sent to the backend gets a CLIENT span and a W3C `traceparent`, | ||
| * so the backend event span joins the browser trace. | ||
| * - Web vitals (LCP, CLS, INP, FCP, TTFB) are reported as spans. | ||
| * - React commits are reported as `react.render` spans via a root <Profiler> | ||
| * (opt-in; production builds need the react-dom profiling alias the plugin adds). | ||
| * - Socket connects/disconnects are recorded as spans for reconnect tracking. | ||
| * | ||
| * Configuration comes from `env.json` (`OTEL` key), written by the plugin. | ||
| */ | ||
| import { createElement, Profiler } from "react"; | ||
| import { context, SpanKind, SpanStatusCode, trace } from "@opentelemetry/api"; | ||
| import { W3CTraceContextPropagator } from "@opentelemetry/core"; | ||
| import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; | ||
| import { resourceFromAttributes } from "@opentelemetry/resources"; | ||
| import { | ||
| BatchSpanProcessor, | ||
| ParentBasedSampler, | ||
| TraceIdRatioBasedSampler, | ||
| WebTracerProvider, | ||
| } from "@opentelemetry/sdk-trace-web"; | ||
| import { onCLS, onFCP, onINP, onLCP, onTTFB } from "web-vitals"; | ||
| import env from "$/env.json"; | ||
|
|
||
| const config = env.OTEL ?? {}; | ||
|
|
||
| const provider = new WebTracerProvider({ | ||
|
FarhanAliRaza marked this conversation as resolved.
|
||
| resource: resourceFromAttributes({ "service.name": config.service_name }), | ||
| // Browser spans are trace roots: their sampled flag travels in `traceparent` | ||
| // and a parent-based backend sampler follows it, so sample here. | ||
| sampler: new ParentBasedSampler({ | ||
| root: new TraceIdRatioBasedSampler(config.sample_rate ?? 1), | ||
| }), | ||
| spanProcessors: [ | ||
| new BatchSpanProcessor( | ||
| new OTLPTraceExporter({ url: config.endpoint, headers: config.headers }), | ||
| ), | ||
| ], | ||
| }); | ||
| const tracer = provider.getTracer("reflex", config.version); | ||
| const propagator = new W3CTraceContextPropagator(); | ||
|
|
||
| const setter = { | ||
| set(carrier, key, value) { | ||
| carrier[key] = value; | ||
| }, | ||
| }; | ||
|
|
||
| // Absolute epoch time (ms) of a performance timeline offset. | ||
| const epoch = (offset) => performance.timeOrigin + offset; | ||
|
|
||
| let connectCount = 0; | ||
|
|
||
| window.__reflex_otel = { | ||
| onEventSend(event) { | ||
| // Fire-and-forget over the socket: a PRODUCER span that marks the send. The | ||
| // browser has no completion signal for an event, so it has no duration. | ||
| const span = tracer.startSpan(event.name, { | ||
| kind: SpanKind.PRODUCER, | ||
| attributes: { "reflex.event.name": event.name }, | ||
| }); | ||
| propagator.inject(trace.setSpan(context.active(), span), event, setter); | ||
| span.end(); | ||
| }, | ||
| onSocketConnect() { | ||
| connectCount += 1; | ||
| tracer | ||
| .startSpan("socket.connect", { | ||
| attributes: { "reflex.socket.connect_count": connectCount }, | ||
| }) | ||
| .end(); | ||
| }, | ||
| onSocketDisconnect(reason) { | ||
| const span = tracer.startSpan("socket.disconnect", { | ||
| attributes: { "reflex.socket.disconnect_reason": reason }, | ||
| }); | ||
| // Intentional disconnects (navigation, server shutdown) are not errors. | ||
| if (!reason.startsWith("io ")) { | ||
| span.setStatus({ code: SpanStatusCode.ERROR, message: reason }); | ||
| } | ||
| span.end(); | ||
|
FarhanAliRaza marked this conversation as resolved.
|
||
| // Unload disconnects happen after the processor's own pagehide flush ran, | ||
| // so the span would otherwise sit in the queue while the page tears down. | ||
| provider.forceFlush(); | ||
| }, | ||
| }; | ||
|
|
||
| if (config.web_vitals) { | ||
| // FCP/LCP/TTFB values are offsets from the current navigation: activation | ||
| // start for a (pre)rendered page, `navigationStartTime` for a BFCache | ||
| // restore or soft navigation. INP is a duration starting at its interaction; | ||
| // CLS is unitless and reported as an instant. | ||
| const report = (metric) => { | ||
| const activationStart = | ||
| performance.getEntriesByType("navigation")[0]?.activationStart ?? 0; | ||
| const attributes = { | ||
| "web_vital.name": metric.name, | ||
| "web_vital.value": metric.value, | ||
| "web_vital.rating": metric.rating, | ||
| "web_vital.id": metric.id, | ||
| "web_vital.navigation_type": metric.navigationType, | ||
| }; | ||
| let startTime = epoch(metric.navigationStartTime || activationStart); | ||
| let endTime = startTime + metric.value; | ||
| if (metric.name === "INP") { | ||
| startTime = epoch(metric.entries[0]?.startTime ?? 0); | ||
| endTime = startTime + metric.value; | ||
| } else if (metric.name === "CLS") { | ||
| startTime = endTime = Date.now(); | ||
| } | ||
| tracer | ||
| .startSpan(`web_vital.${metric.name}`, { startTime, attributes }) | ||
| .end(endTime); | ||
| }; | ||
| onCLS(report); | ||
| onFCP(report); | ||
| onINP(report); | ||
| onLCP(report); | ||
| onTTFB(report); | ||
| } | ||
|
|
||
| const onRender = ( | ||
| id, | ||
| phase, | ||
| actualDuration, | ||
| baseDuration, | ||
| startTime, | ||
| commitTime, | ||
| ) => { | ||
| tracer | ||
| .startSpan("react.render", { | ||
| startTime: epoch(startTime), | ||
| attributes: { | ||
| "react.profiler.id": id, | ||
| "react.render.phase": phase, | ||
| "react.render.actual_duration_ms": actualDuration, | ||
| "react.render.base_duration_ms": baseDuration, | ||
| }, | ||
| }) | ||
| .end(epoch(commitTime)); | ||
| }; | ||
|
|
||
| /** | ||
| * Root wrapper used by the patched entry: profiles React commits when enabled. | ||
| */ | ||
| export const OtelRoot = ({ children }) => | ||
| config.render_timing | ||
| ? createElement(Profiler, { id: "app", onRender }, children) | ||
| : children; | ||
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.