-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgetRequestSpanData.ts
59 lines (50 loc) · 1.63 KB
/
getRequestSpanData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Span } from '@opentelemetry/api';
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import {
ATTR_HTTP_REQUEST_METHOD,
ATTR_URL_FULL,
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_URL,
} from '@opentelemetry/semantic-conventions';
import { getSanitizedUrlString, parseUrl } from '@sentry/core';
import type { SanitizedRequestData } from '@sentry/core';
import { spanHasAttributes } from './spanTypes';
/**
* Get sanitizied request data from an OTEL span.
*/
export function getRequestSpanData(span: Span | ReadableSpan): Partial<SanitizedRequestData> {
// The base `Span` type has no `attributes`, so we need to guard here against that
if (!spanHasAttributes(span)) {
return {};
}
// eslint-disable-next-line deprecation/deprecation
const maybeUrlAttribute = (span.attributes[ATTR_URL_FULL] || span.attributes[SEMATTRS_HTTP_URL]) as
| string
| undefined;
const data: Partial<SanitizedRequestData> = {
url: maybeUrlAttribute,
// eslint-disable-next-line deprecation/deprecation
'http.method': (span.attributes[ATTR_HTTP_REQUEST_METHOD] || span.attributes[SEMATTRS_HTTP_METHOD]) as
| string
| undefined,
};
// Default to GET if URL is set but method is not
if (!data['http.method'] && data.url) {
data['http.method'] = 'GET';
}
try {
if (typeof maybeUrlAttribute === 'string') {
const url = parseUrl(maybeUrlAttribute);
data.url = getSanitizedUrlString(url);
if (url.search) {
data['http.query'] = url.search;
}
if (url.hash) {
data['http.fragment'] = url.hash;
}
}
} catch {
// ignore
}
return data;
}