Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add otlp exporter support behind config option #745

Merged
merged 11 commits into from
Mar 14, 2024
Merged
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
528 changes: 342 additions & 186 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "@splunk/otel-web-dev-root",
"private": true,
"version": "0.17.0-beta.1",
"--workspaces": "Hardcoded so npm runs workspaces commands in order",
"workspaces": [
"packages/*"
"packages/web",
"packages/session-recorder"
],
"engines": {
"--": "Versions required for development only (recent enough to use npm workspaces)",
Expand Down
6 changes: 3 additions & 3 deletions packages/session-recorder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
],
"dependencies": {
"@babel/runtime": "~7.22.6",
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/core": "^1.17.1",
"@opentelemetry/resources": "^1.17.1",
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/core": "^1.22.0",
"@opentelemetry/resources": "^1.22.0",
"fflate": "^0.8.0",
"protobufjs": "~7.2.4",
"rrweb": "^1.1.3",
Expand Down
5 changes: 2 additions & 3 deletions packages/session-recorder/src/OTLPLogExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { Resource } from '@opentelemetry/resources';
import { gzipSync } from 'fflate';
import type { Root } from 'protobufjs';
import type { JsonArray, JsonObject, JsonValue } from 'type-fest';
Expand All @@ -26,7 +25,7 @@ import { VERSION } from './version.js';
interface OTLPLogExporterConfig {
headers?: Record<string, string>;
beaconUrl: string;
resource: Resource;
getResourceAttributes: () => JsonObject;
debug?: boolean;
}

Expand Down Expand Up @@ -98,7 +97,7 @@ export default class OTLPLogExporter {
return {
resourceLogs: [{
resource: {
attributes: convertToAnyValue(this.config.resource?.attributes || {}).kvlistValue.values,
attributes: convertToAnyValue(this.config.getResourceAttributes() || {}).kvlistValue!.values,
},
scopeLogs: [{
scope: { name: 'splunk.rr-web', version: VERSION },
Expand Down
29 changes: 21 additions & 8 deletions packages/session-recorder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import { record } from 'rrweb';
import OTLPLogExporter from './OTLPLogExporter';
import { BatchLogProcessor, convert } from './BatchLogProcessor';
import { VERSION } from './version';
import { getGlobal } from './utils';

import type { Resource } from '@opentelemetry/resources';

import type { SplunkOtelWebType } from '@splunk/otel-web';

interface BasicTracerProvider extends TracerProvider {
readonly resource: Resource;
Expand Down Expand Up @@ -110,16 +111,18 @@ const SplunkRumRecorder = {
return;
}

const SplunkRum = getGlobal<SplunkOtelWebType>('splunk.rum');

let tracerProvider: BasicTracerProvider | ProxyTracerProvider = trace.getTracerProvider() as BasicTracerProvider;
if (tracerProvider && 'getDelegate' in tracerProvider) {
tracerProvider = (tracerProvider as unknown as ProxyTracerProvider).getDelegate() as BasicTracerProvider;
}
if (!(tracerProvider?.resource)) {
console.error('Splunk OTEL Web must be inited before recorder.');
if (!SplunkRum || !SplunkRum.resource) {
console.error('Splunk OTEL Web must be inited before session recorder.');
return;
}

const resource = tracerProvider.resource;
const resource = SplunkRum.resource;

migrateConfig(config);

Expand Down Expand Up @@ -156,10 +159,20 @@ const SplunkRumRecorder = {
exportUrl += `?auth=${rumAccessToken}`;
}

const exporter = new OTLPLogExporter({ beaconUrl: exportUrl, debug, headers, resource });
const exporter = new OTLPLogExporter({
beaconUrl: exportUrl,
debug,
headers,
getResourceAttributes() {
return {
...resource.attributes,
'splunk.rumSessionId': SplunkRum.getSessionId()!
};
}
});
const processor = new BatchLogProcessor(exporter, {});

lastKnownSession = resource.attributes['splunk.rumSessionId'] as string;
lastKnownSession = SplunkRum.getSessionId() as string;
sessionStartTime = Date.now();

inited = record({
Expand All @@ -174,11 +187,11 @@ const SplunkRumRecorder = {
// Safeguards from our ingest getting DDOSed:
// 1. A session can send up to 4 hours of data
// 2. Recording resumes on session change if it isn't a background tab (session regenerated in an another tab)
if (resource.attributes['splunk.rumSessionId'] !== lastKnownSession) {
if (SplunkRum.getSessionId() !== lastKnownSession) {
if (document.hidden) {
return;
}
lastKnownSession = resource.attributes['splunk.rumSessionId'] as string;
lastKnownSession = SplunkRum.getSessionId() as string;
sessionStartTime = Date.now();
// reset counters
eventCounter = 1;
Expand Down
36 changes: 36 additions & 0 deletions packages/session-recorder/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 Splunk Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { VERSION } from './version';

const GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for('opentelemetry.js.api.1');
/**
* otel-api's global function. This isn't exported by otel/api but would be
* super useful to access global components for experimental purposes...
* For us, it's included to access components accessed by other packages,
* eg. sharing session id manager with session recorder
*/
export function getGlobal<Type>(
type: string
): Type | undefined {
const globalSplunkRumVersion = globalThis[GLOBAL_OPENTELEMETRY_API_KEY]?.['splunk.rum.version'];
if (!globalSplunkRumVersion || globalSplunkRumVersion !== VERSION) {
console.warn(`SplunkSessionRecorder: Version mismatch with SplunkRum (RUM: ${globalSplunkRumVersion}, recorder: ${VERSION})`);
return undefined; // undefined for eslint
}

return globalThis[GLOBAL_OPENTELEMETRY_API_KEY]?.[type];
}
27 changes: 14 additions & 13 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@
],
"dependencies": {
"@babel/runtime": "^7.22.6",
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/core": "^1.17.1",
"@opentelemetry/exporter-zipkin": "^1.17.1",
"@opentelemetry/instrumentation": "^0.44.0",
"@opentelemetry/instrumentation-document-load": "^0.33.2",
"@opentelemetry/instrumentation-fetch": "^0.44.0",
"@opentelemetry/instrumentation-xml-http-request": "^0.44.0",
"@opentelemetry/resources": "^1.17.1",
"@opentelemetry/sdk-trace-base": "^1.17.1",
"@opentelemetry/sdk-trace-web": "^1.17.1",
"@opentelemetry/semantic-conventions": "^1.17.1",
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/core": "^1.22.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.49.1",
"@opentelemetry/exporter-zipkin": "^1.22.0",
"@opentelemetry/instrumentation": "^0.49.1",
"@opentelemetry/instrumentation-document-load": "^0.36.0",
"@opentelemetry/instrumentation-fetch": "^0.49.1",
"@opentelemetry/instrumentation-xml-http-request": "^0.49.1",
"@opentelemetry/resources": "^1.22.0",
"@opentelemetry/sdk-trace-base": "^1.22.0",
"@opentelemetry/sdk-trace-web": "^1.22.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"shimmer": "^1.2.1",
"web-vitals": "^3.4.0"
"web-vitals": "^3.5.2"
},
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.22.9",
Expand All @@ -79,7 +80,7 @@
"browserstack-local": "^1.5.3",
"chai": "^4.3.7",
"chrome-launcher": "^1.0.0",
"chromedriver": "^119.0.1",
"chromedriver": "^121.0.0",
"codecov": "^3.8.2",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/SplunkSpanAttributesProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

import { Attributes } from '@opentelemetry/api';
import { Span, SpanProcessor } from '@opentelemetry/sdk-trace-base';
import { getRumSessionId } from './session';

export class SplunkSpanAttributesProcessor implements SpanProcessor {
private readonly _globalAttributes: Attributes;
Expand Down Expand Up @@ -45,6 +46,7 @@ export class SplunkSpanAttributesProcessor implements SpanProcessor {
onStart(span: Span): void {
span.setAttribute('location.href', location.href);
span.setAttributes(this._globalAttributes);
span.setAttribute('splunk.rumSessionId', getRumSessionId());
}

onEnd(): void {
Expand Down
45 changes: 45 additions & 0 deletions packages/web/src/exporters/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2024 Splunk Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';

export interface SplunkExporterConfig {
url: string;
onAttributesSerializing?: (attributes: Attributes, span: ReadableSpan) => Attributes,
xhrSender?: (url: string, data: string, headers?: Record<string, string>) => void,
beaconSender?: (url: string, data: string, headers?: Record<string, string>) => void,
}

export function NOOP_ATTRIBUTES_TRANSFORMER(attributes: Attributes): Attributes {
return attributes;
}
export function NATIVE_XHR_SENDER(url: string, data: string, headers?: Record<string, string>): void {
const xhr = new XMLHttpRequest();
xhr.open('POST', url);
const defaultHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
Object.entries(Object.assign(Object.assign({}, defaultHeaders), headers)).forEach(([k, v]) => {
xhr.setRequestHeader(k, v);
});
xhr.send(data);
}
export function NATIVE_BEACON_SENDER(url: string, data: string, blobPropertyBag?: BlobPropertyBag): void {
const payload = blobPropertyBag ? new Blob([data], blobPropertyBag) : data;
navigator.sendBeacon(url, payload);
}
70 changes: 70 additions & 0 deletions packages/web/src/exporters/otlp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2024 Splunk Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { NOOP_ATTRIBUTES_TRANSFORMER, NATIVE_XHR_SENDER, NATIVE_BEACON_SENDER, SplunkExporterConfig } from './common';
import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer';
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';

export class SplunkOTLPTraceExporter extends OTLPTraceExporter {
protected readonly _onAttributesSerializing: SplunkExporterConfig['onAttributesSerializing'];
protected readonly _xhrSender: SplunkExporterConfig['xhrSender'] = NATIVE_XHR_SENDER;
protected readonly _beaconSender: SplunkExporterConfig['beaconSender'] = typeof navigator !== 'undefined' && navigator.sendBeacon ? NATIVE_BEACON_SENDER : undefined;

constructor(options: SplunkExporterConfig) {
super(options);
this._onAttributesSerializing = options.onAttributesSerializing || NOOP_ATTRIBUTES_TRANSFORMER;
}

convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
// Changes: Add attribute serializing hook to remove data before export
spans = spans.map(span => {
// @ts-expect-error Yep we're overwriting a readonly property here. Deal with it
span.attributes = this._onAttributesSerializing ? this._onAttributesSerializing(span.attributes, span) : span.attributes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._onAttributesSerializing alway true no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be, tho types says maybe

return span;
});

return super.convert(spans);
}

send(
items: ReadableSpan[],
onSuccess: () => void,
): void {
if (this._shutdownOnce.isCalled) {
diag.debug('Shutdown already started. Cannot send objects');
return;
}
const serviceRequest = this.convert(items);
const body = JSON.stringify(serviceRequest);

// Changed: Determine which exporter to use at the time of export
if (document.hidden && this._beaconSender && body.length <= 64000) {
this._beaconSender(this.url, body, { type: 'application/json' });
} else {
this._xhrSender!(this.url, body, {
// These headers may only be necessary for otel's collector,
// need to test with actual ingest
Accept: 'application/json',
'Content-Type': 'application/json',
...this.headers
});
}

onSuccess();
}
}
Loading
Loading