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

[RUM-7629] New Tracing Headers APIs #793

Merged

Conversation

marco-saia-datadog
Copy link
Member

@marco-saia-datadog marco-saia-datadog commented Feb 5, 2025

What does this PR do?

Introduces new APIs to retrieve the tracing context for different propagator types, and for manually generating Trace IDs and Span IDs.

Tracing Context APIs

A new method has been introduced in this PR, for exposing Tracing Headers and RUM Resource Context attributes:

    /**
     * Gets the tracing context for the given url, tracingSamplingRate and firstPartyHosts.
     * The returned {@link DatadogTracingContext} can be used to retrieve the tracing headers
     * to append to your network request, and the attributes to add to your RUM Resource.
     * @param url the request URL.
     * @param tracingSamplingRate Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.
     * @param firstPartyHosts List of your backends hosts with propagator types.
     */
    getTracingContext(
        url: string,
        tracingSamplingRate: number,
        firstPartyHosts: FirstPartyHost[]
    ): DatadogTracingContext;

(1) Example usage with custom network library (e.g SSL pinning)

// SSL pinning
import {fetch} from 'react-native-ssl-pinning';

const url = "http://example.com/api/getPosts"

const tracingContext = DdRum.getTracingContext(url, samplingRate, firstPartyHosts);

fetch(url, {
	method: "POST" ,
	timeoutInterval: communication_timeout,
	body: body,
	sslPinning: {
		certs: ["cert1","cert2"]
	},
	headers: {
		'Accept': "application/json; charset=utf-8", "Access-Control-Allow-Origin": "*", "e_platform": "mobile",
		...tracingContext.getHeadersForRequest()
	}
});

await DdRum.startResource("My API call", "POST", url, tracingContext.getRumResourceContext());

(2) Example usage with custom network library (e.g SSL pinning) - using injector

// SSL pinning
import {fetch} from 'react-native-ssl-pinning';

const url = "http://example.com/api/getPosts"
const headers = {
     'Accept': "application/json; charset=utf-8", "Access-Control-Allow-Origin": "*", "e_platform": "mobile" 
}

const tracingContext = DdRum.getTracingContext(url, samplingRate, firstPartyHosts);
tracingContext.injectHeadersForRequest((header: string, value: string) => {
    headers[header] = value;
})


fetch(url, {
	method: "POST" ,
	timeoutInterval: communication_timeout,
	body: body,
	sslPinning: {
		certs: ["cert1","cert2"]
	},
	headers: headers
});

const resourceContext = { ...myContext }
tracingContext.injectRumResourceContext((attribute: string, value: string) => {
    resourceContext[attribute] = value;
});

await DdRum.startResource("My API call", "POST", url, resourceContext);

(3) Example utility method with custom network library (e.g SSL pinning)

const fetchWithSslPinning = (
    resourceName: string, 
    resourceContext: any, 
    url: string, 
    body: any, 
    method: string, 
    headers: Record<string, string | number>
) => {
	const tracingContext = DdRum.getTracingContext(url, DD_CONFIGURATION.samplingRate, DD_CONFIGURATION.firstPartyHosts);

	fetch(url, {
		method: method,
		timeoutInterval: communication_timeout,
		body: body,
		sslPinning: {
			certs: ["cert1","cert2"]
		},
		headers: {
			...headers,
			...tracingContext.getHeadersForRequest()
		}
	});

	await DdRum.startResource(resourceName, method, url, {...context, tracingContext.getResourceContext()});
}

Trace IDs and Span IDs APIs

/**
 * Generates a unique 128bit Trace ID.
 */
generateTraceId(): DatadogTracingIdentifier;

/**
 * Generates a unique 128bit Span ID.
 */
generateSpanId(): DatadogTracingIdentifier;

Example Usage

const traceId = DdRum.generateTraceId();

// Get the high 64 bit part as padded hex
const traceIdStr = traceId.toString(TracingIdFormat.paddedHighHex)

Additional Notes

This PR is a rework on the original #784 from @jhssilva, by providing a helper class that encapsulates the tracing identifiers and exposes different utility methods to convert it to the desired string representations, and by adding methods to directly retrieve the headers for an easy and out of the box usage.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)
  • If this PR is auto-generated, please make sure also to manually update the code related to the change

@marco-saia-datadog marco-saia-datadog requested a review from a team as a code owner February 5, 2025 15:46
example/src/screens/MainScreen.tsx Outdated Show resolved Hide resolved
example/src/screens/MainScreen.tsx Outdated Show resolved Hide resolved
packages/core/src/rum/DdRum.ts Outdated Show resolved Hide resolved
example/src/screens/MainScreen.tsx Outdated Show resolved Hide resolved
example/src/screens/MainScreen.tsx Outdated Show resolved Hide resolved
example/src/screens/MainScreen.tsx Outdated Show resolved Hide resolved
example/src/screens/MainScreen.tsx Outdated Show resolved Hide resolved
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Feb 5, 2025

Datadog Report

Branch report: marcosaia/RUM-7629/improve-generate-uuid-api
Commit report: 8436efc
Test service: dd-sdk-reactnative

✅ 0 Failed, 640 Passed, 1 Skipped, 11.89s Total Time

packages/core/src/rum/DdRum.ts Outdated Show resolved Hide resolved
packages/core/src/rum/__tests__/DdRum.test.ts Outdated Show resolved Hide resolved
packages/core/src/rum/__tests__/DdRum.test.ts Outdated Show resolved Hide resolved
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-7629/improve-generate-uuid-api branch from 883d5a2 to 5744256 Compare February 10, 2025 11:07
@marco-saia-datadog marco-saia-datadog changed the title [RUM-7629] Extend and improve generateUUID method [RUM-7629] New Tracing Headers APIs Feb 10, 2025
packages/core/jest/mock.js Outdated Show resolved Hide resolved
packages/core/jest/mock.js Outdated Show resolved Hide resolved
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-7629/improve-generate-uuid-api branch from 5744256 to ce833f6 Compare February 10, 2025 11:17
* Removed generateUUID
* Added generateTraceId()
* Added generateSpanId()
* Added getTracingHeaders()
* Added injectTracingHeaders()
* Added buildTracingHeadersInjector()
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-7629/improve-generate-uuid-api branch from ce833f6 to 17229bf Compare February 10, 2025 11:26
@marco-saia-datadog marco-saia-datadog requested review from 0xnm and a team February 10, 2025 11:27
@marco-saia-datadog
Copy link
Member Author

@0xnm I have done a big refactoring and opted for a completely different approach. I have described the changes in the PR notes, let me know if you have any questions :)

Thank you for your feedback and your help with this PR!

@jhssilva
Copy link
Contributor

Hey @marco-saia-datadog . Great work.
I've a few questions regarding this:

  • How does a user is aware of the difference between the string required for the resource and for the trace?
  • Does the injector will inject as well in the resources? Even if it's a react-native-ssl-pinning library?
  • How does a user knows the difference between lowPaddex and highPaddex in the trace context propagation? And how is aware of this in the context of Datadog trace context propagation style?

@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-7629/improve-generate-uuid-api branch from 8436efc to 9638b36 Compare February 10, 2025 16:47
plousada
plousada previously approved these changes Feb 12, 2025
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-7629/improve-generate-uuid-api branch from 320b900 to ccce42b Compare February 17, 2025 08:22
@marco-saia-datadog marco-saia-datadog merged commit 2635371 into develop Feb 17, 2025
7 of 9 checks passed
@marco-saia-datadog marco-saia-datadog deleted the marcosaia/RUM-7629/improve-generate-uuid-api branch February 17, 2025 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants