Skip to content

Commit ad896e6

Browse files
committed
feat: Update instrumentation configuration for ease of use.
1 parent 408d84b commit ad896e6

File tree

7 files changed

+1184
-70
lines changed

7 files changed

+1184
-70
lines changed

sdk/@launchdarkly/observability-node/package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@
2929
"dependencies": {
3030
"@graphql-codegen/cli": "^5.0.7",
3131
"@launchdarkly/node-server-sdk-otel": "^1.3.0",
32+
"@opentelemetry/api": "^1.9.0",
33+
"@opentelemetry/api-logs": "^0.203.0",
34+
"@opentelemetry/auto-instrumentations-node": "^0.62.0",
35+
"@opentelemetry/core": "^2.0.1",
36+
"@opentelemetry/exporter-jaeger": "^2.0.1",
37+
"@opentelemetry/exporter-logs-otlp-http": "^0.203.0",
38+
"@opentelemetry/exporter-metrics-otlp-http": "^0.203.0",
39+
"@opentelemetry/exporter-trace-otlp-http": "^0.203.0",
40+
"@opentelemetry/instrumentation": "^0.203.0",
41+
"@opentelemetry/otlp-exporter-base": "^0.203.0",
42+
"@opentelemetry/otlp-transformer": "^0.203.0",
43+
"@opentelemetry/resources": "^2.0.1",
44+
"@opentelemetry/sdk-logs": "^0.203.0",
45+
"@opentelemetry/sdk-node": "^0.203.0",
46+
"@opentelemetry/sdk-trace-base": "^2.0.1",
47+
"@opentelemetry/semantic-conventions": "^1.34.0",
48+
"@opentelemetry/winston-transport": "^0.14.0",
3249
"@prisma/instrumentation": ">=5.0.0",
3350
"require-in-the-middle": "^7.4.0"
3451
},
@@ -38,23 +55,6 @@
3855
},
3956
"devDependencies": {
4057
"@launchdarkly/node-server-sdk": "^9.9.2",
41-
"@opentelemetry/api": "^1.9.0",
42-
"@opentelemetry/api-logs": "^0.57.2",
43-
"@opentelemetry/auto-instrumentations-node": "^0.56.0",
44-
"@opentelemetry/core": "^1.30.1",
45-
"@opentelemetry/exporter-jaeger": "^1.30.1",
46-
"@opentelemetry/exporter-logs-otlp-http": "^0.57.2",
47-
"@opentelemetry/exporter-metrics-otlp-http": "^0.57.2",
48-
"@opentelemetry/exporter-trace-otlp-http": "^0.57.2",
49-
"@opentelemetry/instrumentation": "^0.57.2",
50-
"@opentelemetry/otlp-exporter-base": "^0.57.2",
51-
"@opentelemetry/otlp-transformer": "^0.57.2",
52-
"@opentelemetry/resources": "^1.30.1",
53-
"@opentelemetry/sdk-logs": "^0.57.2",
54-
"@opentelemetry/sdk-node": "^0.57.2",
55-
"@opentelemetry/sdk-trace-base": "^1.30.1",
56-
"@opentelemetry/semantic-conventions": "^1.30.0",
57-
"@opentelemetry/winston-transport": "^0.10.0",
5858
"@rollup/plugin-commonjs": "^28.0.2",
5959
"@rollup/plugin-json": "^6.1.0",
6060
"@rollup/plugin-node-resolve": "^16.0.0",

sdk/@launchdarkly/observability-node/rollup.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ const config = {
3636
format: 'es',
3737
sourcemap: true,
3838
exports: 'auto',
39+
inlineDynamicImports: true,
3940
},
4041
{
4142
file: 'dist/index.cjs',
4243
format: 'cjs',
4344
sourcemap: true,
4445
exports: 'auto',
46+
inlineDynamicImports: true,
4547
},
4648
],
4749
treeshake: 'safest',

