1
+ /*
2
+ Copyright 2024 Splunk Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { diag } from '@opentelemetry/api' ;
18
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http' ;
19
+ import { NOOP_ATTRIBUTES_TRANSFORMER , NATIVE_XHR_SENDER , NATIVE_BEACON_SENDER , SplunkExporterConfig } from './common' ;
20
+ import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer' ;
21
+ import { ReadableSpan } from '@opentelemetry/sdk-trace-base' ;
22
+
23
+ export class SplunkOTLPTraceExporter extends OTLPTraceExporter {
24
+ protected readonly _onAttributesSerializing : SplunkExporterConfig [ 'onAttributesSerializing' ] ;
25
+ protected readonly _xhrSender : SplunkExporterConfig [ 'xhrSender' ] = NATIVE_XHR_SENDER ;
26
+ protected readonly _beaconSender : SplunkExporterConfig [ 'beaconSender' ] = typeof navigator !== 'undefined' && navigator . sendBeacon ? NATIVE_BEACON_SENDER : undefined ;
27
+
28
+ constructor ( options : SplunkExporterConfig ) {
29
+ super ( options ) ;
30
+ this . _onAttributesSerializing = options . onAttributesSerializing || NOOP_ATTRIBUTES_TRANSFORMER ;
31
+ }
32
+
33
+ convert ( spans : ReadableSpan [ ] ) : IExportTraceServiceRequest {
34
+ // Changes: Add attribute serializing hook to remove data before export
35
+ spans = spans . map ( span => {
36
+ // @ts -expect-error Yep we're overwriting a readonly property here. Deal with it
37
+ span . attributes = this . _onAttributesSerializing ? this . _onAttributesSerializing ( span . attributes , span ) : span . attributes ;
38
+ return span ;
39
+ } ) ;
40
+
41
+ return super . convert ( spans ) ;
42
+ }
43
+
44
+ send (
45
+ items : ReadableSpan [ ] ,
46
+ onSuccess : ( ) => void ,
47
+ ) : void {
48
+ if ( this . _shutdownOnce . isCalled ) {
49
+ diag . debug ( 'Shutdown already started. Cannot send objects' ) ;
50
+ return ;
51
+ }
52
+ const serviceRequest = this . convert ( items ) ;
53
+ const body = JSON . stringify ( serviceRequest ) ;
54
+
55
+ // Changed: Determine which exporter to use at the time of export
56
+ if ( document . hidden && this . _beaconSender && body . length <= 64000 ) {
57
+ this . _beaconSender ( this . url , body , { type : 'application/json' } ) ;
58
+ } else {
59
+ this . _xhrSender ! ( this . url , body , {
60
+ // These headers may only be necessary for otel's collector,
61
+ // need to test with actual ingest
62
+ Accept : 'application/json' ,
63
+ 'Content-Type' : 'application/json' ,
64
+ ...this . headers
65
+ } ) ;
66
+ }
67
+
68
+ onSuccess ( ) ;
69
+ }
70
+ }
0 commit comments