Skip to content

Commit b681a47

Browse files
committed
add tests
1 parent d8f5d53 commit b681a47

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

packages/core/src/tracing/spanstatus.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ export function spanStatusfromHttpCode(httpStatus: number): SpanStatusType {
128128
* Additionally, the span's status is updated, depending on the http code.
129129
*/
130130
export function setHttpStatus(span: Span, httpStatus: number): void {
131-
span.setAttribute('http.status_code', String(httpStatus));
132-
span.setAttribute('http.response.status_code', httpStatus);
133-
134131
// TODO (v8): Remove these calls
132+
// Relay does not require us to send the status code as a tag
133+
// For now, just because users might expect it to land as a tag we keep sending it.
134+
// Same with data.
135+
// In v8, we replace both, simply with
136+
// span.setAttribute('http.response.status_code', httpStatus);
137+
135138
// eslint-disable-next-line deprecation/deprecation
136139
span.setTag('http.status_code', String(httpStatus));
137140
// eslint-disable-next-line deprecation/deprecation
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Span, setHttpStatus, spanToJSON } from '../../../src/index';
2+
3+
describe('setHttpStatus', () => {
4+
it.each([
5+
[200, 'ok'],
6+
[300, 'ok'],
7+
[401, 'unauthenticated'],
8+
[403, 'permission_denied'],
9+
[404, 'not_found'],
10+
[409, 'already_exists'],
11+
[413, 'failed_precondition'],
12+
[429, 'resource_exhausted'],
13+
[455, 'invalid_argument'],
14+
[501, 'unimplemented'],
15+
[503, 'unavailable'],
16+
[504, 'deadline_exceeded'],
17+
[520, 'internal_error'],
18+
])('applies the correct span status and http status code to the span (%s - $%s)', (code, status) => {
19+
const span = new Span({ name: 'test' });
20+
21+
setHttpStatus(span!, code);
22+
23+
const { status: spanStatus, data, tags } = spanToJSON(span!);
24+
25+
expect(spanStatus).toBe(status);
26+
expect(data).toMatchObject({ 'http.response.status_code': code });
27+
expect(tags).toMatchObject({ 'http.status_code': String(code) });
28+
});
29+
30+
it("doesn't set the status for an unknown http status code", () => {
31+
const span = new Span({ name: 'test' });
32+
33+
setHttpStatus(span!, 600);
34+
35+
const { status: spanStatus, data, tags } = spanToJSON(span!);
36+
37+
expect(spanStatus).toBeUndefined();
38+
expect(data).toMatchObject({ 'http.response.status_code': 600 });
39+
expect(tags).toMatchObject({ 'http.status_code': '600' });
40+
});
41+
});

0 commit comments

Comments
 (0)