This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
http_propagation.go
116 lines (101 loc) · 3.4 KB
/
http_propagation.go
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
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2018, OpenCensus 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
//
// http://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.
package aws
import (
"net/http"
"strings"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
)
const (
httpHeaderMaxSize = 200
httpHeader = `X-Amzn-Trace-Id`
prefixRoot = "Root="
prefixParent = "Parent="
prefixSampled = "Sampled="
separator = ";" // separator used by x-ray to split parts of X-Amzn-Trace-Id header
)
// HTTPFormat implements propagation.HTTPFormat to propagate
// traces in HTTP headers for for Amazon services: ELB, ALB, Lambda, etc.
type HTTPFormat struct{}
var _ propagation.HTTPFormat = (*HTTPFormat)(nil)
// ParseTraceHeader parses an Amazon trace header to OpenCensus span context.
func ParseTraceHeader(header string) (trace.SpanContext, bool) {
var (
traceID trace.TraceID
traceIDSet bool
spanID trace.SpanID
traceOptions trace.TraceOptions
)
// Parse the parts of the amazon http trace id regardless of the
// order the parts appear in.
//
// In most cases, the Root= will be the first part. However, in
// other cases (like an API Gateway proxy to an HTTP server), the
// leading part will be Self=
//
parts := strings.Split(header, separator)
for _, part := range parts {
switch {
case strings.HasPrefix(part, prefixRoot):
v, err := parseAmazonTraceID(part[len(prefixRoot):])
if err != nil {
return trace.SpanContext{}, false
}
traceID = v
traceIDSet = true
case strings.HasPrefix(part, prefixParent):
v, err := parseAmazonSpanID(part[len(prefixParent):])
if err != nil {
return trace.SpanContext{}, false
}
spanID = v
case strings.HasPrefix(part, prefixSampled):
if part[len(prefixParent)+1] == '1' {
traceOptions = 1
}
default:
// possibly a naked trace id. because we're not sure, we won't bail
// on failure.
if v, err := parseAmazonTraceID(part); err == nil {
traceID = v
traceIDSet = true
}
}
}
if !traceIDSet {
return trace.SpanContext{}, false
}
return trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceOptions: traceOptions,
}, true
}
// SpanContextFromRequest extracts an AWS X-Ray Trace span context from incoming requests.
func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool) {
h := req.Header.Get(httpHeader)
// See https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html
// for the header format. Return if the header is empty or missing, or if
// the header is unreasonably large, to avoid making unnecessary copies of
// a large string.
if h == "" || len(h) > httpHeaderMaxSize {
return trace.SpanContext{}, false
}
return ParseTraceHeader(h)
}
// SpanContextToRequest modifies the given request to include a AWS X-Ray trace header.
func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request) {
req.Header.Set(httpHeader, TraceHeader(sc))
}