Skip to content

Instantly share code, notes, and snippets.

@bayzi
Last active February 27, 2023 13:16
Show Gist options
  • Save bayzi/2a4be6b9ab3a0096965af7ca3a098790 to your computer and use it in GitHub Desktop.
Save bayzi/2a4be6b9ab3a0096965af7ca3a098790 to your computer and use it in GitHub Desktop.
web tracer opentelemetry
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { W3CTraceContextPropagator } from '@opentelemetry/core'
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { Resource } from '@opentelemetry/resources'
import { ZoneContextManager } from '@opentelemetry/context-zone'
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
export const initInstrumentation = () => {
const exporter = new OTLPTraceExporter({
url: "http://localhost:4318/v1/traces",
})
const resource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'frontend',
application: 'frontend',
})
const provider = new WebTracerProvider({ resource })
provider.addSpanProcessor(new BatchSpanProcessor(exporter))
// Initialize the provider
provider.register({
propagator: new W3CTraceContextPropagator(),
contextManager: new ZoneContextManager(),
})
// Registering instrumentations / plugins
registerInstrumentations({
instrumentations: [
new FetchInstrumentation({
propagateTraceHeaderCorsUrls: [/.+/g], // this is too broad for production
clearTimingResources: true,
}),
],
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment