Skip to content

Instantly share code, notes, and snippets.

@thecraftman
Last active January 4, 2024 16:09
Show Gist options
  • Save thecraftman/27fdc2573ba1bf6c0853fc64b7593432 to your computer and use it in GitHub Desktop.
Save thecraftman/27fdc2573ba1bf6c0853fc64b7593432 to your computer and use it in GitHub Desktop.
Send Otel Node traces to Axiom
const opentelemetry = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
// Initialize OTLP trace exporter
const traceExporter = new OTLPTraceExporter({
url: 'https://api.axiom.co/v1/traces',
headers: {
'Authorization': 'Bearer $API_TOKEN',
'X-Axiom-Dataset': '$DATASET'
},
});
const resource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'node traces',
});
const sdk = new opentelemetry.NodeSDK({
spanProcessor: new BatchSpanProcessor(traceExporter),
resource: resource, // Add the resource here
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
@pmbanugo
Copy link

pmbanugo commented Jan 2, 2024

Thanks for sharing. Is it needed for me to specify a value for traceExporter or is it enough to just use the spanProcessor?

For example, line 21 would become

const sdk = new opentelemetry.NodeSDK({
  spanProcessor: new BatchSpanProcessor(traceExporter),
  traceExporter: traceExporter,
  resource: resource, // Add the resource here
  instrumentations: [getNodeAutoInstrumentations()],
});

@pmbanugo
Copy link

pmbanugo commented Jan 2, 2024

Also, if I use your code in TypeScript and build it, I get the error

../../node_modules/.pnpm/@opentelemetry+otlp-exporter-base@0.46.0_@opentelemetry+api@1.7.0/node_modules/@opentelemetry/otlp-exporter-base/build/src/platform/browser/util.d.ts:10:84 - error TS2304: Cannot find name 'BlobPropertyBag'.

any idea for a fix?

@thecraftman
Copy link
Author

Thanks @pmbanugo is it working for you?

No, it's not needed to specify traceExporter separately in the NodeSDK configuration when you're already using it in the BatchSpanProcessor

@thecraftman
Copy link
Author

thecraftman commented Jan 2, 2024

Also, if I use your code in TypeScript and build it, I get the error

../../node_modules/.pnpm/@opentelemetry+otlp-exporter-base@0.46.0_@opentelemetry+api@1.7.0/node_modules/@opentelemetry/otlp-exporter-base/build/src/platform/browser/util.d.ts:10:84 - error TS2304: Cannot find name 'BlobPropertyBag'.

any idea for a fix?

which version of ts are you using? i think older versions might not have the recent definitions for the DOM. in your compiler options can you confirm in tsconfig.json:

{
  "compilerOptions": {
    "lib": ["dom", "es2015", ],
    
  }
}

@pmbanugo
Copy link

pmbanugo commented Jan 2, 2024

My lib option is "lib": ["ES2023"],. I'm compiling for Node so I omit the DOM types.

Anyway, I got a hack to make it compile but it still doesn't export the logs. I'm guessing it's something with otel. Maybe you could check with your team for someone that's familiar with logging for OTel in JavaScript.

I could try to configure it with the pino or winston extension directly with Axiom. But I'd prefer auto-instrumentation if it;s possible

@thecraftman
Copy link
Author

The config above is to send traces to Axiom using url: 'https://api.axiom.co/v1/traces You can use our supported SDKs to log into Axiom:

@thecraftman
Copy link
Author

The js logs package for Otel is still under development and experimental: https://opentelemetry.io/docs/instrumentation/js/

@pmbanugo
Copy link

pmbanugo commented Jan 4, 2024

Thanks. I got it working it working with the pino package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment