DevInSpe
DevInSpe

Reputation: 11

Exporting OpenTelemetry data in node.js to OTLP endpoint AND to local console

I am super impressed by the OpenTelemetry project and would like to learn more about it and experiment with it. This is super flexible and I am not even locked in to a vendor.

Currently I am working with node.js and ideally I would like to expose all available information (traces, metrics, logs) of my services via OTLP and (probably in the future just in case of debugging) to the console.

When starting my demo app I use --require ./instrumentation.ts

and the instrumentation.ts looks like this:

import { NodeSDK } from '@opentelemetry/sdk-node';
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
import { envDetector, processDetector } from '@opentelemetry/resources';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { Resource }  from "@opentelemetry/resources";
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import {  PeriodicExportingMetricReader} from '@opentelemetry/sdk-metrics';
import {  BatchLogRecordProcessor } from "@opentelemetry/sdk-logs"; 
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';


const otlpTraceExporter = new OTLPTraceExporter({
  url: 'http://localhost:4318/v1/traces',
});

const otlpMetricExporter = new OTLPMetricExporter({
  url: 'http://localhost:4318/v1/metrics',
});

const otlpMetricReader = new PeriodicExportingMetricReader({
  exporter: otlpMetricExporter,
  exportIntervalMillis: 1000
});

const logExporter = new OTLPLogExporter({
  url: 'http://localhost:4318/v1/logs',
});

const otlpLogProcessor = new BatchLogRecordProcessor(logExporter);

const sdk = new NodeSDK({
  resourceDetectors: [envDetector, processDetector],
  resource: new Resource({
    [ SEMRESATTRS_SERVICE_NAME ]: "my-service-name",
  }),

  instrumentations: [
    getNodeAutoInstrumentations(),
  ], 
  spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter()), new BatchSpanProcessor(otlpTraceExporter)],
  metricReader: otlpMetricReader,
  logRecordProcessor: otlpLogProcessor,
});

sdk.start();

From my point of view super straightforward and for traces I can specify two spanProcessors. How can I do this with metrics and logs?

I have already checked the documentation and search the web for a solution, but I have not found anything yet. I can probably do this manually creating MeterProviders and LoggerProviders and adding it there, but I think there must be an easier way.

Thanks a lot, Peter

Upvotes: 1

Views: 1349

Answers (0)

Related Questions