github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/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  	instana "github.com/instana/go-sensor"
    18  	"github.com/opentracing/opentracing-go"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    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 TestLambdaAgent_SendSpans_Error(t *testing.T) {
    89  	defer agent.Reset()
    90  
    91  	tracer := instana.NewTracer()
    92  	sensor := instana.NewSensorWithTracer(tracer)
    93  	defer instana.ShutdownSensor()
    94  
    95  	sp := sensor.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
    96  		"lambda.arn":     "aws::test-lambda::$LATEST",
    97  		"lambda.name":    "test-lambda",
    98  		"lambda.version": "$LATEST",
    99  		"lambda.error":   "true",
   100  	})
   101  	sp.Finish()
   102  
   103  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
   104  	defer cancel()
   105  
   106  	require.NoError(t, tracer.Flush(ctx))
   107  	require.Len(t, agent.Bundles, 0)
   108  }
   109  
   110  func setupAWSLambdaEnv() func() {
   111  	teardown := restoreEnvVarFunc("AWS_EXECUTION_ENV")
   112  	os.Setenv("AWS_EXECUTION_ENV", "AWS_Lambda_go1.x")
   113  
   114  	return teardown
   115  }