Collecting traces from the browser will allow you to track user interaction. Traces will include the frontend operation that lead to backend trace.
Instrument your web app using the OpenTelemetry JS SDK and send traces to Aspecto.
Add this code to your application in a file named opentelemetry.ts
This code contains parts you need to fill in yourself in step 4. ignoreUrls should include all your third parties domains to avoid CORS errors.
import { WebTracerProvider } from'@opentelemetry/sdk-trace-web';import { BatchSpanProcessor } from'@opentelemetry/sdk-trace-base';import { ZoneContextManager } from'@opentelemetry/context-zone';import { Resource, detectResources, browserDetector } from'@opentelemetry/resources';import { SemanticResourceAttributes } from'@opentelemetry/semantic-conventions';import { OTLPTraceExporter } from'@opentelemetry/exporter-trace-otlp-http';import { registerInstrumentations } from'@opentelemetry/instrumentation';import { getWebAutoInstrumentations } from'@opentelemetry/auto-instrumentations-web';constaspectoToken='--YOUR ASPECTO TOKEN--';constserviceName='--YOUR SERVICE NAME--';// in order to propagate the context to your instrumented services, add their urls by matching a regexconstpropagateTraceHeaderCorsUrls= [/my\.company\.domain/];// urls that match any regex in ignoreUrls will not be traced and the context will not be propagate.// it can be used to exclude uninteresting spans from the traces, avoid CORS issues, and ignore third party API callsconstignoreUrls= [/third\.parties.\urls/];exportconstregisterOpenTelemetry=async () => {constautoResource=awaitdetectResources({ detectors: [browserDetector], });constresource=newResource({ [SemanticResourceAttributes.SERVICE_NAME]: serviceName, }).merge(autoResource);constprovider=newWebTracerProvider({ resource, });constaspectoExporter=newOTLPTraceExporter({ url:'https://otelcol.aspecto.io/v1/traces', headers: { Authorization: aspectoToken, }, });provider.addSpanProcessor(newBatchSpanProcessor(aspectoExporter));provider.register({ contextManager:newZoneContextManager(), });constinstrumentations=getWebAutoInstrumentations({// load custom configuration for xml-http-request instrumentation'@opentelemetry/instrumentation-xml-http-request': { propagateTraceHeaderCorsUrls, ignoreUrls, }, });registerInstrumentations({ instrumentations, });};
Step 4 - Configure the Installation
Fill in the following in the code above in order to export traces to Aspecto:
2) serviceName - The name of your web application. You will be able to filter and search for it in Aspecto trace viewer and use it for sampling rules and alerts.
3) propagateTraceHeaderCorsUrls - In order to propagate the context to your instrumented services, add their urls by matching a regex.
This option is where you control which API calls in your frontend (browser) connect with traces in your backend (nodejs \ java \ python \ ruby \ .NET \ GO).
4) ignoreUrls - Urls that match any regex in ignoreUrls will not be traced and the context will not be propagated. It can be used to exclude uninteresting spans from the traces, avoid CORS issues, and ignore third-party API calls.
Step 5 - Register OpenTelemetry in Your Code
Invoke the registerOpenTelemetry function in your main index.ts file as early as possible.
An example React startup might look similar to this: