/
aggregation.go
91 lines (82 loc) · 2.24 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
package stats
import (
"strconv"
"strings"
"github.com/DataDog/datadog-agent/pkg/trace/pb"
"github.com/DataDog/datadog-agent/pkg/trace/traceutil"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
tagHostname = "_dd.hostname"
tagStatusCode = "http.status_code"
tagVersion = "version"
tagOrigin = "_dd.origin"
tagSynthetics = "synthetics"
)
// 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
StatusCode uint32
Synthetics bool
}
// PayloadAggregationKey specifies the key by which a payload is aggregated.
type PayloadAggregationKey struct {
Env string
Hostname string
Version string
}
func getStatusCode(s *pb.Span) uint32 {
strC := traceutil.GetMetaDefault(s, tagStatusCode, "")
if strC == "" {
return 0
}
c, err := strconv.Atoi(strC)
if err != nil {
log.Debugf("Invalid status code %s. Using 0.", strC)
return 0
}
return uint32(c)
}
// NewAggregationFromSpan creates a new aggregation from the provided span and env
func NewAggregationFromSpan(s *pb.Span, env string, agentHostname string) Aggregation {
synthetics := strings.HasPrefix(traceutil.GetMetaDefault(s, tagOrigin, ""), tagSynthetics)
hostname := traceutil.GetMetaDefault(s, tagHostname, "")
if hostname == "" {
hostname = agentHostname
}
return Aggregation{
PayloadAggregationKey: PayloadAggregationKey{
Env: env,
Hostname: hostname,
Version: traceutil.GetMetaDefault(s, tagVersion, ""),
},
BucketsAggregationKey: BucketsAggregationKey{
Resource: s.Resource,
Service: s.Service,
Name: s.Name,
Type: s.Type,
StatusCode: getStatusCode(s),
Synthetics: synthetics,
},
}
}
// 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,
StatusCode: g.HTTPStatusCode,
Synthetics: g.Synthetics,
},
}
}