Skip to content

Migrate to opentelemetry-js v2.0.0, update using @opentelemetry/sdk-trace-node example code #6942

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,20 @@ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { PrismaInstrumentation, registerInstrumentations } from '@prisma/instrumentation'
import { Resource } from '@opentelemetry/resources'
import { resourceFromAttributes } from '@opentelemetry/resources'

// Configure the trace provider
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'example application', // Replace with your service name
[SEMRESATTRS_SERVICE_VERSION]: '0.0.1', // Replace with your service version
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: 'example application', // Replace with your service name
[ATTR_SERVICE_VERSION]: '0.0.1', // Replace with your service version
}),
})

// Configure how spans are processed and exported. In this case, we're sending spans
// as we receive them to an OTLP-compatible collector (e.g., Jaeger).
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()))
spanProcessors: [
// Configure how spans are processed and exported. In this case, we're sending spans
// as we receive them to an OTLP-compatible collector (e.g., Jaeger).
new SimpleSpanProcessor(new OTLPTraceExporter()),
],
});

// Register your auto-instrumentors
registerInstrumentations({
Expand Down Expand Up @@ -240,46 +241,47 @@ The following example sends output tracing to the console with `ConsoleSpanExpor

```ts
// Imports
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import * as api from '@opentelemetry/api';
import {
BasicTracerProvider,
ConsoleSpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base'
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'
import * as api from '@opentelemetry/api'
import { PrismaInstrumentation, registerInstrumentations } from '@prisma/instrumentation'
import { Resource } from '@opentelemetry/resources'
PrismaInstrumentation,
registerInstrumentations,
} from '@prisma/instrumentation';
import { resourceFromAttributes } from '@opentelemetry/resources';

// Export the tracing
export function otelSetup() {
const contextManager = new AsyncHooksContextManager().enable()
const contextManager = new AsyncHooksContextManager().enable();

api.context.setGlobalContextManager(contextManager)
api.context.setGlobalContextManager(contextManager);

//Configure the console exporter
const consoleExporter = new ConsoleSpanExporter()
const consoleExporter = new ConsoleSpanExporter();

// Configure the trace provider
const provider = new BasicTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'test-tracing-service',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
const provider = new NodeTracerProvider({
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: 'example application', // Replace with your service name
[ATTR_SERVICE_VERSION]: '0.0.1', // Replace with your service version
}),
})

// Configure how spans are processed and exported. In this case we're sending spans
// as we receive them to the console
provider.addSpanProcessor(new SimpleSpanProcessor(consoleExporter))
spanProcessors: [
// Configure how spans are processed and exported. In this case, we're sending spans
// as we receive them to the console
new SimpleSpanProcessor(consoleExporter),
],
});

// Register your auto-instrumentors
registerInstrumentations({
tracerProvider: provider,
instrumentations: [new PrismaInstrumentation()],
})
});

// Register the provider
provider.register()
provider.register();
}
```

Expand Down Expand Up @@ -402,8 +404,8 @@ You can adjust how your application's traces are grouped by changing the resourc
```js
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'weblog',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
[ATTR_SERVICE_NAME]: 'weblog',
[ATTR_SERVICE_VERSION]: '1.0.0',
}),
})
```
Expand All @@ -427,13 +429,20 @@ You can configure your tracing configuration to use the appropriate span process
import {
SimpleSpanProcessor,
BatchSpanProcessor,
} from '@opentelemetry/sdk-trace-base'
} from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';

const spanProcessors = [];
if (process.env.NODE_ENV === 'production') {
provider.addSpanProcessor(new BatchSpanProcessor(otlpTraceExporter))
spanProcessors.push(new BatchSpanProcessor(otlpTraceExporter));
} else {
provider.addSpanProcessor(new SimpleSpanProcessor(otlpTraceExporter))
spanProcessors.push(new SimpleSpanProcessor(otlpTraceExporter));
}

const provider = new NodeTracerProvider({
spanProcessors,
// ...other configurations
});
```

#### Send fewer spans to the collector with sampling
Expand All @@ -443,21 +452,21 @@ Another way to reduce the performance impact is to [use probability sampling](ht
An example implementation looks like this:

```ts highlight=3,7;add
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
//add-next-line
import { TraceIdRatioBasedSampler } from '@opentelemetry/core'
import { Resource } from '@opentelemetry/resources'
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { TraceIdRatioBasedSampler } from '@opentelemetry/core';
import { resourceFromAttributes } from '@opentelemetry/resources';

const provider = new NodeTracerProvider({
//add-next-line
sampler: new TraceIdRatioBasedSampler(0.1),
resource: new Resource({
resource: resourceFromAttributes({
// we can define some metadata about the trace resource
[SemanticResourceAttributes.SERVICE_NAME]: 'test-tracing-service',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
[ATTR_SERVICE_NAME]: 'test-tracing-service',
[ATTR_SERVICE_VERSION]: '1.0.0',
}),
})
});

```

## Troubleshoot tracing
Expand Down