-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (75 loc) · 2.13 KB
/
main.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
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/jan-xyz/box"
awslambdago "github.com/jan-xyz/box/transports/github.com/aws/aws-lambda-go"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
)
var (
service = "otel-test"
environment = "dev"
res = resource.NewWithAttributes(
semconv.SchemaURL,
// sets the service correctly
semconv.ServiceName(service),
// helps parse stack traces and errors
semconv.TelemetrySDKLanguageGo,
// others
semconv.DeploymentEnvironment(environment),
semconv.ServiceVersion("v0.0.1"),
)
)
func main() {
ctx := context.Background()
// Setup instrumentation
lp := setupLogging(ctx, res)
mp := setupMetrics(ctx, res)
tp := setupTracing(ctx, res)
ep := box.Chain(
tracingMiddleware(),
metricMiddleware(),
loggingMiddleware(),
)(Endpoint)
handler := awslambdago.NewAPIGatewayTransport(
func(_ *events.APIGatewayProxyRequest) (string, error) { return "", nil },
func(_ string) (*events.APIGatewayProxyResponse, error) { return nil, nil },
func(err error) *events.APIGatewayProxyResponse { return nil },
ep,
)
defer func() {
if err := lp.Shutdown(ctx); err != nil {
panic(err)
}
}()
defer func() {
if err := tp.Shutdown(ctx); err != nil {
panic(err)
}
}()
defer func() {
if err := mp.Shutdown(ctx); err != nil {
panic(err)
}
}()
// Setup Lambda
lambda.StartWithOptions(
// wraps the lambda handler to inject the OTEL context
otellambda.InstrumentHandler(
handler,
// options to properly extract the AWS traceID from the trigger event,
// flush the trace provide after each handler event,
// set the propagator, carrier and trace provider.
xrayconfig.WithEventToCarrier(),
xrayconfig.WithPropagator(),
otellambda.WithTracerProvider(tp),
otellambda.WithFlusher(lp),
otellambda.WithFlusher(tp),
otellambda.WithFlusher(mp),
),
)
}