forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f078f5
commit cecabd3
Showing
1 changed file
with
135 additions
and
75 deletions.
There are no files selected for viewing
210 changes: 135 additions & 75 deletions
210
packages/opentelemetry-exporter-collector/src/platform/browser/CollectorExporter.ts
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 |
---|---|---|
@@ -1,84 +1,144 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 { CollectorTraceExporterBase } from '../../CollectorTraceExporterBase'; | ||
import { ReadableSpan } from '@opentelemetry/tracing'; | ||
import { toCollectorExportTraceServiceRequest } from '../../transform'; | ||
import * as collectorTypes from '../../types'; | ||
import { CollectorExporterConfigBrowser } from '../../types'; | ||
import { sendWithBeacon, sendWithXhr } from './util'; | ||
import { | ||
CollectorExporterBase, | ||
CollectorExporterConfigBase, | ||
} from '../../CollectorExporterBase'; | ||
import { ReadableSpan } from '@opentelemetry/tracing'; | ||
import { toCollectorExportTraceServiceRequest } from '../../transform'; | ||
import * as collectorTypes from '../../types'; | ||
|
||
const DEFAULT_COLLECTOR_URL = 'http://localhost:55678/v1/trace'; | ||
/** | ||
* Collector Exporter Config for Web | ||
*/ | ||
export interface CollectorExporterConfig extends CollectorExporterConfigBase { | ||
headers?: { [key: string]: string }; | ||
} | ||
|
||
/** | ||
* Collector Trace Exporter for Web | ||
*/ | ||
export class CollectorTraceExporter extends CollectorTraceExporterBase< | ||
CollectorExporterConfigBrowser | ||
> { | ||
DEFAULT_HEADERS: { [key: string]: string } = { | ||
[collectorTypes.OT_REQUEST_HEADER]: '1', | ||
}; | ||
private _headers: { [key: string]: string }; | ||
private _useXHR: boolean = false; | ||
const DEFAULT_COLLECTOR_URL = 'http://localhost:55678/v1/trace'; | ||
|
||
/** | ||
* @param config | ||
*/ | ||
constructor(config: CollectorExporterConfigBrowser = {}) { | ||
super(config); | ||
this._headers = config.headers || this.DEFAULT_HEADERS; | ||
this._useXHR = | ||
!!config.headers || typeof navigator.sendBeacon !== 'function'; | ||
} | ||
/** | ||
* Collector Exporter for Web | ||
*/ | ||
export class CollectorExporter extends CollectorExporterBase< | ||
CollectorExporterConfig | ||
> { | ||
DEFAULT_HEADERS: { [key: string]: string } = { | ||
[collectorTypes.OT_REQUEST_HEADER]: '1', | ||
}; | ||
private _headers: { [key: string]: string }; | ||
private _useXHR: boolean = false; | ||
|
||
onInit(): void { | ||
window.addEventListener('unload', this.shutdown); | ||
} | ||
/** | ||
* @param config | ||
*/ | ||
constructor(config: CollectorExporterConfig = {}) { | ||
super(config); | ||
this._headers = config.headers || this.DEFAULT_HEADERS; | ||
this._useXHR = | ||
!!config.headers || typeof navigator.sendBeacon !== 'function'; | ||
} | ||
|
||
onShutdown(): void { | ||
window.removeEventListener('unload', this.shutdown); | ||
} | ||
onInit(): void { | ||
window.addEventListener('unload', this.shutdown); | ||
} | ||
|
||
getDefaultUrl(url: string | undefined) { | ||
return url || DEFAULT_COLLECTOR_URL; | ||
} | ||
onShutdown(): void { | ||
window.removeEventListener('unload', this.shutdown); | ||
} | ||
|
||
sendSpans( | ||
spans: ReadableSpan[], | ||
onSuccess: () => void, | ||
onError: (error: collectorTypes.CollectorExporterError) => void | ||
) { | ||
const exportTraceServiceRequest = toCollectorExportTraceServiceRequest( | ||
spans, | ||
this | ||
); | ||
const body = JSON.stringify(exportTraceServiceRequest); | ||
getDefaultUrl(url: string | undefined) { | ||
return url || DEFAULT_COLLECTOR_URL; | ||
} | ||
|
||
if (this._useXHR) { | ||
sendWithXhr( | ||
body, | ||
this.url, | ||
this._headers, | ||
this.logger, | ||
onSuccess, | ||
onError | ||
); | ||
} else { | ||
sendWithBeacon(body, this.url, this.logger, onSuccess, onError); | ||
} | ||
} | ||
} | ||
sendSpans( | ||
spans: ReadableSpan[], | ||
onSuccess: () => void, | ||
onError: (error: collectorTypes.CollectorExporterError) => void | ||
) { | ||
const exportTraceServiceRequest = toCollectorExportTraceServiceRequest( | ||
spans, | ||
this | ||
); | ||
const body = JSON.stringify(exportTraceServiceRequest); | ||
|
||
if (this._useXHR) { | ||
this._sendSpansWithXhr(body, onSuccess, onError); | ||
} else { | ||
this._sendSpansWithBeacon(body, onSuccess, onError); | ||
} | ||
} | ||
|
||
/** | ||
* send spans using browser navigator.sendBeacon | ||
* @param body | ||
* @param onSuccess | ||
* @param onError | ||
*/ | ||
private _sendSpansWithBeacon( | ||
body: string, | ||
onSuccess: () => void, | ||
onError: (error: collectorTypes.CollectorExporterError) => void | ||
) { | ||
if (navigator.sendBeacon(this.url, body)) { | ||
this.logger.debug('sendBeacon - can send', body); | ||
onSuccess(); | ||
} else { | ||
this.logger.error('sendBeacon - cannot send', body); | ||
onError({}); | ||
} | ||
} | ||
|
||
/** | ||
* function to send spans using browser XMLHttpRequest | ||
* used when navigator.sendBeacon is not available | ||
* @param body | ||
* @param onSuccess | ||
* @param onError | ||
*/ | ||
private _sendSpansWithXhr( | ||
body: string, | ||
onSuccess: () => void, | ||
onError: (error: collectorTypes.CollectorExporterError) => void | ||
) { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('POST', this.url); | ||
xhr.setRequestHeader('Accept', 'application/json'); | ||
xhr.setRequestHeader('Content-Type', 'application/json'); | ||
Object.entries(this._headers).forEach(([k, v]) => { | ||
xhr.setRequestHeader(k, v); | ||
}); | ||
|
||
xhr.send(body); | ||
|
||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState === XMLHttpRequest.DONE) { | ||
if (xhr.status >= 200 && xhr.status <= 299) { | ||
this.logger.debug('xhr success', body); | ||
onSuccess(); | ||
} else { | ||
this.logger.error('body', body); | ||
this.logger.error('xhr error', xhr); | ||
onError({ | ||
code: xhr.status, | ||
message: xhr.responseText, | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
} |