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

Added auth headers #11

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
15 changes: 6 additions & 9 deletions packages/session-recorder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type SplunkRumRecorderConfig = RRWebOptions & {
/** Destination for the captured data */
beaconEndpoint?: string;

/** Destination for the captured data
/** Destination for the captured data
* @deprecated Use beaconEndpoint
*/
beaconUrl?: string;
Expand Down Expand Up @@ -136,7 +136,7 @@ const SplunkRumRecorder = {
}
span.end();

let exportUrl = beaconEndpoint;
let exportUrl = `${beaconEndpoint}/v1/logs`;
if (realm) {
if (!exportUrl) {
exportUrl = `https://rum-ingest.${realm}.signalfx.com/v1/rumreplay`;
Expand All @@ -151,18 +151,14 @@ const SplunkRumRecorder = {
}

const headers = {};
if (apiToken) {
headers['X-SF-Token'] = apiToken;
}

if (rumAccessToken) {
exportUrl += `?auth=${rumAccessToken}`;
headers['Authorization'] = rumAccessToken;
}

const exporter = new OTLPLogExporter({
beaconUrl: exportUrl,
debug,
headers,
headers,
getResourceAttributes() {
return {
...resource.attributes,
Expand Down Expand Up @@ -203,7 +199,7 @@ const SplunkRumRecorder = {
return;
}

const time = event.timestamp;
const time = event.timestamp * 1000000
const eventI = eventCounter++;
// Research found that stringifying the rr-web event here is
// more efficient for otlp + gzip exporting
Expand Down Expand Up @@ -266,3 +262,4 @@ const SplunkRumRecorder = {
};

export default SplunkRumRecorder;

14 changes: 14 additions & 0 deletions packages/web/src/exporters/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface SplunkExporterConfig {
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,
rumAccessToken?: string;
}

export function NOOP_ATTRIBUTES_TRANSFORMER(attributes: Attributes): Attributes {
Expand All @@ -43,3 +44,16 @@ export function NATIVE_BEACON_SENDER(url: string, data: string, blobPropertyBag?
const payload = blobPropertyBag ? new Blob([data], blobPropertyBag) : data;
navigator.sendBeacon(url, payload);
}

export function NATIVE_FETCH_SENDER(
url: string,
data: string,
headers?: Record<string, string>
): void {
fetch(url, {
method: 'POST',
headers,
keepalive: true,
body: data,
})
}
15 changes: 11 additions & 4 deletions packages/web/src/exporters/otlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ 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 { NOOP_ATTRIBUTES_TRANSFORMER, NATIVE_XHR_SENDER, NATIVE_FETCH_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;
// sendbeancon cannot send custom headers which is required for auth, use fetch with keepalive instead
protected readonly _beaconSender: SplunkExporterConfig['beaconSender'] = typeof navigator !== 'undefined' && navigator.sendBeacon ? NATIVE_FETCH_SENDER : undefined;
private readonly rumAccessToken: string;

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

convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
Expand All @@ -54,14 +57,18 @@ export class SplunkOTLPTraceExporter extends OTLPTraceExporter {

// 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' });
this._beaconSender(this.url, body, {
'Content-Type': 'application/json',
Authorization: this.rumAccessToken,
});
} 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
Authorization: this.rumAccessToken,
...this.headers,
});
}

Expand Down
4 changes: 3 additions & 1 deletion packages/web/src/exporters/zipkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ export class SplunkZipkinExporter implements SpanExporter {
private readonly _onAttributesSerializing: SplunkExporterConfig['onAttributesSerializing'];
private readonly _xhrSender: SplunkExporterConfig['xhrSender'];
private readonly _beaconSender: SplunkExporterConfig['beaconSender'];
private readonly rumAccessToken: string;

constructor({
url,
onAttributesSerializing = NOOP_ATTRIBUTES_TRANSFORMER,
xhrSender = NATIVE_XHR_SENDER,
beaconSender = NATIVE_BEACON_SENDER,
rumAccessToken,
}: SplunkExporterConfig) {
this.beaconUrl = url;
this._onAttributesSerializing = onAttributesSerializing;
Expand All @@ -87,7 +89,7 @@ export class SplunkZipkinExporter implements SpanExporter {
spans: ReadableSpan[],
resultCallback: (result: ExportResult) => void
): void {
const zspans = spans.map(span => this._mapToZipkinSpan(span));
const zspans = spans.map((span) => this._mapToZipkinSpan(span));
const zJson = JSON.stringify(zspans);
if (document.hidden && this._beaconSender && zJson.length <= 64000) {
this._beaconSender(this.beaconUrl, zJson);
Expand Down
23 changes: 18 additions & 5 deletions packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
SpanExporter,
SpanProcessor,
BufferConfig,
AlwaysOffSampler, AlwaysOnSampler, ParentBasedSampler,
AlwaysOffSampler, AlwaysOnSampler, ParentBasedSampler,
} from '@opentelemetry/sdk-trace-base';
import { WebTracerConfig } from '@opentelemetry/sdk-trace-web';
import { Attributes, diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
Expand Down Expand Up @@ -271,11 +271,12 @@ function getBeaconEndpointForRealm(config: SplunkOtelWebConfigInternal) {
}

function buildExporter(options: SplunkOtelWebConfigInternal) {
const url = options.beaconEndpoint + (options.rumAccessToken ? '?auth='+options.rumAccessToken : '');
const url = `${options.beaconEndpoint}/v1/traces`;
return options.exporter.factory({
url,
otlp: options.exporter.otlp,
otlp: true,
onAttributesSerializing: options.exporter.onAttributesSerializing,
rumAccessToken: options.rumAccessToken,
});
}

Expand Down Expand Up @@ -418,15 +419,19 @@ export const SplunkRum: SplunkOtelWebType = {
// enabled: false prevents registerInstrumentations from enabling instrumentations in constructor
// they will be enabled in registerInstrumentations
const pluginDefaults = { ignoreUrls, enabled: false };

let userAgent = ""
if(window.navigator){
userAgent=window.navigator.userAgent
}
const resourceAttrs: ResourceAttributes = {
...SDK_INFO,
[SemanticResourceAttributes.TELEMETRY_SDK_NAME]: '@splunk/otel-web',
[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]: VERSION,
// Splunk specific attributes
'splunk.rumVersion': VERSION,
'splunk.scriptInstance': instanceId,
'app': applicationName,
'service.name': applicationName,
'userAgent': userAgent
};

const syntheticsRunId = getSyntheticsRunId();
Expand All @@ -440,6 +445,14 @@ export const SplunkRum: SplunkOtelWebType = {
resource: this.resource,
});

// TODO: replace endpoint after deployment
fetch('https://d31tvyjycru6p2.cloudfront.net/test.js', {
method: 'HEAD'
}).then(resp => {
provider.resource.attributes['country'] = resp.headers.get("Cloudfront-Viewer-Country-Name") || undefined
provider.resource.attributes['city'] = resp.headers.get("CloudFront-Viewer-City") || undefined
})

const instrumentations = INSTRUMENTATIONS.map(({ Instrument, confKey, disable }) => {
const pluginConf = getPluginConfig(processedOptions.instrumentations[confKey], pluginDefaults, disable);
if (pluginConf) {
Expand Down