File tree Expand file tree Collapse file tree 6 files changed +114
-0
lines changed
dev-packages/node-integration-tests/suites/tracing/meta-tags Expand file tree Collapse file tree 6 files changed +114
-0
lines changed Original file line number Diff line number Diff line change
1
+ const { loggingTransport } = require ( '@sentry-internal/node-integration-tests' ) ;
2
+ const Sentry = require ( '@sentry/node' ) ;
3
+
4
+ Sentry . init ( {
5
+ dsn : 'https://public@dsn.ingest.sentry.io/1337' ,
6
+ tracesSampleRate : 1.0 ,
7
+ transport : loggingTransport ,
8
+ } ) ;
9
+
10
+ // express must be required after Sentry is initialized
11
+ const express = require ( 'express' ) ;
12
+ const { startExpressServerAndSendPortToRunner } = require ( '@sentry-internal/node-integration-tests' ) ;
13
+
14
+ const app = express ( ) ;
15
+
16
+ app . get ( '/test' , ( _req , res ) => {
17
+ res . send ( {
18
+ response : `
19
+ <html>
20
+ <head>
21
+ ${ Sentry . getTracingMetaTags ( ) }
22
+ </head>
23
+ <body>
24
+ Hi :)
25
+ </body>
26
+ </html>
27
+ ` ,
28
+ } ) ;
29
+ } ) ;
30
+
31
+ Sentry . setupExpressErrorHandler ( app ) ;
32
+
33
+ startExpressServerAndSendPortToRunner ( app ) ;
Original file line number Diff line number Diff line change
1
+ import { cleanupChildProcesses , createRunner } from '../../../utils/runner' ;
2
+
3
+ describe ( 'getTracingMetaTags' , ( ) => {
4
+ afterAll ( ( ) => {
5
+ cleanupChildProcesses ( ) ;
6
+ } ) ;
7
+
8
+ test ( 'injects sentry tracing <meta> tags' , async ( ) => {
9
+ const traceId = 'cd7ee7a6fe3ebe7ab9c3271559bc203c' ;
10
+ const parentSpanId = '100ff0980e7a4ead' ;
11
+
12
+ const runner = createRunner ( __dirname , 'server.js' ) . start ( ) ;
13
+
14
+ const response = await runner . makeRequest ( 'get' , '/test' , {
15
+ 'sentry-trace' : `${ traceId } -${ parentSpanId } -1` ,
16
+ baggage : 'sentry-environment=production' ,
17
+ } ) ;
18
+
19
+ // @ts -expect-error - this is a string, types just don't work well
20
+ const html = response ?. response as string ;
21
+
22
+ expect ( html ) . toMatch ( / < m e t a n a m e = " s e n t r y - t r a c e " c o n t e n t = " c d 7 e e 7 a 6 f e 3 e b e 7 a b 9 c 3 2 7 1 5 5 9 b c 2 0 3 c - [ a - z 0 - 9 ] { 16 } - 1 " \/ > / ) ;
23
+ expect ( html ) . toContain ( '<meta name="baggage" content="sentry-environment=production"/>' ) ;
24
+ } ) ;
25
+ } ) ;
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ export {
83
83
export { parseSampleRate } from './utils/parseSampleRate' ;
84
84
export { applySdkMetadata } from './utils/sdkMetadata' ;
85
85
export { getTraceData } from './utils/traceData' ;
86
+ export { getTracingMetaTags } from './utils/meta' ;
86
87
export { DEFAULT_ENVIRONMENT } from './constants' ;
87
88
export { addBreadcrumb } from './breadcrumbs' ;
88
89
export { functionToStringIntegration } from './integrations/functiontostring' ;
Original file line number Diff line number Diff line change
1
+ import type { Client , Scope , Span } from '@sentry/types' ;
2
+ import { getTraceData } from './traceData' ; /**
3
+ * Returns a string of meta tags that represent the tracing data.
4
+ *
5
+ * You can use this to propagate a trace from your server-side rendered Html to the browser.
6
+ * Usage example:
7
+ *
8
+ * ```js
9
+ * function renderHtml() {
10
+ * return `
11
+ * <head>
12
+ * ${getTracingMetaTags()}
13
+ * </head>
14
+ * `;
15
+ * }
16
+ * ```
17
+ *
18
+ * @returns
19
+ */
20
+ export function getTracingMetaTags ( span ?: Span , scope ?: Scope , client ?: Client ) : string {
21
+ return Object . entries ( getTraceData ( span , scope , client ) )
22
+ . map ( ( [ key , value ] ) => `<meta name="${ key } " content="${ value } "/>` )
23
+ . join ( '\n' ) ;
24
+ }
Original file line number Diff line number Diff line change
1
+ import { getTracingMetaTags } from '../../../src/utils/meta' ;
2
+ import * as TraceDataModule from '../../../src/utils/traceData' ;
3
+
4
+ describe ( 'getTracingMetaTags' , ( ) => {
5
+ it ( 'renders baggage and sentry-trace values to stringified Html meta tags' , ( ) => {
6
+ jest . spyOn ( TraceDataModule , 'getTraceData' ) . mockReturnValueOnce ( {
7
+ 'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
8
+ baggage : 'sentry-environment=production' ,
9
+ } ) ;
10
+
11
+ expect ( getTracingMetaTags ( ) ) . toBe ( `<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>
12
+ <meta name="baggage" content="sentry-environment=production"/>` ) ;
13
+ } ) ;
14
+
15
+ it ( 'renders just sentry-trace values to stringified Html meta tags' , ( ) => {
16
+ jest . spyOn ( TraceDataModule , 'getTraceData' ) . mockReturnValueOnce ( {
17
+ 'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
18
+ } ) ;
19
+
20
+ expect ( getTracingMetaTags ( ) ) . toBe (
21
+ '<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>' ,
22
+ ) ;
23
+ } ) ;
24
+
25
+ it ( 'returns an empty string if neither sentry-trace nor baggage values are available' , ( ) => {
26
+ jest . spyOn ( TraceDataModule , 'getTraceData' ) . mockReturnValueOnce ( { } ) ;
27
+
28
+ expect ( getTracingMetaTags ( ) ) . toBe ( '' ) ;
29
+ } ) ;
30
+ } ) ;
Original file line number Diff line number Diff line change @@ -96,6 +96,7 @@ export {
96
96
getCurrentScope ,
97
97
getIsolationScope ,
98
98
getTraceData ,
99
+ getTracingMetaTags ,
99
100
withScope ,
100
101
withIsolationScope ,
101
102
captureException ,
You can’t perform that action at this time.
0 commit comments