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

feat(core): Return array or string from getTraceMetaTags() #13293

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/astro/src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function addMetaTagToHead(htmlChunk: string, scope: Scope, client: Client, span?
if (typeof htmlChunk !== 'string') {
return htmlChunk;
}
const metaTags = getTraceMetaTags(span, scope, client);
const metaTags = getTraceMetaTags({ span, scope, client });

if (!metaTags) {
return htmlChunk;
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/utils/meta.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Client, Scope, Span } from '@sentry/types';
import { getTraceData } from './traceData';

// Function overloads
export function getTraceMetaTags(params?: { span?: Span; scope?: Scope; client?: Client; asArray: true }): string[];
export function getTraceMetaTags(params?: { span?: Span; scope?: Scope; client?: Client; asArray?: false }): string;
/**
* Returns a string of meta tags that represent the current trace data.
* Returns a string or 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
Expand All @@ -22,8 +25,15 @@ import { getTraceData } from './traceData';
* ```
*
*/
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');
export function getTraceMetaTags({
span,
scope,
client,
asArray,
}: { span?: Span; scope?: Scope; client?: Client; asArray?: boolean } = {}): string | string[] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we move forward with this, we can adjust the types so that TS knows that if asArray is true, it returns an array and otherwise a string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type checking should already check this as I created two function overloads above. But I am open for another approach (in the case this gets merged) :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I missed this. This is what I had in mind :)

const traceTags = Object.entries(getTraceData(span, scope, client)).map(
([key, value]) => `<meta name="${key}" content="${value}"/>`,
);

return asArray ? traceTags : traceTags.join('\n');
}
14 changes: 14 additions & 0 deletions packages/core/test/lib/utils/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ describe('getTraceMetaTags', () => {
<meta name="baggage" content="sentry-environment=production"/>`);
});

it('returns baggage and sentry-trace values to stringified Html meta tag array', () => {
jest.spyOn(TraceDataModule, 'getTraceData').mockReturnValueOnce({
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
baggage: 'sentry-environment=production',
});

const metaTags = getTraceMetaTags({ asArray: true });

expect(metaTags).toContain('<meta name="baggage" content="sentry-environment=production"/>');
expect(metaTags).toContain(
'<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>',
);
});

it('renders just sentry-trace values to stringified Html meta tags', () => {
jest.spyOn(TraceDataModule, 'getTraceData').mockReturnValueOnce({
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
Expand Down
Loading