github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/lambda_agent_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  //go:build lambda && integration
     5  // +build lambda,integration
     6  
     7  package instana_test
     8  
     9  import (
    10  	"context"
    11  	"encoding/json"
    12  	"log"
    13  	"os"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/instana/testify/assert"
    18  	"github.com/instana/testify/require"
    19  	instana "github.com/mier85/go-sensor"
    20  	"github.com/opentracing/opentracing-go"
    21  )
    22  
    23  var agent *serverlessAgent
    24  
    25  func TestMain(m *testing.M) {
    26  	teardownEnv := setupAWSLambdaEnv()
    27  	defer teardownEnv()
    28  
    29  	defer restoreEnvVarFunc("INSTANA_AGENT_KEY")
    30  	os.Setenv("INSTANA_AGENT_KEY", "testkey1")
    31  
    32  	defer restoreEnvVarFunc("INSTANA_ZONE")
    33  	os.Setenv("INSTANA_ZONE", "testzone")
    34  
    35  	defer restoreEnvVarFunc("INSTANA_TAGS")
    36  	os.Setenv("INSTANA_TAGS", "key1=value1,key2")
    37  
    38  	defer restoreEnvVarFunc("INSTANA_SECRETS")
    39  	os.Setenv("INSTANA_SECRETS", "contains-ignore-case:key,password,secret,classified")
    40  
    41  	defer restoreEnvVarFunc("CLASSIFIED_DATA")
    42  	os.Setenv("CLASSIFIED_DATA", "classified")
    43  
    44  	var err error
    45  	agent, err = setupServerlessAgent()
    46  	if err != nil {
    47  		log.Fatalf("failed to initialize serverless agent: %s", err)
    48  	}
    49  
    50  	instana.InitSensor(instana.DefaultOptions())
    51  
    52  	os.Exit(m.Run())
    53  }
    54  
    55  func TestLambdaAgent_SendSpans(t *testing.T) {
    56  	defer agent.Reset()
    57  
    58  	tracer := instana.NewTracer()
    59  	sensor := instana.NewSensorWithTracer(tracer)
    60  
    61  	sp := sensor.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
    62  		"lambda.arn":     "aws::test-lambda::$LATEST",
    63  		"lambda.name":    "test-lambda",
    64  		"lambda.version": "$LATEST",
    65  	})
    66  	sp.Finish()
    67  
    68  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    69  	defer cancel()
    70  
    71  	require.NoError(t, tracer.Flush(ctx))
    72  	require.Len(t, agent.Bundles, 1)
    73  
    74  	var spans []map[string]json.RawMessage
    75  	for _, bundle := range agent.Bundles {
    76  		var payload struct {
    77  			Spans []map[string]json.RawMessage `json:"spans"`
    78  		}
    79  
    80  		require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
    81  		spans = append(spans, payload.Spans...)
    82  	}
    83  
    84  	require.Len(t, spans, 1)
    85  	assert.JSONEq(t, `{"hl": true, "cp": "aws", "e": "aws::test-lambda::$LATEST"}`, string(spans[0]["f"]))
    86  }
    87  
    88  func setupAWSLambdaEnv() func() {
    89  	teardown := restoreEnvVarFunc("AWS_EXECUTION_ENV")
    90  	os.Setenv("AWS_EXECUTION_ENV", "AWS_Lambda_go1.x")
    91  
    92  	return teardown
    93  }