/
span.go
315 lines (275 loc) · 8.52 KB
/
span.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package transform
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/google/uuid"
lumigoctx "github.com/lumigo-io/lumigo-go-tracer/internal/context"
"github.com/lumigo-io/lumigo-go-tracer/internal/telemetry"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
apitrace "go.opentelemetry.io/otel/trace"
)
type mapper struct {
ctx context.Context
span sdktrace.ReadOnlySpan
logger logrus.FieldLogger
maxEntrySize int
}
func NewMapper(ctx context.Context, span sdktrace.ReadOnlySpan, logger logrus.FieldLogger, maxEntrySize int) *mapper {
return &mapper{
ctx: ctx,
span: span,
logger: logger,
maxEntrySize: maxEntrySize,
}
}
func (m *mapper) Transform(invocationStartedTimestamp int64) telemetry.Span {
numAttrs := len(m.span.Attributes()) + m.span.Resource().Len() + 2
if m.span.SpanKind() != apitrace.SpanKindUnspecified {
numAttrs++
}
attrs := make(map[string]interface{}, numAttrs)
for iter := m.span.Resource().Iter(); iter.Next(); {
kv := iter.Label()
attrs[string(kv.Key)] = kv.Value.AsInterface()
}
for _, kv := range m.span.Attributes() {
attrs[string(kv.Key)] = kv.Value.AsInterface()
m.logger.WithField(string(kv.Key), kv.Value.AsInterface()).Info("span.Attributes() in Transform")
}
m.logger.WithFields(attrs).Info("span attributes in Transform")
if m.span.SpanKind() != apitrace.SpanKindUnspecified {
attrs["m.span.kind"] = strings.ToLower(m.span.SpanKind().String())
}
startTime := unixMilli(m.span.StartTime())
if telemetry.IsEndSpan(m.span) {
startTime = invocationStartedTimestamp
}
lumigoSpan := telemetry.Span{
StartedTimestamp: startTime,
EndedTimestamp: unixMilli(m.span.EndTime()),
}
isStartSpan := telemetry.IsStartSpan(m.span)
isEndSpan := telemetry.IsEndSpan(m.span)
lumigoSpan.Region = os.Getenv("AWS_REGION")
awsRoot := getAmazonTraceID()
if awsRoot == "" {
m.logger.Error("unable to fetch Amazon Trace ID")
}
lumigoSpan.SpanInfo = telemetry.SpanInfo{
LogStreamName: os.Getenv("AWS_LAMBDA_LOG_STREAM_NAME"),
LogGroupName: os.Getenv("AWS_LAMBDA_LOG_GROUP_NAME"),
TraceID: telemetry.SpanTraceRoot{
Root: awsRoot,
},
}
lambdaName := os.Getenv("AWS_LAMBDA_FUNCTION_NAME")
spanType := "function"
if m.span.Name() != lambdaName && m.span.Name() != "LumigoParentSpan" {
spanType = "http"
lumigoSpan.SpanInfo.HttpInfo = m.getHTTPInfo(attrs)
} else {
lumigoSpan.LambdaName = lambdaName
lumigoSpan.MemoryAllocated = os.Getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
lumigoSpan.Runtime = os.Getenv("AWS_EXECUTION_ENV")
if event, ok := attrs["event"]; ok {
lumigoSpan.Event = fmt.Sprint(event)
} else {
m.logger.Error("unable to fetch event")
}
isWarmStart := os.Getenv("IS_WARM_START")
if isWarmStart == "" && !isProvisionConcurrencyInitialization() {
lumigoSpan.LambdaReadiness = "cold"
} else {
lumigoSpan.LambdaReadiness = "warm"
}
}
lumigoSpan.SpanType = spanType
if spanType == "function" {
lumigoSpan.LambdaEnvVars = m.getEnvVars()
}
lambdaCtx, lambdaOk := lambdacontext.FromContext(m.ctx)
if lambdaOk {
containerID, _ := uuid.NewUUID()
lumigoSpan.LambdaContainerID = containerID.String()
if spanType == "http" {
spanID, _ := uuid.NewUUID()
lumigoSpan.ID = spanID.String()
lumigoSpan.ParentID = lambdaCtx.AwsRequestID
} else {
lumigoSpan.ID = lambdaCtx.AwsRequestID
}
if isStartSpan {
lumigoSpan.ID = fmt.Sprintf("%s_started", lumigoSpan.ID)
deadline, _ := m.ctx.Deadline()
lumigoSpan.MaxFinishTime = unixMilli(time.Now()) - unixMilli(deadline)
}
accountID, err := getAccountID(lambdaCtx)
if err != nil {
m.logger.WithError(err).Error()
}
lumigoSpan.Account = accountID
} else {
m.logger.Error("unable to fetch from LambdaContext")
}
if token, ok := attrs["lumigo_token"]; ok {
lumigoSpan.Token = fmt.Sprint(token)
} else {
m.logger.Error("unable to fetch lumigo token from span")
}
if isEndSpan {
os.Setenv("IS_WARM_START", "true")
lumigoSpan.SpanError = m.getSpanError(attrs)
lambdaResp := m.getAttrAndLimit(attrs, "response")
if lambdaResp != "" {
lumigoSpan.LambdaResponse = aws.String(lambdaResp)
}
}
if transactionID := getTransactionID(awsRoot); transactionID != "" {
lumigoSpan.TransactionID = transactionID
} else {
m.logger.Error("unable to fetch transaction ID")
}
lumigoCtx, lumigoOk := lumigoctx.FromContext(m.ctx)
if lumigoOk {
lumigoSpan.SpanInfo.TracerVersion = telemetry.TracerVersion{
Version: lumigoCtx.TracerVersion,
}
} else {
m.logger.Error("unable to fetch from LumigoContext")
}
return lumigoSpan
}
func (m *mapper) getSpanError(attrs map[string]interface{}) *telemetry.SpanError {
if _, ok := attrs["has_error"]; !ok {
return nil
}
var spanError telemetry.SpanError
if errType, ok := attrs["error_type"]; ok {
spanError.Type = fmt.Sprint(errType)
} else {
m.logger.Error("unable to fetch lambda error type from span")
}
if errMessage, ok := attrs["error_message"]; ok {
spanError.Message = fmt.Sprint(errMessage)
} else {
m.logger.Error("unable to fetch lambda error message from span")
}
if errStacktrace, ok := attrs["error_stacktrace"]; ok {
spanError.Stacktrace = fmt.Sprint(errStacktrace)
} else {
m.logger.Error("unable to fetch lambda error stacktrace from span")
}
if spanError.IsEmpty() {
return nil
}
return &spanError
}
func (m *mapper) getEnvVars() string {
envs := make(map[string]string)
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
envs[pair[0]] = pair[1]
}
envsBytes, err := json.Marshal(envs)
if err != nil {
m.logger.Error("unable to fetch lambda environment vars")
}
if len(envsBytes) > m.maxEntrySize {
envsBytes = envsBytes[:m.maxEntrySize]
}
return string(envsBytes)
}
func (m *mapper) getHTTPInfo(attrs map[string]interface{}) *telemetry.SpanHttpInfo {
var spanHttpInfo telemetry.SpanHttpInfo
if host, ok := attrs["http.host"]; ok {
spanHttpInfo.Host = fmt.Sprint(host)
} else {
m.logger.Error("unable to fetch HTTP host")
}
if method, ok := attrs["http.method"]; ok {
spanHttpInfo.Request.Method = aws.String(fmt.Sprint(method))
} else {
m.logger.Error("unable to fetch HTTP method")
}
if target, ok := attrs["http.target"]; ok {
uri := fmt.Sprintf("%s%s", spanHttpInfo.Host, target)
spanHttpInfo.Request.URI = aws.String(uri)
} else {
m.logger.Error("unable to fetch HTTP target")
}
if headers, ok := attrs["http.request_headers"]; ok {
spanHttpInfo.Request.Headers = fmt.Sprint(headers)
} else {
m.logger.Error("unable to fetch HTTP request headers")
}
if reqBody, ok := attrs["http.request_body"]; ok {
spanHttpInfo.Request.Body = fmt.Sprint(reqBody)
}
if headers, ok := attrs["http.response_headers"]; ok {
spanHttpInfo.Response.Headers = fmt.Sprint(headers)
} else {
m.logger.Error("unable to fetch HTTP response headers")
}
// response
if respBody, ok := attrs["http.response_body"]; ok {
spanHttpInfo.Response.Body = fmt.Sprint(respBody)
} else {
m.logger.Error("unable to fetch HTTP response body")
}
if code, ok := attrs["http.status_code"]; ok {
spanHttpInfo.Response.StatusCode = aws.Int64(code.(int64))
} else {
m.logger.Error("unable to fetch HTTP status code")
}
return &spanHttpInfo
}
func isProvisionConcurrencyInitialization() bool {
return os.Getenv("AWS_LAMBDA_INITIALIZATION_TYPE") == "provisioned-concurrency"
}
func getAccountID(ctx *lambdacontext.LambdaContext) (string, error) {
functionARN, err := arn.Parse(ctx.InvokedFunctionArn)
if err != nil {
return "", errors.Wrap(err, "failed to parse ARN")
}
return functionARN.AccountID, nil
}
func getAmazonTraceID() string {
awsTraceItems := strings.SplitN(os.Getenv("_X_AMZN_TRACE_ID"), ";", 2)
if len(awsTraceItems) > 1 {
root := strings.SplitN(awsTraceItems[0], "=", 2)
return root[1]
}
return ""
}
func getTransactionID(root string) string {
items := strings.SplitN(root, "-", 3)
if len(items) > 1 {
return items[2]
}
return ""
}
func (m *mapper) getAttrAndLimit(attrs map[string]interface{}, key string) string {
if value, ok := attrs[key]; ok {
valueStr := fmt.Sprint(value)
if len(valueStr) > m.maxEntrySize {
valueStr = valueStr[:m.maxEntrySize]
}
return valueStr
} else {
m.logger.Errorf("unable to fetch lambda %s from span", key)
}
return ""
}
// backward compatability for time.Now().UnixMilli() in go 1.16 and earlier
func unixMilli(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}