DocumentaciónHelp

Instrument the browser

Add OpenTelemetry to your website or Angular application so the trace starts at the user's click and continues to your API and your database.

On this page

With the browser instrumented, each trace starts at the page load or the user's click and continues into your API and the database. That way you see how much of the total time goes to the network, the back end or the query. The guides are in Settings → APM → API key and SDK, section SDK by language and other ways to send, Browser tab.

Before you start

  • A browser token with the service name and your site's origins. See Create an ingest token.

  • Access to the site's code and your API's configuration.

In a web page, use only a browser token. It's public by design: it only sends traces, only from the origins you specified, and always with the fixed service name. Never put a server token there.

Steps for any website

  1. Install the packages:

npm install @opentelemetry/sdk-trace-web @opentelemetry/exporter-trace-otlp-http @opentelemetry/instrumentation \
  @opentelemetry/instrumentation-document-load @opentelemetry/instrumentation-fetch \
  @opentelemetry/instrumentation-xml-http-request @opentelemetry/resources
  1. At application startup, before mounting the interface, register the trace provider. The exporter goes to the ingest URL ending in /v1/traces, with your browser token:

const provider = new WebTracerProvider({
  resource: resourceFromAttributes({ 'service.name': 'mi-web', 'deployment.environment.name': 'produccion' }),
  spanProcessors: [new BatchSpanProcessor(new OTLPTraceExporter({
    url: 'https://URL_DE_INGESTA/v1/traces',
    headers: { Authorization: 'Bearer jtk_TU_TOKEN_DE_NAVEGADOR' },
  }))],
});
provider.register();
// Only YOUR API gets the trace context (traceparent).
const api = [/^https:\/\/api\.midominio\.cl/];
registerInstrumentations({
  instrumentations: [
    new DocumentLoadInstrumentation(),
    new FetchInstrumentation({ propagateTraceHeaderCorsUrls: api }),
    new XMLHttpRequestInstrumentation({ propagateTraceHeaderCorsUrls: api }),
  ],
});

The on-screen guide includes the full block, with the import statements.

  1. In your API, accept the context headers in CORS:

Access-Control-Allow-Headers: traceparent, tracestate, content-type, authorization
  1. If your site has a Content-Security-Policy, allow the ingest URL:

connect-src 'self' https://URL_DE_INGESTA

Change the api expression to your API's domain. Only those addresses get the trace context; calls to third parties don't receive it.

Angular

It's the same as above, in a src/app/telemetry.ts file that you import in main.ts before bootstrapApplication. HttpClient uses XHR or fetch (with withFetch()) and both are instrumented.

  • If your application uses zone.js, also install @opentelemetry/context-zone and register with provider.register({ contextManager: new ZoneContextManager() }).

  • If it's zoneless (no zone.js), provider.register() is enough.

What happens next

The website appears in APM → Services with the Browser type and counts toward the Browsers indicator. When you open a trace that started in the browser, you see its spans and, below them, those of your API and the database in the same waterfall.

Related

Search Argunis Monitoring

Type at least 2 characters to search Argunis Monitoring.

↑↓ to navigate · Enter to open · Esc to close