Skip to content

Commit

Permalink
Merge branch 'main' into warren/rename-api-key-param
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 authored Jun 27, 2024
2 parents e4f2347 + f84fd61 commit 24affd9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-ravens-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/node-opentelemetry': patch
---

fix: introduce pino mixin function to handle trace/span id injection issue
19 changes: 11 additions & 8 deletions packages/node-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ configure your logging module.

```ts
import winston from 'winston';
import { getWinstonTransport } from '@hyperdx/node-opentelemetry';
import * as HyperDX from '@hyperdx/node-opentelemetry';

const MAX_LEVEL = 'info';

Expand All @@ -34,7 +34,7 @@ const logger = winston.createLogger({
format: winston.format.json(),
transports: [
new winston.transports.Console(),
getWinstonTransport(MAX_LEVEL), // append this to the existing transports
HyperDX.getWinstonTransport(MAX_LEVEL), // append this to the existing transports
],
});

Expand All @@ -45,18 +45,21 @@ export default logger;

```ts
import pino from 'pino';
import { getPinoTransport } from '@hyperdx/node-opentelemetry';
import * as HyperDX from '@hyperdx/node-opentelemetry';

const MAX_LEVEL = 'info';

const logger = pino(
pino.transport({
const logger = pino({
mixin: HyperDX.getPinoMixinFunction,
transport: {
targets: [
getPinoTransport(MAX_LEVEL),
HyperDX.getPinoTransport(MAX_LEVEL),
// other transports
],
}),
);
},
});

export default logger;
```

### Configure Environment Variables
Expand Down
13 changes: 7 additions & 6 deletions packages/node-opentelemetry/examples/dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const mongoose = require('mongoose');
const mysql = require('mysql');
const mysql2 = require('mysql2');
const pg = require('pg');
const pino = require('pino');
const winston = require('winston');
const pino = require('pino');

const HyperDX = require('../build/src');

HyperDX.init({
apiKey: 'blabla',
apiKey: '',
});

// setTimeout(() => {
Expand Down Expand Up @@ -80,14 +80,15 @@ const logger = winston.createLogger({
],
});

const pinoLogger = pino(
pino.transport({
const pinoLogger = pino({
mixin: HyperDX.getPinoMixinFunction,
transport: {
targets: [
HyperDX.getPinoTransport('info'),
// other transports
],
}),
);
},
});

const bunyanLogger = bunyan.createLogger({ name: 'myapp' });

Expand Down
3 changes: 3 additions & 0 deletions packages/node-opentelemetry/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DEFAULT_HDX_NODE_BETA_MODE,
DEFAULT_SERVICE_NAME,
} from './constants';
import * as HyperDXPino from './otel-logger/pino';
import HyperDXWinston from './otel-logger/winston';

import type { HyperDXPinoOptions } from './otel-logger/pino';
Expand Down Expand Up @@ -55,11 +56,13 @@ export const getWinstonTransport = (
// TODO: WILL BE DEPRECATED
export const getWinsonTransport = getWinstonTransport;

export const getPinoMixinFunction = HyperDXPino.getMixinFunction;
export const getPinoTransport = (
maxLevel = 'info',
options: PinotTransportOptions = {},
) => {
const apiKey = DEFAULT_HDX_API_KEY();

return {
target: '@hyperdx/node-opentelemetry/build/src/otel-logger/pino',
options: {
Expand Down
32 changes: 31 additions & 1 deletion packages/node-opentelemetry/src/otel-logger/pino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import build from 'pino-abstract-transport';
import isString from 'lodash.isstring';
import { Attributes, diag } from '@opentelemetry/api';
import {
Attributes,
context,
diag,
isSpanContextValid,
trace,
} from '@opentelemetry/api';

import { Logger } from './';
import { jsonToString } from '../utils';
Expand Down Expand Up @@ -48,6 +54,30 @@ export type HyperDXPinoOptions = LoggerOptions & {
getCustomMeta?: () => Attributes;
};

// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/65bc979f9f04410e9a78b4d546f5e08471c1fb6d/plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts#L167C11-L167C30
const DEFAULT_LOG_KEYS = {
traceId: 'trace_id',
spanId: 'span_id',
traceFlags: 'trace_flags',
};
export const getMixinFunction = () => {
const span = trace.getSpan(context.active());
if (!span) {
return {};
}

const spanContext = span.spanContext();

if (!isSpanContextValid(spanContext)) {
return {};
}
return {
[DEFAULT_LOG_KEYS.traceId]: spanContext.traceId,
[DEFAULT_LOG_KEYS.spanId]: spanContext.spanId,
[DEFAULT_LOG_KEYS.traceFlags]: `0${spanContext.traceFlags.toString(16)}`,
};
};

export default async ({
apiKey,
getCustomMeta,
Expand Down

0 comments on commit 24affd9

Please sign in to comment.