github.com/newrelic/go-agent@v3.26.0+incompatible/internal/integrationsupport/integrationsupport.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 // Package integrationsupport exists to expose functionality to integration 5 // packages without adding noise to the public API. 6 package integrationsupport 7 8 import ( 9 newrelic "github.com/newrelic/go-agent" 10 "github.com/newrelic/go-agent/internal" 11 ) 12 13 // AddAgentAttribute allows instrumentation packages to add agent attributes. 14 func AddAgentAttribute(txn newrelic.Transaction, id internal.AgentAttributeID, stringVal string, otherVal interface{}) { 15 if aa, ok := txn.(internal.AddAgentAttributer); ok { 16 aa.AddAgentAttribute(id, stringVal, otherVal) 17 } 18 } 19 20 // AddAgentSpanAttribute allows instrumentation packages to add span attributes. 21 func AddAgentSpanAttribute(txn newrelic.Transaction, key internal.SpanAttribute, val string) { 22 internal.AddAgentSpanAttribute(txn, key, val) 23 } 24 25 // This code below is used for testing and is based on the similar code in internal_test.go in 26 // the newrelic package. That code is not exported, though, and we frequently need something similar 27 // for integration packages, so it is copied here. 28 const ( 29 testLicenseKey = "0123456789012345678901234567890123456789" 30 SampleAppName = "my app" 31 ) 32 33 // ExpectApp combines Application and Expect, for use in validating data in test apps 34 type ExpectApp interface { 35 internal.Expect 36 newrelic.Application 37 } 38 39 // NewTestApp creates an ExpectApp with the given ConnectReply function and Config function 40 func NewTestApp(replyfn func(*internal.ConnectReply), cfgFn func(*newrelic.Config)) ExpectApp { 41 42 cfg := newrelic.NewConfig(SampleAppName, testLicenseKey) 43 44 if nil != cfgFn { 45 cfgFn(&cfg) 46 } 47 48 // Prevent spawning app goroutines in tests. 49 if !cfg.ServerlessMode.Enabled { 50 cfg.Enabled = false 51 } 52 53 app, err := newrelic.NewApplication(cfg) 54 if nil != err { 55 panic(err) 56 } 57 58 internal.HarvestTesting(app, replyfn) 59 60 return app.(ExpectApp) 61 } 62 63 // NewBasicTestApp creates an ExpectApp with the standard testing connect reply function and config 64 func NewBasicTestApp() ExpectApp { 65 return NewTestApp(nil, BasicConfigFn) 66 } 67 68 // BasicConfigFn is a default config function to be used when no special settings are needed for a test app 69 var BasicConfigFn = func(cfg *newrelic.Config) { 70 cfg.Enabled = false 71 } 72 73 // DTEnabledCfgFn is a reusable Config function that sets Distributed Tracing to enabled 74 var DTEnabledCfgFn = func(cfg *newrelic.Config) { 75 cfg.Enabled = false 76 cfg.DistributedTracer.Enabled = true 77 } 78 79 // SampleEverythingReplyFn is a reusable ConnectReply function that samples everything 80 var SampleEverythingReplyFn = func(reply *internal.ConnectReply) { 81 reply.AdaptiveSampler = internal.SampleEverything{} 82 }