Skip to content

Commit 22b40f6

Browse files
committed
Merge branch 'fix/otel-dupe-knex' into feat/kanon-request-metrics
2 parents 684b739 + fdb7a69 commit 22b40f6

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

.env.local.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ PUBSUB_EMULATOR_HOST=localhost:8085
6969
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
7070
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
7171
OTEL_SERVICE_NAME=monitor
72+
OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,knex,fs,grpc,aws-sdk,ioredis,winston,undici,runtime-node,redis"
73+
OTEL_RESOURCE_ATTRIBUTES="service.namespace=monitor-local,service.version=0.0.1"

src/instrumentation.node.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
ConsoleMetricExporter,
1111
PeriodicExportingMetricReader,
1212
} from "@opentelemetry/sdk-metrics";
13-
import { resourceFromAttributes } from "@opentelemetry/resources";
13+
import {
14+
envDetector,
15+
processDetector,
16+
resourceFromAttributes,
17+
} from "@opentelemetry/resources";
1418
import { NodeSDK } from "@opentelemetry/sdk-node";
1519
import {
1620
AlwaysOnSampler,
@@ -19,7 +23,6 @@ import {
1923
ParentBasedSampler,
2024
SimpleSpanProcessor,
2125
} from "@opentelemetry/sdk-trace-node";
22-
import { KnexInstrumentation } from "@opentelemetry/instrumentation-knex";
2326
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
2427
import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
2528
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
@@ -56,6 +59,15 @@ export async function nodeSDKBuilder() {
5659

5760
const sentryClient = Sentry.getClient();
5861

62+
// All required resources are injected through env vars
63+
// in cloud deployment, but locally we can use detectors
64+
// The host detector has a known issue with async attributes
65+
// and isn't super important
66+
// https://github.com/open-telemetry/opentelemetry-js/issues/4638
67+
const resourceDetectors = isLocalCollector
68+
? [processDetector, envDetector]
69+
: undefined;
70+
5971
if (sentryClient) {
6072
// https://github.com/getsentry/sentry-javascript/tree/develop/packages/opentelemetry
6173
setupEventContextTrace(sentryClient);
@@ -92,7 +104,6 @@ export async function nodeSDKBuilder() {
92104
const sdk = new NodeSDK({
93105
resource: resourceFromAttributes({
94106
[ATTR_SERVICE_NAME]: appConfig.otel.serviceName,
95-
...appConfig.otel.resourceAttributes,
96107
}),
97108
metricReaders: [
98109
new PeriodicExportingMetricReader({
@@ -113,17 +124,17 @@ export async function nodeSDKBuilder() {
113124
// Ensure the correct subset of traces is sent to Sentry
114125
// This also ensures trace propagation works as expected
115126
sampler,
127+
// Kuberenetes infrastructure automatically injects
128+
// required resources through environment variables
129+
autoDetectResources: isLocalCollector ? true : false,
130+
resourceDetectors,
116131
instrumentations: [
117132
getNodeAutoInstrumentations({
118133
"@opentelemetry/instrumentation-fs": {
119134
// Disable unless required by parent span (noisy and memory-intensive)
120135
enabled: false,
121136
requireParentSpan: true,
122137
},
123-
"@opentelemetry/instrumentation-pg": {
124-
// Disable because we're using @opentelemetry/instrumentation-knex
125-
enabled: false,
126-
},
127138
// Node native fetch instrumentation
128139
"@opentelemetry/instrumentation-undici": {
129140
// Ignore sending anonymous telemetry to next.js
@@ -146,13 +157,7 @@ export async function nodeSDKBuilder() {
146157
return false;
147158
},
148159
},
149-
// Similar to prometheus default metrics
150-
"@opentelemetry/instrumentation-runtime-node": {
151-
enabled: true,
152-
},
153160
}),
154-
// Additional instrumentations
155-
new KnexInstrumentation(),
156161
],
157162
});
158163
sdk.start();

0 commit comments

Comments
 (0)