Skip to content

Commit 0fb9563

Browse files
authored
Correlating logs to traces (#980)
* logs correlation * update comment
1 parent 8f77e3e commit 0fb9563

File tree

7 files changed

+111
-19
lines changed

7 files changed

+111
-19
lines changed

next.config.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ const nextConfig = {
1919
'@': path.resolve(__dirname, './src'),
2020
};
2121
if (options.isServer) {
22-
config.externals.push('@grpc/grpc-js', 'require-in-the-middle', '@opentelemetry/exporter-jaeger');
22+
config.externals.push(
23+
'@grpc/grpc-js',
24+
'require-in-the-middle',
25+
'pino',
26+
/^@opentelemetry\//
27+
);
2328
}
2429
return config;
2530
},

package-lock.json

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@opentelemetry/exporter-trace-otlp-grpc": "^0.200.0",
3434
"@opentelemetry/instrumentation-grpc": "^0.200.0",
3535
"@opentelemetry/instrumentation-http": "^0.200.0",
36+
"@opentelemetry/instrumentation-pino": "^0.50.0",
3637
"@opentelemetry/instrumentation-undici": "^0.11.0",
3738
"@opentelemetry/propagator-jaeger": "^2.0.0",
3839
"@opentelemetry/resources": "^2.0.0",

src/instrumentation.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
import logger, { registerLoggers } from '@/utils/logger';
1+
import { type Instrumentation } from '@opentelemetry/instrumentation';
22

33
import getTransformedConfigs from './utils/config/get-transformed-configs';
44
import { setLoadedGlobalConfigs } from './utils/config/global-configs-ref';
55

66
export async function register() {
7+
let instrumentations: (Instrumentation | Instrumentation[])[] = [];
8+
// register instrumentations before any other code is executed
9+
if (
10+
process.env.NEXT_RUNTIME === 'nodejs' &&
11+
process.env.OTEL_SDK_DISABLED === 'false'
12+
) {
13+
instrumentations = (
14+
await import('@/utils/otel/otel-register-instrumentations')
15+
).otelRegisterInstrumentations({});
16+
}
17+
18+
// import/register loggers after instrumentations are registered to have instrumented pino along with logs correlation
19+
const { registerLoggers, default: logger } = await import('@/utils/logger');
720
registerLoggers();
821

922
if (process.env.NEXT_RUNTIME === 'nodejs') {
1023
if (process.env.OTEL_SDK_DISABLED === 'false') {
11-
(await import('@/utils/otel/otel-register')).register();
24+
await (
25+
await import('@/utils/otel/otel-register')
26+
).otelRegister({
27+
instrumentations,
28+
});
1229
}
13-
1430
try {
1531
const configs = await getTransformedConfigs();
1632
setLoadedGlobalConfigs(configs);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
GrpcInstrumentation,
3+
type GrpcInstrumentationConfig,
4+
} from '@opentelemetry/instrumentation-grpc';
5+
import {
6+
HttpInstrumentation,
7+
type HttpInstrumentationConfig,
8+
} from '@opentelemetry/instrumentation-http';
9+
import {
10+
PinoInstrumentation,
11+
type PinoInstrumentationConfig,
12+
} from '@opentelemetry/instrumentation-pino';
13+
import {
14+
UndiciInstrumentation,
15+
type UndiciInstrumentationConfig,
16+
} from '@opentelemetry/instrumentation-undici';
17+
18+
export function otelRegisterInstrumentations({
19+
grpcInstrumentationConfig,
20+
httpInstrumentationConfig,
21+
undiciInstrumentationConfig,
22+
pinoInstrumentationConfig,
23+
}: {
24+
grpcInstrumentationConfig?: GrpcInstrumentationConfig;
25+
httpInstrumentationConfig?: HttpInstrumentationConfig;
26+
undiciInstrumentationConfig?: UndiciInstrumentationConfig;
27+
pinoInstrumentationConfig?: PinoInstrumentationConfig;
28+
} = {}) {
29+
return [
30+
new GrpcInstrumentation(grpcInstrumentationConfig),
31+
new HttpInstrumentation(httpInstrumentationConfig),
32+
new UndiciInstrumentation(undiciInstrumentationConfig),
33+
new PinoInstrumentation(pinoInstrumentationConfig),
34+
];
35+
}

src/utils/otel/otel-register.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
2-
import { GrpcInstrumentation } from '@opentelemetry/instrumentation-grpc';
3-
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
4-
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
52
import { JaegerPropagator } from '@opentelemetry/propagator-jaeger';
63
import { resourceFromAttributes } from '@opentelemetry/resources';
74
import { NodeSDK } from '@opentelemetry/sdk-node';
@@ -11,20 +8,14 @@ import logger from '@/utils/logger';
118

129
import { type OtelRegisterConfig } from './otel.types';
1310

14-
export async function register(config?: OtelRegisterConfig) {
15-
const { grpcInstrumentationConfig, ...sdkConfig } = config || {};
11+
export async function otelRegister(config?: OtelRegisterConfig) {
1612
const sdk = new NodeSDK({
1713
resource: resourceFromAttributes({
1814
[ATTR_SERVICE_NAME]: 'cadence-web',
1915
}),
20-
instrumentations: [
21-
new GrpcInstrumentation(grpcInstrumentationConfig),
22-
new HttpInstrumentation(),
23-
new UndiciInstrumentation(),
24-
],
2516
textMapPropagator: new JaegerPropagator(),
2617
traceExporter: new OTLPTraceExporter(),
27-
...sdkConfig,
18+
...config,
2819
});
2920
try {
3021
await sdk.start();

src/utils/otel/otel.types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { type GrpcInstrumentationConfig } from '@opentelemetry/instrumentation-grpc';
21
import { type NodeSDKConfiguration } from '@opentelemetry/sdk-node';
32

4-
export type OtelRegisterConfig = Partial<NodeSDKConfiguration> & {
5-
grpcInstrumentationConfig?: GrpcInstrumentationConfig;
6-
};
3+
export type OtelRegisterConfig = Partial<NodeSDKConfiguration>;

0 commit comments

Comments
 (0)