/
aggregation.go
143 lines (130 loc) · 3.52 KB
/
aggregation.go
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
// 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.
//nolint:revive // TODO(APM) Fix revive linter
package stats
import (
"hash/fnv"
"sort"
"strconv"
"strings"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/DataDog/datadog-agent/pkg/trace/log"
"github.com/DataDog/datadog-agent/pkg/trace/traceutil"
)
const (
tagStatusCode = "http.status_code"
tagSynthetics = "synthetics"
tagSpanKind = "span.kind"
)
// 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
}
// 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
}
func getStatusCode(s *pb.Span) uint32 {
code, ok := traceutil.GetMetric(s, tagStatusCode)
if ok {
// only 7.39.0+, for lesser versions, always use Meta
return uint32(code)
}
strC := traceutil.GetMetaDefault(s, tagStatusCode, "")
if strC == "" {
return 0
}
c, err := strconv.ParseUint(strC, 10, 32)
if err != nil {
log.Debugf("Invalid status code %s. Using 0.", strC)
return 0
}
return uint32(c)
}
func clientOrProducer(spanKind string) bool {
sk := strings.ToLower(spanKind)
return sk == "client" || sk == "producer"
}
// NewAggregationFromSpan creates a new aggregation from the provided span and env
func NewAggregationFromSpan(s *pb.Span, origin string, aggKey PayloadAggregationKey, enablePeerTagsAgg bool, peerTagKeys []string) (Aggregation, []string) {
synthetics := strings.HasPrefix(origin, tagSynthetics)
agg := Aggregation{
PayloadAggregationKey: aggKey,
BucketsAggregationKey: BucketsAggregationKey{
Resource: s.Resource,
Service: s.Service,
Name: s.Name,
SpanKind: s.Meta[tagSpanKind],
Type: s.Type,
StatusCode: getStatusCode(s),
Synthetics: synthetics,
},
}
var peerTags []string
if clientOrProducer(agg.SpanKind) && enablePeerTagsAgg {
peerTags = matchingPeerTags(s, peerTagKeys)
agg.PeerTagsHash = peerTagsHash(peerTags)
}
return agg, peerTags
}
func matchingPeerTags(s *pb.Span, peerTagKeys []string) []string {
if len(peerTagKeys) == 0 {
return nil
}
var pt []string
for _, t := range peerTagKeys {
if v, ok := s.Meta[t]; ok && v != "" {
pt = append(pt, t+":"+v)
}
}
return pt
}
func peerTagsHash(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: peerTagsHash(g.PeerTags),
},
}
}