-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
aggregator.go
163 lines (146 loc) · 5.12 KB
/
aggregator.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2021 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adaptive
import (
"sync"
"time"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/cmd/collector/app/sampling/model"
"github.com/jaegertracing/jaeger/cmd/collector/app/sampling/strategystore"
span_model "github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/pkg/hostname"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/plugin/sampling/leaderelection"
"github.com/jaegertracing/jaeger/storage/samplingstore"
)
const (
maxProbabilities = 10
)
type aggregator struct {
sync.Mutex
operationsCounter metrics.Counter
servicesCounter metrics.Counter
currentThroughput serviceOperationThroughput
postAggregator *PostAggregator
aggregationInterval time.Duration
storage samplingstore.Store
stop chan struct{}
bgFinished sync.WaitGroup
}
// NewAggregator creates a throughput aggregator that simply emits metrics
// about the number of operations seen over the aggregationInterval.
func NewAggregator(options Options, logger *zap.Logger, metricsFactory metrics.Factory, participant leaderelection.ElectionParticipant, store samplingstore.Store) (strategystore.Aggregator, error) {
hostname, err := hostname.AsIdentifier()
if err != nil {
return nil, err
}
logger.Info("Using unique participantName in adaptive sampling", zap.String("participantName", hostname))
postAggregator, err := newPostAggregator(options, hostname, store, participant, metricsFactory, logger)
if err != nil {
return nil, err
}
return &aggregator{
operationsCounter: metricsFactory.Counter(metrics.Options{Name: "sampling_operations"}),
servicesCounter: metricsFactory.Counter(metrics.Options{Name: "sampling_services"}),
currentThroughput: make(serviceOperationThroughput),
aggregationInterval: options.CalculationInterval,
postAggregator: postAggregator,
storage: store,
stop: make(chan struct{}),
}, nil
}
func (a *aggregator) runAggregationLoop() {
ticker := time.NewTicker(a.aggregationInterval)
for {
select {
case <-ticker.C:
a.Lock()
a.saveThroughput()
a.currentThroughput = make(serviceOperationThroughput)
a.postAggregator.runCalculation()
a.Unlock()
case <-a.stop:
ticker.Stop()
return
}
}
}
func (a *aggregator) saveThroughput() {
totalOperations := 0
var throughputSlice []*model.Throughput
for _, opThroughput := range a.currentThroughput {
totalOperations += len(opThroughput)
for _, throughput := range opThroughput {
throughputSlice = append(throughputSlice, throughput)
}
}
a.operationsCounter.Inc(int64(totalOperations))
a.servicesCounter.Inc(int64(len(a.currentThroughput)))
a.storage.InsertThroughput(throughputSlice)
}
func (a *aggregator) RecordThroughput(service, operation string, samplerType span_model.SamplerType, probability float64) {
a.Lock()
defer a.Unlock()
if _, ok := a.currentThroughput[service]; !ok {
a.currentThroughput[service] = make(map[string]*model.Throughput)
}
throughput, ok := a.currentThroughput[service][operation]
if !ok {
throughput = &model.Throughput{
Service: service,
Operation: operation,
Probabilities: make(map[string]struct{}),
}
a.currentThroughput[service][operation] = throughput
}
probStr := TruncateFloat(probability)
if len(throughput.Probabilities) != maxProbabilities {
throughput.Probabilities[probStr] = struct{}{}
}
// Only if we see probabilistically sampled root spans do we increment the throughput counter,
// for lowerbound sampled spans, we don't increment at all but we still save a count of 0 as
// the throughput so that the adaptive sampling processor is made aware of the endpoint.
if samplerType == span_model.SamplerTypeProbabilistic {
throughput.Count++
}
}
func (a *aggregator) Start() {
a.postAggregator.Start()
a.bgFinished.Add(1)
go func() {
a.runAggregationLoop()
a.bgFinished.Done()
}()
}
func (a *aggregator) Close() error {
close(a.stop)
a.bgFinished.Wait()
return nil
}
func (a *aggregator) HandleRootSpan(span *span_model.Span, logger *zap.Logger) {
// simply checking parentId to determine if a span is a root span is not sufficient. However,
// we can be sure that only a root span will have sampler tags.
if span.ParentSpanID() != span_model.NewSpanID(0) {
return
}
service := span.Process.ServiceName
if service == "" || span.OperationName == "" {
return
}
samplerType, samplerParam := span.GetSamplerParams(logger)
if samplerType == span_model.SamplerTypeUnrecognized {
return
}
a.RecordThroughput(service, span.OperationName, samplerType, samplerParam)
}