forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtracer.go
More file actions
152 lines (129 loc) · 5.26 KB
/
tracer.go
File metadata and controls
152 lines (129 loc) · 5.26 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package telemetry
import (
"context"
"encoding/json"
"fmt"
"strings"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi/model"
)
type traceContextKey int
const (
TraceIDKey traceContextKey = iota
DocumentIDKey
)
type Tracer interface {
Configure(invoke *interop.Invoke)
CaptureInvokeSegment(ctx context.Context, criticalFunction func(context.Context) error) error
CaptureInitSubsegment(ctx context.Context, criticalFunction func(context.Context) error) error
CaptureInvokeSubsegment(ctx context.Context, criticalFunction func(context.Context) error) error
CaptureOverheadSubsegment(ctx context.Context, criticalFunction func(context.Context) error) error
RecordInitStartTime()
RecordInitEndTime()
SendInitSubsegmentWithRecordedTimesOnce(ctx context.Context)
SendRestoreSubsegmentWithRecordedTimesOnce(ctx context.Context)
MarkError(ctx context.Context)
AttachErrorCause(ctx context.Context, errorCause json.RawMessage)
WithErrorCause(ctx context.Context, appCtx appctx.ApplicationContext, criticalFunction func(ctx context.Context) error) func(ctx context.Context) error
WithError(ctx context.Context, appCtx appctx.ApplicationContext, criticalFunction func(ctx context.Context) error) func(ctx context.Context) error
BuildTracingHeader() func(context.Context) string
BuildTracingCtxForStart() *interop.TracingCtx
BuildTracingCtxAfterInvokeComplete() *interop.TracingCtx
}
type NoOpTracer struct{}
func (t *NoOpTracer) Configure(invoke *interop.Invoke) {}
func (t *NoOpTracer) CaptureInvokeSegment(ctx context.Context, criticalFunction func(context.Context) error) error {
return criticalFunction(ctx)
}
func (t *NoOpTracer) CaptureInitSubsegment(ctx context.Context, criticalFunction func(context.Context) error) error {
return criticalFunction(ctx)
}
func (t *NoOpTracer) CaptureInvokeSubsegment(ctx context.Context, criticalFunction func(context.Context) error) error {
return criticalFunction(ctx)
}
func (t *NoOpTracer) CaptureOverheadSubsegment(ctx context.Context, criticalFunction func(context.Context) error) error {
return criticalFunction(ctx)
}
func (t *NoOpTracer) RecordInitStartTime() {}
func (t *NoOpTracer) RecordInitEndTime() {}
func (t *NoOpTracer) SendInitSubsegmentWithRecordedTimesOnce(ctx context.Context) {}
func (t *NoOpTracer) SendRestoreSubsegmentWithRecordedTimesOnce(ctx context.Context) {}
func (t *NoOpTracer) MarkError(ctx context.Context) {}
func (t *NoOpTracer) AttachErrorCause(ctx context.Context, errorCause json.RawMessage) {}
func (t *NoOpTracer) WithErrorCause(ctx context.Context, appCtx appctx.ApplicationContext, criticalFunction func(ctx context.Context) error) func(ctx context.Context) error {
return criticalFunction
}
func (t *NoOpTracer) WithError(ctx context.Context, appCtx appctx.ApplicationContext, criticalFunction func(ctx context.Context) error) func(ctx context.Context) error {
return criticalFunction
}
func (t *NoOpTracer) BuildTracingHeader() func(context.Context) string {
// extract root trace ID and parent from context and build the tracing header
return func(ctx context.Context) string {
root, _ := ctx.Value(TraceIDKey).(string)
parent, _ := ctx.Value(DocumentIDKey).(string)
if root != "" && parent != "" {
return fmt.Sprintf("Root=%s;Parent=%s;Sampled=1", root, parent)
}
return ""
}
}
func (t *NoOpTracer) BuildTracingCtxForStart() *interop.TracingCtx {
return nil
}
func (t *NoOpTracer) BuildTracingCtxAfterInvokeComplete() *interop.TracingCtx {
return nil
}
func NewNoOpTracer() *NoOpTracer {
return &NoOpTracer{}
}
// NewTraceContext returns new derived context with trace config set for testing
func NewTraceContext(ctx context.Context, root string, parent string) context.Context {
ctxWithRoot := context.WithValue(ctx, TraceIDKey, root)
return context.WithValue(ctxWithRoot, DocumentIDKey, parent)
}
// ParseTracingHeader extracts RootTraceID, ParentID, Sampled, and Lineage from a tracing header.
// Tracing header format is defined here:
// https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
func ParseTracingHeader(tracingHeader string) (rootID, parentID, sampled, lineage string) {
keyValuePairs := strings.Split(tracingHeader, ";")
for _, pair := range keyValuePairs {
var key, value string
keyValue := strings.Split(pair, "=")
if len(keyValue) == 2 {
key = keyValue[0]
value = keyValue[1]
}
switch key {
case "Root":
rootID = value
case "Parent":
parentID = value
case "Sampled":
sampled = value
case "Lineage":
lineage = value
}
}
return
}
// BuildFullTraceID takes individual components of X-Ray trace header
// and puts them together into a formatted trace header.
// If root is empty, returns an empty string.
func BuildFullTraceID(root, parent, sample string) string {
if root == "" {
return ""
}
parts := make([]string, 0, 3)
parts = append(parts, "Root="+root)
if parent != "" {
parts = append(parts, "Parent="+parent)
}
if sample == "" {
sample = model.XRayNonSampled
}
parts = append(parts, "Sampled="+sample)
return strings.Join(parts, ";")
}