-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add
getTraceMetaTags
function (#13201)
Export function `getTraceMetaTags` that gives users an easy way to get stringified Html meta tags for server->client trace propagation. --------- Co-authored-by: Andrei <168741329+andreiborza@users.noreply.github.com>
- Loading branch information
1 parent
64d80dd
commit 05684d4
Showing
16 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
dev-packages/node-integration-tests/suites/tracing/meta-tags/server.js
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// express must be required after Sentry is initialized | ||
const express = require('express'); | ||
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); | ||
|
||
const app = express(); | ||
|
||
app.get('/test', (_req, res) => { | ||
res.send({ | ||
response: ` | ||
<html> | ||
<head> | ||
${Sentry.getTraceMetaTags()} | ||
</head> | ||
<body> | ||
Hi :) | ||
</body> | ||
</html> | ||
`, | ||
}); | ||
}); | ||
|
||
Sentry.setupExpressErrorHandler(app); | ||
|
||
startExpressServerAndSendPortToRunner(app); |
26 changes: 26 additions & 0 deletions
26
dev-packages/node-integration-tests/suites/tracing/meta-tags/test.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('getTraceMetaTags', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('injects sentry tracing <meta> tags', async () => { | ||
const traceId = 'cd7ee7a6fe3ebe7ab9c3271559bc203c'; | ||
const parentSpanId = '100ff0980e7a4ead'; | ||
|
||
const runner = createRunner(__dirname, 'server.js').start(); | ||
|
||
const response = await runner.makeRequest('get', '/test', { | ||
'sentry-trace': `${traceId}-${parentSpanId}-1`, | ||
baggage: 'sentry-environment=production', | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const html = response?.response as unknown as string; | ||
|
||
expect(html).toMatch(/<meta name="sentry-trace" content="cd7ee7a6fe3ebe7ab9c3271559bc203c-[a-z0-9]{16}-1"\/>/); | ||
expect(html).toContain('<meta name="baggage" content="sentry-environment=production"/>'); | ||
}); | ||
}); |
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { Client, Scope, Span } from '@sentry/types'; | ||
import { getTraceData } from './traceData'; | ||
|
||
/** | ||
* Returns a string of meta tags that represent the current trace data. | ||
* | ||
* You can use this to propagate a trace from your server-side rendered Html to the browser. | ||
* This function returns up to two meta tags, `sentry-trace` and `baggage`, depending on the | ||
* current trace data state. | ||
* | ||
* @example | ||
* Usage example: | ||
* | ||
* ```js | ||
* function renderHtml() { | ||
* return ` | ||
* <head> | ||
* ${getTraceMetaTags()} | ||
* </head> | ||
* `; | ||
* } | ||
* ``` | ||
* | ||
*/ | ||
export function getTraceMetaTags(span?: Span, scope?: Scope, client?: Client): string { | ||
return Object.entries(getTraceData(span, scope, client)) | ||
.map(([key, value]) => `<meta name="${key}" content="${value}"/>`) | ||
.join('\n'); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getTraceMetaTags } from '../../../src/utils/meta'; | ||
import * as TraceDataModule from '../../../src/utils/traceData'; | ||
|
||
describe('getTraceMetaTags', () => { | ||
it('renders baggage and sentry-trace values to stringified Html meta tags', () => { | ||
jest.spyOn(TraceDataModule, 'getTraceData').mockReturnValueOnce({ | ||
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1', | ||
baggage: 'sentry-environment=production', | ||
}); | ||
|
||
expect(getTraceMetaTags()).toBe(`<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/> | ||
<meta name="baggage" content="sentry-environment=production"/>`); | ||
}); | ||
|
||
it('renders just sentry-trace values to stringified Html meta tags', () => { | ||
jest.spyOn(TraceDataModule, 'getTraceData').mockReturnValueOnce({ | ||
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1', | ||
}); | ||
|
||
expect(getTraceMetaTags()).toBe( | ||
'<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>', | ||
); | ||
}); | ||
|
||
it('returns an empty string if neither sentry-trace nor baggage values are available', () => { | ||
jest.spyOn(TraceDataModule, 'getTraceData').mockReturnValueOnce({}); | ||
|
||
expect(getTraceMetaTags()).toBe(''); | ||
}); | ||
}); |
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
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
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
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
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