Skip to content

Commit

Permalink
New APIs to retrieve tracing headers and generate IDs
Browse files Browse the repository at this point in the history
* Removed generateUUID
* Added generateTraceId()
* Added generateSpanId()
* Added getTracingHeaders()
* Added injectTracingHeaders()
* Added buildTracingHeadersInjector()
  • Loading branch information
marco-saia-datadog committed Feb 10, 2025
1 parent 0a877a0 commit ce833f6
Show file tree
Hide file tree
Showing 9 changed files with 706 additions and 67 deletions.
10 changes: 8 additions & 2 deletions packages/core/jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ module.exports = {
stopResource: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
generateUUID: jest.fn().mockImplementation(() => 'fakeUUID'),
addError: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
Expand All @@ -117,7 +116,14 @@ module.exports = {
() => new Promise(resolve => resolve('test-session-id'))
),
setTimeProvider: jest.fn().mockImplementation(() => {}),
timeProvider: jest.fn().mockReturnValue(undefined)
timeProvider: jest.fn().mockReturnValue(undefined),
getTracingHeaders: jest.fn().mockReturnValue([]),
injectTracingHeaders: jest.fn().mockImplementation(() => {}),
buildTracingHeadersInjector: jest.fn().mockReturnValue({
inject: (url, injectHeaders) => {}
}),
generateTraceId: jest.fn().mockReturnValue('mock-trace-id'),
generateSpanId: jest.fn().mockReturnValue('mock-span-id')
},

DatadogProvider: DatadogProviderMock
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ import {
DATADOG_GRAPH_QL_OPERATION_NAME_HEADER,
DATADOG_GRAPH_QL_VARIABLES_HEADER
} from './rum/instrumentation/resourceTracking/graphql/graphqlHeaders';
import type { TracingHeadersInjector } from './rum/types';
import { RumActionType, ErrorSource, PropagatorType } from './rum/types';
import { DatadogProvider } from './sdk/DatadogProvider/DatadogProvider';
import { FileBasedConfiguration } from './sdk/FileBasedConfiguration/FileBasedConfiguration';
import { DdTrace } from './trace/DdTrace';
import { DefaultTimeProvider } from './utils/time-provider/DefaultTimeProvider';
import { TimeProvider } from './utils/time-provider/TimeProvider';
import type { Timestamp } from './utils/time-provider/TimeProvider';
import { TracingIdType } from './rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import {
TracingIdType,
TracingIdFormat
} from './rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import { DatadogTracingIdentifier } from './rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier';

/* eslint-enable arca/import-ordering */

Expand Down Expand Up @@ -66,7 +71,9 @@ export {
DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
DATADOG_GRAPH_QL_OPERATION_NAME_HEADER,
DATADOG_GRAPH_QL_VARIABLES_HEADER,
TracingIdType
TracingIdType,
TracingIdFormat,
DatadogTracingIdentifier
};

export type { Timestamp };
export type { Timestamp, TracingHeadersInjector };
88 changes: 59 additions & 29 deletions packages/core/src/rum/DdRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider'
import type { TimeProvider } from '../utils/time-provider/TimeProvider';

import { DdAttributes } from './DdAttributes';
import type { ActionEventMapper } from './eventMappers/actionEventMapper';
import { generateActionEventMapper } from './eventMappers/actionEventMapper';
import type { ErrorEventMapper } from './eventMappers/errorEventMapper';
import type { ActionEventMapper } from './eventMappers/actionEventMapper';
import { generateErrorEventMapper } from './eventMappers/errorEventMapper';
import type { ResourceEventMapper } from './eventMappers/resourceEventMapper';
import type { ErrorEventMapper } from './eventMappers/errorEventMapper';
import { generateResourceEventMapper } from './eventMappers/resourceEventMapper';
import {
TracingIdFormat,
TracingIdType,
TracingIdentifier
} from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import type { ResourceEventMapper } from './eventMappers/resourceEventMapper';
import { DatadogTracingIdentifier } from './instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier';
import { TracingIdentifier } from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import { getTracingHeaders } from './instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders';
import type {
ErrorSource,
DdRumType,
RumActionType,
ResourceKind
ResourceKind,
FirstPartyHost,
TracingHeadersInjector
} from './types';

const generateEmptyPromise = () => new Promise<void>(resolve => resolve());
Expand Down Expand Up @@ -230,26 +230,6 @@ class DdRumWrapper implements DdRumType {
);
};

generateUUID = (type: TracingIdType): string => {
switch (type) {
case TracingIdType.trace:
return TracingIdentifier.createTraceId().toString(
TracingIdFormat.paddedHex
);
case TracingIdType.span:
return TracingIdentifier.createSpanId().toString(
TracingIdFormat.decimal
);
default:
console.warn(
`Unsupported tracing ID type '${type}' for generateUUID. Falling back to 64 bit Span ID.`
);
return TracingIdentifier.createSpanId().toString(
TracingIdFormat.decimal
);
}
};

addError = (
message: string,
source: ErrorSource,
Expand Down Expand Up @@ -320,6 +300,56 @@ class DdRumWrapper implements DdRumType {
return this.nativeRum.getCurrentSessionId();
}

getTracingHeaders = (
url: string,
tracingSamplingRate: number,
firstPartyHosts: FirstPartyHost[]
): { header: string; value: string }[] => {
return getTracingHeaders(url, tracingSamplingRate, firstPartyHosts);
};

injectTracingHeaders(
url: string,
tracingSamplingRate: number,
firstPartyHosts: FirstPartyHost[],
injectHeaders: (header: string, value: string) => void
) {
getTracingHeaders(url, tracingSamplingRate, firstPartyHosts).forEach(
({ header, value }) => {
injectHeaders(header, value);
}
);
}

buildTracingHeadersInjector(
tracingSamplingRate: number,
firstPartyHosts: FirstPartyHost[]
): TracingHeadersInjector {
const _firstPartyHosts = [...firstPartyHosts];
return {
inject: (
url: string,
injectHeaders: (header: string, value: string) => void
) => {
getTracingHeaders(
url,
tracingSamplingRate,
_firstPartyHosts
).forEach(({ header, value }) => {
injectHeaders(header, value);
});
}
};
}

generateTraceId(): DatadogTracingIdentifier {
return new DatadogTracingIdentifier(TracingIdentifier.createTraceId());
}

generateSpanId(): DatadogTracingIdentifier {
return new DatadogTracingIdentifier(TracingIdentifier.createSpanId());
}

registerErrorEventMapper(errorEventMapper: ErrorEventMapper) {
this.errorEventMapper = generateErrorEventMapper(errorEventMapper);
}
Expand Down
Loading

0 comments on commit ce833f6

Please sign in to comment.