Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sdk/@launchdarkly/o11y/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ Most applications should install `@launchdarkly/observability` and/or
`@launchdarkly/session-replay` rather than depending on this package directly.
See the [SDK reference](https://docs.launchdarkly.com/sdk/observability).

To add attributes to every span, log, and metric, use
`LDObserve.setAttributes` / `LDObserve.removeAttributes`; see
[Global attributes](https://github.com/launchdarkly/observability-sdk/tree/main/sdk/%40launchdarkly/observability#global-attributes).

Session replay recording is powered by the
[launchdarkly/rrweb](https://github.com/launchdarkly/rrweb) fork.
31 changes: 31 additions & 0 deletions sdk/@launchdarkly/o11y/src/api/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,37 @@ export interface Observe {
metadata?: Attributes,
) => void

/**
* Set attributes that are added to every span, log, and metric recorded
* after the call, including automatically instrumented telemetry. Merges
* by key into the existing global attributes, which last for the page
* lifetime (they are not persisted and survive session changes).
*
* Attributes set explicitly on a span, log, or metric take precedence;
* global attributes only fill in keys that are missing. A span gets the
* global attributes current when it starts. SDK-managed session keys
* (`highlight.session_id`, `session.id`) and values that are not valid
* OpenTelemetry attribute values are ignored.
*
* Can be called before the SDK has initialized or started.
*
* @example
* ```typescript
* LDObserve.setAttributes({ 'app.tenant': 'acme', 'app.build': 42 })
* ```
*/
setAttributes: (attributes: Attributes) => void
/**
* Remove global attributes previously set with `setAttributes`.
* Telemetry recorded after the call no longer carries them.
*
* @example
* ```typescript
* LDObserve.removeAttributes(['app.tenant'])
* ```
*/
removeAttributes: (keys: string[]) => void

register(
client: LDClient,
environmentMetadata: LDPluginEnvironmentMetadata,
Expand Down
5 changes: 5 additions & 0 deletions sdk/@launchdarkly/o11y/src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import {
setSessionSecureID,
} from './utils/sessionStorage/highlightSession'
import { SESSION_STORAGE_KEYS } from './utils/sessionStorage/sessionStorageKeys'
import { getGlobalAttributes } from '../sdk/globalAttributes'
import {
getItem,
LocalStorageKeys,
Expand Down Expand Up @@ -1400,6 +1401,7 @@ SessionSecureID: ${this.sessionData.sessionSecureID}`,
this._gauges.set(metric.name, gauge)
}
gauge.record(metric.value, {
...getGlobalAttributes(),
...metric.tags?.reduce((a, b) => ({ ...a, [b.name]: b.value }), {}),
group: metric.group,
category: metric.category,
Expand All @@ -1421,6 +1423,7 @@ SessionSecureID: ${this.sessionData.sessionSecureID}`,
this._counters.set(metric.name, counter)
}
counter.add(metric.value, {
...getGlobalAttributes(),
...metric.tags?.reduce((a, b) => ({ ...a, [b.name]: b.value }), {}),
group: metric.group,
category: metric.category,
Expand All @@ -1443,6 +1446,7 @@ SessionSecureID: ${this.sessionData.sessionSecureID}`,
this._histograms.set(metric.name, histogram)
}
histogram.record(metric.value, {
...getGlobalAttributes(),
...metric.tags?.reduce((a, b) => ({ ...a, [b.name]: b.value }), {}),
group: metric.group,
category: metric.category,
Expand All @@ -1461,6 +1465,7 @@ SessionSecureID: ${this.sessionData.sessionSecureID}`,
this._up_down_counters.set(metric.name, up_down_counter)
}
up_down_counter.add(metric.value, {
...getGlobalAttributes(),
...metric.tags?.reduce((a, b) => ({ ...a, [b.name]: b.value }), {}),
group: metric.group,
category: metric.category,
Expand Down
4 changes: 3 additions & 1 deletion sdk/@launchdarkly/o11y/src/client/otel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
} from '../utils/sessionStorage/highlightSession'
import { isSdkDebugLogging, sdkLog } from '../utils/sdkLogger'
import { ProductAnalyticsEvents } from '../types/observe'
import { applyGlobalAttributesToSpan } from '../../sdk/globalAttributes'

export type Callback = (span?: Span) => any

Expand Down Expand Up @@ -647,7 +648,7 @@ export const registerFlushOnUnload = (
}
}

class CustomBatchSpanProcessor extends BatchSpanProcessor {
export class CustomBatchSpanProcessor extends BatchSpanProcessor {
private _pendingSpans = new Set<Promise<void>>()
// When set, forceFlush/shutdown return immediately without waiting for
// in-flight response-body reads. Used on page unload where waiting risks
Expand All @@ -660,6 +661,7 @@ class CustomBatchSpanProcessor extends BatchSpanProcessor {

onStart(span: SDKSpan, parentContext: Context): void {
span.setAttribute(SESSION_ID_ATTRIBUTE, getPersistentSessionSecureID())
applyGlobalAttributesToSpan(span)
super.onStart(span, parentContext)
}

Expand Down
11 changes: 11 additions & 0 deletions sdk/@launchdarkly/o11y/src/sdk/LDObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BufferedClass } from './buffer'
import { getNoopSpan } from '../client/otel/utils'
import { ConsoleMethods } from '../client/types/client'
import { internalLogOnce } from './util'
import { removeGlobalAttributes, setGlobalAttributes } from './globalAttributes'

/**
* Resolves the span callback from whichever argument position it was passed in,
Expand Down Expand Up @@ -136,6 +137,16 @@ class _LDObserve extends BufferedClass<Observe> implements Observe {
return this._bufferCall('setLDContextKeyAttributes', [contextKeys])
}

// Never buffered: the attributes must apply to telemetry recorded as soon
// as the SDK starts, including automatic spans emitted during init.
setAttributes(attributes: Attributes) {
setGlobalAttributes(attributes)
}

removeAttributes(keys: string[]) {
removeGlobalAttributes(keys)
}

getLDContextKeyAttributes(): Attributes | undefined {
// A getter: read directly once attached rather than buffering.
return this._isLoaded
Expand Down
Loading
Loading