-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathaggregation.go
More file actions
229 lines (210 loc) · 6.65 KB
/
aggregation.go
File metadata and controls
229 lines (210 loc) · 6.65 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package stats contains the logic to process APM stats.
package stats
import (
"hash/fnv"
"math"
"sort"
"strconv"
"strings"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace/idx"
"github.com/DataDog/datadog-agent/pkg/trace/semantics"
"google.golang.org/genproto/googleapis/rpc/code"
)
var ddRegistry = semantics.DefaultRegistry()
const (
tagSynthetics = "synthetics"
tagServiceSource = "_dd.svc_src"
)
// Aggregation contains all the dimension on which we aggregate statistics.
type Aggregation struct {
BucketsAggregationKey
PayloadAggregationKey
}
// BucketsAggregationKey specifies the key by which a bucket is aggregated.
type BucketsAggregationKey struct {
Service string
Name string
Resource string
Type string
SpanKind string
StatusCode uint32
Synthetics bool
PeerTagsHash uint64
AdditionalMetricTagsHash uint64
ServiceSource string
IsTraceRoot pb.Trilean
GRPCStatusCode string
HTTPMethod string
HTTPEndpoint string
}
// PayloadAggregationKey specifies the key by which a payload is aggregated.
type PayloadAggregationKey struct {
Env string
Hostname string
Version string
ContainerID string
GitCommitSha string
ImageTag string
Lang string
ProcessTagsHash uint64
BaseService string
}
func toStatusCode(v int64) (uint32, bool) {
if v < 0 || v > math.MaxUint32 {
return 0, false
}
return uint32(v), true
}
func getStatusCode(meta map[string]string, metrics map[string]float64) uint32 {
a := semantics.NewDDSpanAccessor(meta, metrics)
v, ok := semantics.LookupInt64(ddRegistry, a, semantics.ConceptHTTPStatusCode)
if !ok {
return 0
}
code, ok := toStatusCode(v)
if !ok {
return 0
}
return code
}
func getStatusCodeV1(s *idx.InternalSpan) uint32 {
a := semantics.NewDDSpanAccessorV1(s)
v, ok := semantics.LookupInt64(ddRegistry, a, semantics.ConceptHTTPStatusCode)
if !ok {
return 0
}
code, ok := toStatusCode(v)
if !ok {
return 0
}
return code
}
// NewAggregationFromSpan creates a new aggregation from the provided span and env
func NewAggregationFromSpan(s *StatSpan, origin string, aggKey PayloadAggregationKey) Aggregation {
synthetics := strings.HasPrefix(origin, tagSynthetics)
var isTraceRoot pb.Trilean
if s.parentID == 0 {
isTraceRoot = pb.Trilean_TRUE
} else {
isTraceRoot = pb.Trilean_FALSE
}
agg := Aggregation{
PayloadAggregationKey: aggKey,
BucketsAggregationKey: BucketsAggregationKey{
Resource: s.resource,
Service: s.service,
Name: s.name,
SpanKind: s.spanKind,
Type: s.typ,
StatusCode: s.statusCode,
ServiceSource: s.serviceSource,
Synthetics: synthetics,
IsTraceRoot: isTraceRoot,
GRPCStatusCode: s.grpcStatusCode,
PeerTagsHash: tagsFnvHash(s.matchingPeerTags),
AdditionalMetricTagsHash: tagsFnvHash(s.matchingAdditionalMetricTags),
HTTPMethod: s.httpMethod,
HTTPEndpoint: s.httpEndpoint,
},
}
return agg
}
func processTagsHash(processTags string) uint64 {
if processTags == "" {
return 0
}
return tagsFnvHash(strings.Split(processTags, ","))
}
func tagsFnvHash(tags []string) uint64 {
if len(tags) == 0 {
return 0
}
if !sort.StringsAreSorted(tags) {
sort.Strings(tags)
}
h := fnv.New64a()
for i, t := range tags {
if i > 0 {
h.Write([]byte{0})
}
h.Write([]byte(t))
}
return h.Sum64()
}
// NewAggregationFromGroup gets the Aggregation key of grouped stats.
func NewAggregationFromGroup(g *pb.ClientGroupedStats) Aggregation {
return Aggregation{
BucketsAggregationKey: BucketsAggregationKey{
Resource: g.Resource,
Service: g.Service,
Name: g.Name,
SpanKind: g.SpanKind,
StatusCode: g.HTTPStatusCode,
Synthetics: g.Synthetics,
PeerTagsHash: tagsFnvHash(g.PeerTags),
AdditionalMetricTagsHash: tagsFnvHash(g.AdditionalMetricTags),
ServiceSource: g.ServiceSource,
IsTraceRoot: g.IsTraceRoot,
GRPCStatusCode: g.GRPCStatusCode,
HTTPMethod: g.HTTPMethod,
HTTPEndpoint: g.HTTPEndpoint,
},
}
}
/*
The gRPC codes Google API checks for "CANCELLED". Sometimes we receive "Canceled" from upstream,
sometimes "CANCELLED", which is why both spellings appear in the map.
For multi-word codes, sometimes from upstream we receive them as one word, such as DeadlineExceeded.
Google's API checks for strings with an underscore and in all caps, and would only recognize codes
formatted like "ALREADY_EXISTS" or "DEADLINE_EXCEEDED"
*/
var grpcStatusMap = map[string]string{
"CANCELLED": "1",
"CANCELED": "1",
"INVALIDARGUMENT": "3",
"DEADLINEEXCEEDED": "4",
"NOTFOUND": "5",
"ALREADYEXISTS": "6",
"PERMISSIONDENIED": "7",
"RESOURCEEXHAUSTED": "8",
"FAILEDPRECONDITION": "9",
"OUTOFRANGE": "11",
"DATALOSS": "15",
}
// parseGRPCStatusString converts a raw gRPC status string (numeric, enum name, or
// "StatusCode."-prefixed) to its canonical numeric string form, or "" if unrecognized.
func parseGRPCStatusString(strC string) string {
if c, err := strconv.ParseUint(strC, 10, 32); err == nil {
return strconv.FormatUint(c, 10)
}
strC = strings.TrimPrefix(strC, "StatusCode.") // Some tracers send status code values prefixed by "StatusCode."
strCUpper := strings.ToUpper(strC)
if statusCode, exists := grpcStatusMap[strCUpper]; exists {
return statusCode
}
if codeNum, found := code.Code_value[strCUpper]; found {
return strconv.Itoa(int(codeNum))
}
return ""
}
func getGRPCStatusCode(meta map[string]string, metrics map[string]float64) string {
a := semantics.NewDDSpanAccessor(meta, metrics)
strC := semantics.LookupString(ddRegistry, a, semantics.ConceptGRPCStatusCode)
if strC == "" {
return ""
}
return parseGRPCStatusString(strC)
}
func getGRPCStatusCodeV1(s *idx.InternalSpan) string {
a := semantics.NewDDSpanAccessorV1(s)
strC := semantics.LookupString(ddRegistry, a, semantics.ConceptGRPCStatusCode)
if strC == "" {
return ""
}
return parseGRPCStatusString(strC)
}