sdk/@launchdarkly/observability-node/src/api/Options.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ export const ALL_CONSOLE_METHODS = [
2323
type ConsoleMethodsTuple = typeof ALL_CONSOLE_METHODS
2424
export type ConsoleMethods = ConsoleMethodsTuple[number]
2525

26+
/**
27+
* Options for configuring the LaunchDarkly Observability Plugin.
28+
*
29+
* Additionally the following environment variables can be used to configure
30+
* the plugin.
31+
*
32+
* LAUNCHDARKLY_OTEL_NODE_ENABLE_FILESYSTEM_INSTRUMENTATION - Enable filesystem
33+
* instrumentation. Defaults to false.
34+
* LAUNCHDARKLY_OTEL_NODE_ENABLE_OUTGOING_HTTP_INSTRUMENTATION - Enable outgoing
35+
* HTTP instrumentation. Defaults to true.
36+
* This only affects the outgoing HTTP requests instrumented by
37+
* `@opentelemetry/instrumentation-http`. It does not affect fetch for example.
38+
*
39+
* OTEL_NODE_ENABLED_INSTRUMENTATIONS, and OTEL_NODE_DISABLED_INSTRUMENTATIONS
40+
* can be used per the OpenTelemetry documentation, but with a few exceptions.
41+
* https://opentelemetry.io/docs/zero-code/js/configuration/
42+
*
43+
* The `@opentelemetry/instrumentation-fs` instrumentation will only be enabled
44+
* if LAUNCHDARKLY_OTEL_NODE_ENABLE_FILESYSTEM_INSTRUMENTATION is true, and
45+
* will be unaffected by the OTEL_NODE_ENABLED_INSTRUMENTATIONS and
46+
* OTEL_NODE_DISABLED_INSTRUMENTATIONS environment variables.
47+
*/
2648
export interface NodeOptions {
2749
/**
2850
* The endpoint string to send OTLP HTTP data to.
@@ -82,4 +104,18 @@ export interface NodeOptions {
82104
* @example consoleMethodsToRecord: ['log', 'info', 'error']
83105
*/
84106
consoleMethodsToRecord?: ConsoleMethods[]
107+
108+
/**
109+
* Specifies whether to enable HTTP instrumentation.
110+
*
111+
* @default true
112+
*/
113+
enableHttpInstrumentation?: boolean
114+
115+
/**
116+
* Specifies whether to enable filesystem instrumentation.
117+
*
118+
* @default false
119+
*/
120+
enableFileSystemInstrumentation?: boolean
85121
}

sdk/@launchdarkly/observability-node/src/client/ObservabilityClient.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import {
3232
registerInstrumentations,
3333
} from '@opentelemetry/instrumentation'
3434
import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base'
35-
import { processDetectorSync, Resource } from '@opentelemetry/resources'
35+
import {
36+
processDetector,
37+
resourceFromAttributes,
38+
} from '@opentelemetry/resources'
3639
import { NodeSDK } from '@opentelemetry/sdk-node'
3740
import {
3841
AlwaysOnSampler,
@@ -70,16 +73,20 @@ export const HIGHLIGHT_REQUEST_HEADER = 'x-highlight-request'
7073

7174
const instrumentations = getNodeAutoInstrumentations({
7275
'@opentelemetry/instrumentation-http': {
73-
enabled:
74-
(process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS || '')
75-
.split(',')
76-
.indexOf('http') !== -1,
76+
disableOutgoingRequestInstrumentation:
77+
(
78+
process.env
79+
.LAUNCHDARKLY_OTEL_NODE_ENABLE_OUTGOING_HTTP_INSTRUMENTATION ||
80+
''
81+
).toLowerCase() === 'false',
7782
},
7883
'@opentelemetry/instrumentation-fs': {
7984
enabled:
80-
(process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS || '')
81-
.split(',')
82-
.indexOf('fs') !== -1,
85+
(
86+
process.env
87+
.LAUNCHDARKLY_OTEL_NODE_ENABLE_FILESYSTEM_INSTRUMENTATION ||
88+
''
89+
).toLowerCase() === 'true',
8390
},
8491
'@opentelemetry/instrumentation-pino': {
8592
logHook: (span, record, _) => {
@@ -91,6 +98,7 @@ const instrumentations = getNodeAutoInstrumentations({
9198
},
9299
},
93100
})
101+
94102
instrumentations.push(new PrismaInstrumentation())
95103
registerInstrumentations({ instrumentations })
96104

@@ -216,7 +224,7 @@ export class ObservabilityClient {
216224
attributes[otelAttr] = options[option]
217225
}
218226
}
219-
const resource = new Resource(attributes)
227+
const resource = resourceFromAttributes(attributes)
220228

221229
const sampler = new CustomSampler()
222230
this._getSamplingConfig(sampler)
@@ -229,18 +237,19 @@ export class ObservabilityClient {
229237
)
230238
this.processor = new BatchSpanProcessor(exporter, opts)
231239

232-
this.loggerProvider = new LoggerProvider({
233-
resource,
234-
})
235240
const logsExporter = new SamplingLogExporter(
236241
{
237242
...config,
238243
url: `${config.url}/v1/logs`,
239244
},
240245
sampler,
241246
)
247+
242248
const logsProcessor = new BatchLogRecordProcessor(logsExporter, opts)
243-
this.loggerProvider.addLogRecordProcessor(logsProcessor)
249+
this.loggerProvider = new LoggerProvider({
250+
resource,
251+
processors: [logsProcessor],
252+
})
244253

245254
const metricsExporter = new OTLPMetricExporter({
246255
...config,
@@ -254,7 +263,7 @@ export class ObservabilityClient {
254263

255264
this.otel = new NodeSDK({
256265
autoDetectResources: true,
257-
resourceDetectors: [processDetectorSync],
266+
resourceDetectors: [processDetector],
258267
resource,
259268
spanProcessors: [this.processor],
260269
logRecordProcessors: [logsProcessor],

sdk/@launchdarkly/observability-node/src/otel/sampling/sampleSpans.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReadableSpan } from '@opentelemetry/sdk-trace-web'
1+
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'
22
import { Attributes } from '@opentelemetry/api'
33
import { merge } from '@opentelemetry/core'
44
import { ExportSampler } from './ExportSampler'
@@ -12,7 +12,7 @@ export function cloneReadableSpanWithAttributes(
1212
name: span.name,
1313
kind: span.kind,
1414
spanContext: () => spanContext,
15-
parentSpanId: span.parentSpanId,
15+
parentSpanContext: span.parentSpanContext,
1616
startTime: span.startTime,
1717
endTime: span.endTime,
1818
status: span.status,
@@ -22,7 +22,7 @@ export function cloneReadableSpanWithAttributes(
2222
duration: span.duration,
2323
ended: span.ended,
2424
resource: span.resource,
25-
instrumentationLibrary: span.instrumentationLibrary,
25+
instrumentationScope: span.instrumentationScope,
2626
droppedAttributesCount: span.droppedAttributesCount,
2727
droppedEventsCount: span.droppedEventsCount,
2828
droppedLinksCount: span.droppedLinksCount,
@@ -44,10 +44,10 @@ export function sampleSpans(
4444
// The first pass we sample items which are directly impacted by a sampling decision.
4545
// We also build a map of children spans by parent span id, which allows us to quickly traverse the span tree.
4646
for (const item of items) {
47-
if (item.parentSpanId) {
48-
childrenByParentId[item.parentSpanId] =
49-
childrenByParentId[item.parentSpanId] || []
50-
childrenByParentId[item.parentSpanId].push(
47+
if (item.parentSpanContext?.spanId) {
48+
childrenByParentId[item.parentSpanContext.spanId] =
49+
childrenByParentId[item.parentSpanContext.spanId] || []
50+
childrenByParentId[item.parentSpanContext.spanId].push(
5151
item.spanContext().spanId,
5252
)
5353
}

sdk/@launchdarkly/observability-node/src/sdk/LDObserve.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Attributes, Span as OtelSpan, SpanOptions } from '@opentelemetry/api'
2-
import { ResourceAttributes } from '@opentelemetry/resources'
32
import { ObservabilityClient } from '../client/ObservabilityClient'
43

54
import { RequestContext } from '../api/RequestContext'
@@ -129,7 +128,7 @@ const _LDObserve = {
129128
) => {
130129
return observabilityClient.startWithHeaders(spanName, headers, options)
131130
},
132-
setAttributes: (attributes: ResourceAttributes) => {
131+
setAttributes: (attributes: Attributes) => {
133132
return observabilityClient.setAttributes(attributes)
134133
},
135134
_debug: (...data: any[]) => {

0 commit comments

Comments
 (0)