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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"bytes"
"net/http"
"strings"
"testing"
"time"
"github.com/newrelic/go-agent/v3/internal"
)
func TestServerlessDistributedTracingConfigPresent(t *testing.T) {
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
cfg.DistributedTracer.Enabled = true
cfg.ServerlessMode.AccountID = "123"
cfg.ServerlessMode.TrustedAccountKey = "987"
cfg.ServerlessMode.PrimaryAppID = "456"
}
app := testApp(nil, cfgFn, t)
hdrs := http.Header{}
app.StartTransaction("hello").InsertDistributedTraceHeaders(hdrs)
txn := app.StartTransaction("hello")
txn.AcceptDistributedTraceHeaders(TransportHTTP, hdrs)
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "OtherTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "DurationByCaller/App/123/456/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/App/123/456/HTTP/allOther", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/456/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/456/HTTP/allOther", Scope: "", Forced: false, Data: nil},
{Name: "Supportability/TraceContext/Accept/Success", Scope: "", Forced: true, Data: singleCount},
})
}
func TestServerlessDistributedTracingConfigPartiallyPresent(t *testing.T) {
// This tests that if ServerlessMode.PrimaryAppID is unset it should
// default to "Unknown".
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
cfg.DistributedTracer.Enabled = true
cfg.ServerlessMode.AccountID = "123"
cfg.ServerlessMode.TrustedAccountKey = "987"
}
app := testApp(nil, cfgFn, t)
hdrs := http.Header{}
app.StartTransaction("hello").InsertDistributedTraceHeaders(hdrs)
txn := app.StartTransaction("hello")
txn.AcceptDistributedTraceHeaders(TransportHTTP, hdrs)
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "OtherTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "DurationByCaller/App/123/Unknown/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/App/123/Unknown/HTTP/allOther", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/Unknown/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/Unknown/HTTP/allOther", Scope: "", Forced: false, Data: nil},
{Name: "Supportability/TraceContext/Accept/Success", Scope: "", Forced: true, Data: singleCount},
})
}
func TestServerlessDistributedTracingConfigTrustKeyAbsent(t *testing.T) {
// Test that distributed tracing works if only AccountID has been set.
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
cfg.DistributedTracer.Enabled = true
cfg.ServerlessMode.AccountID = "123"
}
app := testApp(nil, cfgFn, t)
hdrs := http.Header{}
app.StartTransaction("hello").InsertDistributedTraceHeaders(hdrs)
txn := app.StartTransaction("hello")
txn.AcceptDistributedTraceHeaders(TransportHTTP, hdrs)
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "OtherTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "DurationByCaller/App/123/Unknown/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/App/123/Unknown/HTTP/allOther", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/Unknown/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/Unknown/HTTP/allOther", Scope: "", Forced: false, Data: nil},
{Name: "Supportability/TraceContext/Accept/Success", Scope: "", Forced: true, Data: singleCount},
})
}
func TestServerlessDistributedTracingConfigAbsent(t *testing.T) {
// Test that payloads do not get created or accepted when distributed
// tracing configuration is not present.
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
cfg.DistributedTracer.Enabled = true
}
app := testApp(nil, cfgFn, t)
txn := app.StartTransaction("hello")
emptyHdrs := http.Header{}
txn.InsertDistributedTraceHeaders(emptyHdrs)
if len(emptyHdrs) != 0 {
t.Error(emptyHdrs)
}
nonEmptyHdrs := http.Header{}
app2 := testApp(nil, func(cfg *Config) {
cfgFn(cfg)
cfg.ServerlessMode.AccountID = "123"
cfg.ServerlessMode.TrustedAccountKey = "trustkey"
cfg.ServerlessMode.PrimaryAppID = "456"
}, t)
app2.StartTransaction("hello").InsertDistributedTraceHeaders(nonEmptyHdrs)
if len(nonEmptyHdrs) == 0 {
t.Error(nonEmptyHdrs)
}
txn.AcceptDistributedTraceHeaders(TransportHTTP, nonEmptyHdrs)
app.expectNoLoggedErrors(t)
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "OtherTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil},
})
}
func TestServerlessLowApdex(t *testing.T) {
apdex := -1 * time.Second
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
cfg.ServerlessMode.ApdexThreshold = apdex
}
app := testApp(nil, cfgFn, t)
txn := app.StartTransaction("hello")
txn.SetWebRequestHTTP(nil) // only web gets apdex
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
// third apdex field is failed count
{Name: "Apdex", Scope: "", Forced: true, Data: []float64{0, 0, 1, apdex.Seconds(), apdex.Seconds(), 0}},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: []float64{0, 0, 1, apdex.Seconds(), apdex.Seconds(), 0}},
})
}
func TestServerlessHighApdex(t *testing.T) {
apdex := 1 * time.Hour
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
cfg.ServerlessMode.ApdexThreshold = apdex
}
app := testApp(nil, cfgFn, t)
txn := app.StartTransaction("hello")
txn.SetWebRequestHTTP(nil) // only web gets apdex
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
// first apdex field is satisfied count
{Name: "Apdex", Scope: "", Forced: true, Data: []float64{1, 0, 0, apdex.Seconds(), apdex.Seconds(), 0}},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: []float64{1, 0, 0, apdex.Seconds(), apdex.Seconds(), 0}},
})
}
func TestServerlessRecordCustomMetric(t *testing.T) {
cfgFn := func(cfg *Config) { cfg.ServerlessMode.Enabled = true }
app := testApp(nil, cfgFn, t)
app.RecordCustomMetric("myMetric", 123.0)
app.expectSingleLoggedError(t, "unable to record custom metric", map[string]interface{}{
"metric-name": "myMetric",
"reason": errMetricServerless.Error(),
})
}
func TestServerlessRecordCustomEvent(t *testing.T) {
cfgFn := func(cfg *Config) { cfg.ServerlessMode.Enabled = true }
app := testApp(nil, cfgFn, t)
attributes := map[string]interface{}{"zip": 1}
app.RecordCustomEvent("myType", attributes)
app.expectNoLoggedErrors(t)
app.ExpectCustomEvents(t, []internal.WantEvent{{
Intrinsics: map[string]interface{}{
"type": "myType",
"timestamp": internal.MatchAnything,
},
UserAttributes: attributes,
}})
buf := &bytes.Buffer{}
internal.ServerlessWrite(app.Application.Private, "my-arn", buf)
_, data, err := parseServerlessPayload(buf.Bytes())
if err != nil {
t.Fatal(err)
}
// Data should contain only custom events. Dynamic timestamp makes exact
// comparison difficult.
eventData := string(data["custom_event_data"])
if !strings.Contains(eventData, `{"zip":1}`) {
t.Error(eventData)
}
if len(data) != 1 {
t.Fatal(data)
}
}
func TestServerlessJSON(t *testing.T) {
cfgFn := func(cfg *Config) {
cfg.ServerlessMode.Enabled = true
}
app := testApp(nil, cfgFn, t)
txn := app.StartTransaction("hello")
txn.Private.(internal.AddAgentAttributer).AddAgentAttribute(AttributeAWSLambdaARN, "thearn", nil)
txn.End()
buf := &bytes.Buffer{}
internal.ServerlessWrite(app.Application.Private, "lambda-test-arn", buf)
metadata, data, err := parseServerlessPayload(buf.Bytes())
if err != nil {
t.Fatal(err)
}
// Data should contain txn event and metrics. Timestamps make exact
// JSON comparison tough.
if v := data["metric_data"]; nil == v {
t.Fatal(data)
}
if v := data["analytic_event_data"]; nil == v {
t.Fatal(data)
}
if v := string(metadata["arn"]); v != `"lambda-test-arn"` {
t.Fatal(v)
}
if v := string(metadata["agent_version"]); v != `"`+Version+`"` {
t.Fatal(v)
}
}
func TestServerlessConnectReply(t *testing.T) {
cfg := config{Config: defaultConfig()}
cfg.ServerlessMode.ApdexThreshold = 2 * time.Second
cfg.ServerlessMode.AccountID = "the-account-id"
cfg.ServerlessMode.TrustedAccountKey = "the-trust-key"
cfg.ServerlessMode.PrimaryAppID = "the-primary-app"
reply := newServerlessConnectReply(cfg)
if reply.ApdexThresholdSeconds != 2 {
t.Error(reply.ApdexThresholdSeconds)
}
if reply.AccountID != "the-account-id" {
t.Error(reply.AccountID)
}
if reply.TrustedAccountKey != "the-trust-key" {
t.Error(reply.TrustedAccountKey)
}
if reply.PrimaryAppID != "the-primary-app" {
t.Error(reply.PrimaryAppID)
}
if reply.SamplingTargetPeriodInSeconds != 60 {
t.Error(reply.SamplingTargetPeriodInSeconds)
}
if reply.SamplingTarget != 10 {
t.Error(reply.SamplingTarget)
}
// Now test the defaults:
cfg = config{Config: defaultConfig()}
reply = newServerlessConnectReply(cfg)
if reply.ApdexThresholdSeconds != 0.5 {
t.Error(reply.ApdexThresholdSeconds)
}
if reply.AccountID != "" {
t.Error(reply.AccountID)
}
if reply.TrustedAccountKey != "" {
t.Error(reply.TrustedAccountKey)
}
if reply.PrimaryAppID != "Unknown" {
t.Error(reply.PrimaryAppID)
}
}
|