-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add getTraceMetaTags
function
#13201
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e8d71aa
feat(core): Add `getTracingMetaTags` function
Lms24 95fd6b1
biome...
Lms24 478c23b
re-export from framework sdks
Lms24 1fcd07b
s/getTracingMetaTags/getTraceMetaTags
Lms24 93b9ef0
Update packages/core/src/utils/meta.ts
Lms24 54c1cac
add missing export in astro
Lms24 f6b757f
fix ts 3.8 test pls?
Lms24 a6d7a69
maybe now?
Lms24 0b44b9d
remove ts-expect-error
Lms24 44cadee
use ts-ignore
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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://[email protected]/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); |
25 changes: 25 additions & 0 deletions
25
dev-packages/node-integration-tests/suites/tracing/meta-tags/test.ts
This file contains hidden or 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,25 @@ | ||
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', | ||
}); | ||
|
||
// @ts-expect-error - this is a string, types just don't work well | ||
const html = response?.response 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 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> | ||
* `; | ||
* } | ||
* ``` | ||
* | ||
* @returns | ||
*/ | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.