/
lambda.go
112 lines (92 loc) · 2.73 KB
/
lambda.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
package lambda
import (
"context"
"errors"
"os"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
const (
functionName = "AWS_LAMBDA_FUNCTION_NAME"
region = "AWS_REGION"
functionVersion = "AWS_LAMBDA_FUNCTION_VERSION"
)
var (
errNotOnLambda = errors.New("process is not on Lambda, cannot detect environment variables from lambda")
)
type Client interface {
GetCallerIdentity(input *sts.GetCallerIdentityInput) (*sts.GetCallerIdentityOutput, error)
}
var getClient = func() Client {
sess, _ := session.NewSession()
return sts.New(sess)
}
//Lambda implements, resource.Detector for aws lambda
type Lambda struct {
}
//Detect will return a resource instance which will have attributes describing lambda
func (lm *Lambda) Detect(ctx context.Context) (*resource.Resource, error) {
if !isAWSLambda() {
return resource.Empty(), errNotOnLambda
}
functionName := awsLambdafuncName()
awsRegion := awsRegion()
functionVersion := lambdaFunctionVersion()
functionID := getAWSLambdaARN(ctx)
accountID, _ := getAWSAccountID()
attributes := []attribute.KeyValue{
semconv.CloudProviderAWS,
semconv.CloudPlatformAWSLambda,
}
if functionID != "" {
attributes = append(attributes, attribute.String(string(semconv.FaaSIDKey), functionID))
} else {
attributes = append(attributes, []attribute.KeyValue{
attribute.String(string(semconv.FaaSNameKey), functionName),
attribute.String(string(semconv.CloudAccountIDKey), accountID),
attribute.String(string(semconv.CloudRegionKey), awsRegion),
attribute.String(string(semconv.FaaSVersionKey), functionVersion),
}...)
}
return resource.Merge(resource.Default(), resource.NewSchemaless(attributes...))
}
var isAWSLambda = func() bool {
_, present := os.LookupEnv(functionName)
return present
}
func awsLambdafuncName() string {
name, _ := os.LookupEnv(functionName)
return name
}
func awsRegion() string {
regionVal, _ := os.LookupEnv(region)
return regionVal
}
func lambdaFunctionVersion() string {
version, _ := os.LookupEnv(functionVersion)
return version
}
var getAWSLambdaARN = func(ctx context.Context) string {
lc, ok := lambdacontext.FromContext(ctx)
if ok {
return lc.InvokedFunctionArn
}
return ""
}
func getAWSAccountID() (string, error) {
svc := getClient()
input := &sts.GetCallerIdentityInput{}
result, err := svc.GetCallerIdentity(input)
if err != nil {
return "", err
}
return *result.Account, nil
}
//NewResourceDetector will return an implementation for aws lambda resource detector
func NewResourceDetector() resource.Detector {
return &Lambda{}
}