github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/connect_reply.go (about) 1 package internal 2 3 import ( 4 "encoding/json" 5 "strings" 6 "time" 7 ) 8 9 // AgentRunID identifies the current connection with the collector. 10 type AgentRunID string 11 12 func (id AgentRunID) String() string { 13 return string(id) 14 } 15 16 // PreconnectReply contains settings from the preconnect endpoint. 17 type PreconnectReply struct { 18 Collector string `json:"redirect_host"` 19 SecurityPolicies SecurityPolicies `json:"security_policies"` 20 } 21 22 // ConnectReply contains all of the settings and state send down from the 23 // collector. It should not be modified after creation. 24 type ConnectReply struct { 25 RunID AgentRunID `json:"agent_run_id"` 26 27 // Transaction Name Modifiers 28 SegmentTerms segmentRules `json:"transaction_segment_terms"` 29 TxnNameRules metricRules `json:"transaction_name_rules"` 30 URLRules metricRules `json:"url_rules"` 31 MetricRules metricRules `json:"metric_name_rules"` 32 33 // Cross Process 34 EncodingKey string `json:"encoding_key"` 35 CrossProcessID string `json:"cross_process_id"` 36 TrustedAccounts trustedAccountSet `json:"trusted_account_ids"` 37 38 // Settings 39 KeyTxnApdex map[string]float64 `json:"web_transactions_apdex"` 40 ApdexThresholdSeconds float64 `json:"apdex_t"` 41 CollectAnalyticsEvents bool `json:"collect_analytics_events"` 42 CollectCustomEvents bool `json:"collect_custom_events"` 43 CollectTraces bool `json:"collect_traces"` 44 CollectErrors bool `json:"collect_errors"` 45 CollectErrorEvents bool `json:"collect_error_events"` 46 47 // RUM 48 AgentLoader string `json:"js_agent_loader"` 49 Beacon string `json:"beacon"` 50 BrowserKey string `json:"browser_key"` 51 AppID string `json:"application_id"` 52 ErrorBeacon string `json:"error_beacon"` 53 JSAgentFile string `json:"js_agent_file"` 54 55 // PreconnectReply fields are not in the connect reply, this embedding 56 // is done to simplify code. 57 PreconnectReply `json:"-"` 58 59 Messages []struct { 60 Message string `json:"message"` 61 Level string `json:"level"` 62 } `json:"messages"` 63 } 64 65 type trustedAccountSet map[int]struct{} 66 67 func (t *trustedAccountSet) IsTrusted(account int) bool { 68 _, exists := (*t)[account] 69 return exists 70 } 71 72 func (t *trustedAccountSet) UnmarshalJSON(data []byte) error { 73 accounts := make([]int, 0) 74 if err := json.Unmarshal(data, &accounts); err != nil { 75 return err 76 } 77 78 *t = make(trustedAccountSet) 79 for _, account := range accounts { 80 (*t)[account] = struct{}{} 81 } 82 83 return nil 84 } 85 86 // ConnectReplyDefaults returns a newly allocated ConnectReply with the proper 87 // default settings. A pointer to a global is not used to prevent consumers 88 // from changing the default settings. 89 func ConnectReplyDefaults() *ConnectReply { 90 return &ConnectReply{ 91 ApdexThresholdSeconds: 0.5, 92 CollectAnalyticsEvents: true, 93 CollectCustomEvents: true, 94 CollectTraces: true, 95 CollectErrors: true, 96 CollectErrorEvents: true, 97 } 98 } 99 100 // CalculateApdexThreshold calculates the apdex threshold. 101 func CalculateApdexThreshold(c *ConnectReply, txnName string) time.Duration { 102 if t, ok := c.KeyTxnApdex[txnName]; ok { 103 return floatSecondsToDuration(t) 104 } 105 return floatSecondsToDuration(c.ApdexThresholdSeconds) 106 } 107 108 // CreateFullTxnName uses collector rules and the appropriate metric prefix to 109 // construct the full transaction metric name from the name given by the 110 // consumer. 111 func CreateFullTxnName(input string, reply *ConnectReply, isWeb bool) string { 112 var afterURLRules string 113 if "" != input { 114 afterURLRules = reply.URLRules.Apply(input) 115 if "" == afterURLRules { 116 return "" 117 } 118 } 119 120 prefix := backgroundMetricPrefix 121 if isWeb { 122 prefix = webMetricPrefix 123 } 124 125 var beforeNameRules string 126 if strings.HasPrefix(afterURLRules, "/") { 127 beforeNameRules = prefix + afterURLRules 128 } else { 129 beforeNameRules = prefix + "/" + afterURLRules 130 } 131 132 afterNameRules := reply.TxnNameRules.Apply(beforeNameRules) 133 if "" == afterNameRules { 134 return "" 135 } 136 137 return reply.SegmentTerms.apply(afterNameRules) 138 }