Skip to content

Commit

Permalink
Implement Lambda instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettwegan committed Jul 22, 2021
1 parent 6d226af commit 76a52a1
Show file tree
Hide file tree
Showing 8 changed files with 1,098 additions and 0 deletions.
68 changes: 68 additions & 0 deletions detectors/aws/lambda/detector.go
@@ -0,0 +1,68 @@
// Copyright The OpenTelemetry 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 lambda

import (
"context"
"errors"
"os"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

const (
lambdaFunctionNameEnvVar = "AWS_LAMBDA_FUNCTION_NAME"
awsRegionEnvVar = "AWS_REGION"
lambdaFunctionVersionEnvVar = "AWS_LAMBDA_FUNCTION_VERSION"
)

var (
empty = resource.Empty()
errNotOnLambda = errors.New("process is not on Lambda, cannot detect environment variables from Lambda")
)

// resource detector collects resource information from Lambda environment
type resourceDetector struct{}

// compile time assertion that resource detector implements the resource.Detector interface.
var _ resource.Detector = (*resourceDetector)(nil)

// NewResourceDetector returns a resource detector that will detect AWS Lambda resources.
func NewResourceDetector() resource.Detector {
return &resourceDetector{}
}

// Detect collects resources available when running on lambda
func (detector *resourceDetector) Detect(context.Context) (*resource.Resource, error) {

// Lambda resources come from ENV
lambdaName := os.Getenv(lambdaFunctionNameEnvVar)
if len(lambdaName) == 0 {
return empty, errNotOnLambda
}
awsRegion := os.Getenv(awsRegionEnvVar)
functionVersion := os.Getenv(lambdaFunctionVersionEnvVar)

attrs := []attribute.KeyValue{
semconv.CloudProviderAWS,
semconv.CloudRegionKey.String(awsRegion),
semconv.FaaSNameKey.String(lambdaName),
semconv.FaaSVersionKey.String(functionVersion),
}

return resource.NewWithAttributes(semconv.SchemaURL, attrs...), nil
}
44 changes: 44 additions & 0 deletions detectors/aws/lambda/detector_test.go
@@ -0,0 +1,44 @@
package lambda

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

// successfully return resource when process is running on Amazon Lambda environment
func TestDetectSuccess(t *testing.T) {
os.Clearenv()
_ = os.Setenv(lambdaFunctionNameEnvVar, "testFunction")
_ = os.Setenv(awsRegionEnvVar, "us-texas-1")
_ = os.Setenv(lambdaFunctionVersionEnvVar, "$LATEST")

attributes := []attribute.KeyValue{
semconv.CloudProviderAWS,
semconv.CloudRegionKey.String("us-texas-1"),
semconv.FaaSNameKey.String("testFunction"),
semconv.FaaSVersionKey.String("$LATEST"),
}
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...)
detector := resourceDetector{}
res, err := detector.Detect(context.Background())

assert.Nil(t, err, "Detector unexpectedly returned error")
assert.Equal(t, expectedResource, res, "Resource returned is incorrect")
}

// return empty resource when not running on lambda
func TestReturnsIfNoEnvVars(t *testing.T) {
os.Clearenv()
detector := resourceDetector{}
res, err := detector.Detect(context.Background())

assert.Equal(t, errNotOnLambda, err)
assert.Equal(t, 0, len(res.Attributes()))
}
11 changes: 11 additions & 0 deletions detectors/aws/lambda/go.mod
@@ -0,0 +1,11 @@
module go.opentelemetry.io/contrib/detectors/aws/lambda

go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/otel v1.0.0-RC1
go.opentelemetry.io/otel/sdk v1.0.0-RC1
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
24 changes: 24 additions & 0 deletions detectors/aws/lambda/go.sum
@@ -0,0 +1,24 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.0.0-RC1 h1:4CeoX93DNTWt8awGK9JmNXzF9j7TyOu9upscEdtcdXc=
go.opentelemetry.io/otel v1.0.0-RC1/go.mod h1:x9tRa9HK4hSSq7jf2TKbqFbtt58/TGk0f9XiEYISI1I=
go.opentelemetry.io/otel/oteltest v1.0.0-RC1 h1:G685iP3XiskCwk/z0eIabL55XUl2gk0cljhGk9sB0Yk=
go.opentelemetry.io/otel/oteltest v1.0.0-RC1/go.mod h1:+eoIG0gdEOaPNftuy1YScLr1Gb4mL/9lpDkZ0JjMRq4=
go.opentelemetry.io/otel/sdk v1.0.0-RC1 h1:Sy2VLOOg24bipyC29PhuMXYNJrLsxkie8hyI7kUlG9Q=
go.opentelemetry.io/otel/sdk v1.0.0-RC1/go.mod h1:kj6yPn7Pgt5ByRuwesbaWcRLA+V7BSDg3Hf8xRvsvf8=
go.opentelemetry.io/otel/trace v1.0.0-RC1 h1:jrjqKJZEibFrDz+umEASeU3LvdVyWKlnTh7XEfwrT58=
go.opentelemetry.io/otel/trace v1.0.0-RC1/go.mod h1:86UHmyHWFEtWjfWPSbu0+d0Pf9Q6e1U+3ViBOc+NXAg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 19 additions & 0 deletions instrumentation/github.com/aws/otellambda/go.mod
@@ -0,0 +1,19 @@
module go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda

go 1.16

replace (
go.opentelemetry.io/contrib/detectors/aws/lambda => ../../../../../detectors/aws/lambda
go.opentelemetry.io/contrib/propagators/aws => ../../../../../propagators/aws
)

require (
github.com/aws/aws-lambda-go v1.24.0
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/contrib/detectors/aws/lambda v0.21.0
go.opentelemetry.io/contrib/propagators/aws v0.21.0
go.opentelemetry.io/otel v1.0.0-RC1
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0-RC1
go.opentelemetry.io/otel/sdk v1.0.0-RC1
go.opentelemetry.io/otel/trace v1.0.0-RC1
)

0 comments on commit 76a52a1

Please sign in to comment.