-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8e876f0
Add otlp exporter support behind config option
t2t2 e5972a3
Fixes for tests
t2t2 caaef31
update package-lock
t2t2 62f4ad9
fix tests a bit more
t2t2 be66fc7
update deps
t2t2 06f7e56
session.id on spans, change how session recorder gets session id
t2t2 7b8f61c
fix weird past logic in rate-limit
t2t2 fda5769
force workspace scripts order
t2t2 2f9c526
update test
t2t2 b7e1b16
mochaaaaaaaa
t2t2 580da54
Merge branch 'main' into add-otlp-exporter
t2t2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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