All files / packages/observability/src node.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                1x           6x 6x                                   6x   6x   6x    
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";
import { defaultResource, resourceFromAttributes } from "@opentelemetry/resources";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import {
    ATTR_DEPLOYMENT_ENVIRONMENT_NAME,
    ATTR_SERVICE_NAME,
    ATTR_SERVICE_VERSION,
} from "@opentelemetry/semantic-conventions";
 
interface NodeTelemetryConfig {
    url: string;
    instanceId: string;
    token: string;
 
    app: {
        name: string;
        version: string;
        environment: string;
    };
}
 
export const startNodeTelemetry = ({
    url,
    instanceId,
    token,
    app,
}: NodeTelemetryConfig): NodeTracerProvider => {
    const credentials = Buffer.from(`${instanceId}:${token}`).toString("base64");
    const provider = new NodeTracerProvider({
        resource: defaultResource().merge(
            resourceFromAttributes({
                [ATTR_SERVICE_NAME]: app.name,
                [ATTR_SERVICE_VERSION]: app.version,
                [ATTR_DEPLOYMENT_ENVIRONMENT_NAME]: app.environment,
            }),
        ),
        spanProcessors: [
            new BatchSpanProcessor(
                new OTLPTraceExporter({
                    url: `${url.replace(/\/$/, "")}/v1/traces`,
                    headers: { Authorization: `Basic ${credentials}` },
                }),
            ),
        ],
    });
 
    provider.register();
 
    registerInstrumentations({ instrumentations: [new UndiciInstrumentation()] });
 
    return provider;
};