github.com/newrelic/go-agent@v3.26.0+incompatible/internal/serverless_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package internal 5 6 import ( 7 "bytes" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/newrelic/go-agent/internal/logger" 13 ) 14 15 func serverlessGetenvShim(s string) string { 16 if s == "AWS_EXECUTION_ENV" { 17 return "the-execution-env" 18 } 19 return "" 20 } 21 22 func TestServerlessHarvest(t *testing.T) { 23 // Test the expected ServerlessHarvest use. 24 sh := NewServerlessHarvest(logger.ShimLogger{}, "the-version", serverlessGetenvShim) 25 event, err := CreateCustomEvent("myEvent", nil, time.Now()) 26 if nil != err { 27 t.Fatal(err) 28 } 29 sh.Consume(event) 30 buf := &bytes.Buffer{} 31 sh.Write("arn", buf) 32 metadata, data, err := ParseServerlessPayload(buf.Bytes()) 33 if nil != err { 34 t.Fatal(err) 35 } 36 if v := string(metadata["metadata_version"]); v != `2` { 37 t.Error(v) 38 } 39 if v := string(metadata["arn"]); v != `"arn"` { 40 t.Error(v) 41 } 42 if v := string(metadata["protocol_version"]); v != `17` { 43 t.Error(v) 44 } 45 if v := string(metadata["execution_environment"]); v != `"the-execution-env"` { 46 t.Error(v) 47 } 48 if v := string(metadata["agent_version"]); v != `"the-version"` { 49 t.Error(v) 50 } 51 if v := string(metadata["agent_language"]); v != `"go"` { 52 t.Error(v) 53 } 54 eventData := string(data["custom_event_data"]) 55 if !strings.Contains(eventData, `"type":"myEvent"`) { 56 t.Error(eventData) 57 } 58 if len(data) != 1 { 59 t.Fatal(data) 60 } 61 // Test that the harvest was replaced with a new harvest. 62 buf = &bytes.Buffer{} 63 sh.Write("arn", buf) 64 if 0 != buf.Len() { 65 t.Error(buf.String()) 66 } 67 } 68 69 func TestServerlessHarvestNil(t *testing.T) { 70 // The public ServerlessHarvest methods should not panic if the 71 // receiver is nil. 72 var sh *ServerlessHarvest 73 event, err := CreateCustomEvent("myEvent", nil, time.Now()) 74 if nil != err { 75 t.Fatal(err) 76 } 77 sh.Consume(event) 78 buf := &bytes.Buffer{} 79 sh.Write("arn", buf) 80 } 81 82 func TestServerlessHarvestEmpty(t *testing.T) { 83 // Test that ServerlessHarvest.Write doesn't do anything if the harvest 84 // is empty. 85 sh := NewServerlessHarvest(logger.ShimLogger{}, "the-version", serverlessGetenvShim) 86 buf := &bytes.Buffer{} 87 sh.Write("arn", buf) 88 if 0 != buf.Len() { 89 t.Error(buf.String()) 90 } 91 } 92 93 func BenchmarkServerless(b *testing.B) { 94 // The JSON creation in ServerlessHarvest.Write has not been optimized. 95 // This benchmark would be useful for doing so. 96 sh := NewServerlessHarvest(logger.ShimLogger{}, "the-version", serverlessGetenvShim) 97 event, err := CreateCustomEvent("myEvent", nil, time.Now()) 98 if nil != err { 99 b.Fatal(err) 100 } 101 102 b.ResetTimer() 103 b.ReportAllocs() 104 105 for i := 0; i < b.N; i++ { 106 sh.Consume(event) 107 buf := &bytes.Buffer{} 108 sh.Write("arn", buf) 109 if buf.Len() == 0 { 110 b.Fatal(buf.String()) 111 } 112 } 113 }