Skip to content

Fix #9 - undefined navigator will not default to "Unknown" #11

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 1 commit into from
Jan 3, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tricky-kids-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"workers-tracing": patch
---

Fixed #9 - if `navigator` is not defined (old compat date) it will now default to "Unknown" for the runtime name.
5 changes: 4 additions & 1 deletion src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export function getDefaultAttributes(opts: TracerOptions): Attributes {
[ATTRIBUTE_NAME.SDK_NAME]: 'workers-tracing',
[ATTRIBUTE_NAME.SDK_LANG]: 'javascript',
[ATTRIBUTE_NAME.SDK_VERSION]: '__VERSION__',
[ATTRIBUTE_NAME.RUNTIME_NAME]: navigator.userAgent, // Cloudflare-Workers
[ATTRIBUTE_NAME.RUNTIME_NAME]:
typeof navigator !== 'undefined' && navigator.userAgent // Cloudflare-Workers
? navigator.userAgent
: 'Unknown',
};
}

Expand Down
80 changes: 80 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,84 @@ describe('API', () => {
});
});
});

describe('Default attributes', () => {
test('Attributes are all set', async () => {
devWorker = await startWorker('test/scripts/api/root-span.ts');

const res = await devWorker.fetch('http://worker/test');

expect(res.status).toBe(200);
expect(res.headers.get('x-trace-id')).not.toBeNull();

const traceId = res.headers.get('x-trace-id');
if (traceId === null) {
expect(traceId).not.toBeNull();
return;
}
const trace = await getTrace(collectorWorker, traceId);

expect(trace.resourceSpans.length).toBe(1);
const resourceSpan = trace.resourceSpans[0];
const resource = resourceSpan.resource;

// Validate default attributes
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SERVICE_NAME),
).toStrictEqual({ key: ATTRIBUTE_NAME.SERVICE_NAME, value: { stringValue: 'root-span' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SDK_NAME),
).toStrictEqual({ key: ATTRIBUTE_NAME.SDK_NAME, value: { stringValue: 'workers-tracing' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SDK_LANG),
).toStrictEqual({ key: ATTRIBUTE_NAME.SDK_LANG, value: { stringValue: 'javascript' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SDK_VERSION),
).toStrictEqual({ key: ATTRIBUTE_NAME.SDK_VERSION, value: { stringValue: '__VERSION__' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.RUNTIME_NAME),
).toStrictEqual({ key: ATTRIBUTE_NAME.RUNTIME_NAME, value: { stringValue: 'Cloudflare-Workers' } });
});

test('Attributes work with no compat date', async () => {
devWorker = await startWorker('test/scripts/api/root-span.ts', {
// Set compat date to September 2021, this is the earliest compat date possible
// and what it will default to if none is provided
compatibilityDate: '2021-09-14',
});

const res = await devWorker.fetch('http://worker/test');

expect(res.status).toBe(200);
expect(res.headers.get('x-trace-id')).not.toBeNull();

const traceId = res.headers.get('x-trace-id');
if (traceId === null) {
expect(traceId).not.toBeNull();
return;
}
const trace = await getTrace(collectorWorker, traceId);

expect(trace.resourceSpans.length).toBe(1);
const resourceSpan = trace.resourceSpans[0];
const resource = resourceSpan.resource;

// Validate default attributes
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SERVICE_NAME),
).toStrictEqual({ key: ATTRIBUTE_NAME.SERVICE_NAME, value: { stringValue: 'root-span' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SDK_NAME),
).toStrictEqual({ key: ATTRIBUTE_NAME.SDK_NAME, value: { stringValue: 'workers-tracing' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SDK_LANG),
).toStrictEqual({ key: ATTRIBUTE_NAME.SDK_LANG, value: { stringValue: 'javascript' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.SDK_VERSION),
).toStrictEqual({ key: ATTRIBUTE_NAME.SDK_VERSION, value: { stringValue: '__VERSION__' } });
expect(
resource.attributes.find((attribute) => attribute.key === ATTRIBUTE_NAME.RUNTIME_NAME),
).toStrictEqual({ key: ATTRIBUTE_NAME.RUNTIME_NAME, value: { stringValue: 'Unknown' } });
});
});
});