- Notifications
You must be signed in to change notification settings - Fork 881
/
Copy pathHttpTraceContext.ts
104 lines (92 loc) · 3.32 KB
/
HttpTraceContext.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import{SpanContext,HttpTextFormat,TraceFlags}from'@opentelemetry/types';
import{TraceState}from'../../trace/TraceState';
exportconstTRACE_PARENT_HEADER='traceparent';
exportconstTRACE_STATE_HEADER='tracestate';
constVALID_TRACE_PARENT_REGEX=/^[\da-f]{2}-[\da-f]{32}-[\da-f]{16}-[\da-f]{2}$/;
constVALID_TRACEID_REGEX=/^[0-9a-f]{32}$/i;
constVALID_SPANID_REGEX=/^[0-9a-f]{16}$/i;
constINVALID_ID_REGEX=/^0+$/i;
constVERSION='00';
functionparse(traceParent: string): SpanContext|null{
constmatch=traceParent.match(VALID_TRACE_PARENT_REGEX);
if(!match)returnnull;
constparts=traceParent.split('-');
const[version,traceId,spanId,option]=parts;
// tslint:disable-next-line:ban Needed to parse hexadecimal.
consttraceFlags=parseInt(option,16);
if(
!isValidVersion(version)||
!isValidTraceId(traceId)||
!isValidSpanId(spanId)
){
returnnull;
}
return{ traceId, spanId, traceFlags };
}
functionisValidVersion(version: string): boolean{
returnversion===VERSION;
}
functionisValidTraceId(traceId: string): boolean{
returnVALID_TRACEID_REGEX.test(traceId)&&!INVALID_ID_REGEX.test(traceId);
}
functionisValidSpanId(spanId: string): boolean{
returnVALID_SPANID_REGEX.test(spanId)&&!INVALID_ID_REGEX.test(spanId);
}
/**
* Propagates {@link SpanContext} through Trace Context format propagation.
*
* Based on the Trace Context specification:
* https://www.w3.org/TR/trace-context/
*/
exportclassHttpTraceContextimplementsHttpTextFormat{
inject(
spanContext: SpanContext,
format: string,
carrier: {[key: string]: unknown}
){
consttraceParent=`${VERSION}-${spanContext.traceId}-${
spanContext.spanId
}-0${Number(spanContext.traceFlags||TraceFlags.UNSAMPLED).toString(16)}`;
carrier[TRACE_PARENT_HEADER]=traceParent;
if(spanContext.traceState){
carrier[TRACE_STATE_HEADER]=spanContext.traceState.serialize();
}
}
extract(
format: string,
carrier: {[key: string]: unknown}
): SpanContext|null{
consttraceParentHeader=carrier[TRACE_PARENT_HEADER];
if(!traceParentHeader)returnnull;
consttraceParent=Array.isArray(traceParentHeader)
? traceParentHeader[0]
: traceParentHeader;
constspanContext=parse(traceParent);
if(!spanContext)returnnull;
consttraceStateHeader=carrier[TRACE_STATE_HEADER];
if(traceStateHeader){
// If more than one `tracestate` header is found, we merge them into a
// single header.
conststate=Array.isArray(traceStateHeader)
? traceStateHeader.join(',')
: traceStateHeader;
spanContext.traceState=newTraceState(stateasstring);
}
returnspanContext;
}
}