forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
402 lines (326 loc) · 11.1 KB
/
manifest.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright The OpenTelemetry 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 internal // import "go.opentelemetry.io/contrib/samplers/aws/xray/internal"
import (
"context"
crypto "crypto/rand"
"fmt"
"math"
"net/url"
"reflect"
"sort"
"strings"
"sync"
"time"
"github.com/go-logr/logr"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
const manifestTTL = 3600
const version = 1
// Manifest represents a full sampling ruleset and provides
// options for configuring Logger, Clock and xrayClient.
type Manifest struct {
Rules []Rule
SamplingTargetsPollingInterval time.Duration
refreshedAt time.Time
xrayClient *xrayClient
clientID *string
logger logr.Logger
clock clock
mu sync.RWMutex
}
// NewManifest return manifest object configured the passed with logging and an xrayClient
// configured to address addr.
func NewManifest(addr url.URL, logger logr.Logger) (*Manifest, error) {
// Generate client for getSamplingRules and getSamplingTargets API call.
client, err := newClient(addr)
if err != nil {
return nil, err
}
// Generate clientID for sampling statistics.
clientID, err := generateClientID()
if err != nil {
return nil, err
}
return &Manifest{
xrayClient: client,
clock: &defaultClock{},
logger: logger,
SamplingTargetsPollingInterval: 10 * time.Second,
clientID: clientID,
}, nil
}
// Expired returns true if the manifest has not been successfully refreshed in
// manifestTTL seconds.
func (m *Manifest) Expired() bool {
m.mu.RLock()
defer m.mu.RUnlock()
manifestLiveTime := m.refreshedAt.Add(time.Second * manifestTTL)
return m.clock.now().After(manifestLiveTime)
}
// MatchAgainstManifestRules returns a Rule and boolean flag set as true
// if rule has been match against span attributes, otherwise nil and false.
func (m *Manifest) MatchAgainstManifestRules(parameters sdktrace.SamplingParameters, serviceName string, cloudPlatform string) (*Rule, bool, error) {
m.mu.RLock()
rules := m.Rules
m.mu.RUnlock()
for index := range rules {
isRuleMatch, err := rules[index].appliesTo(parameters, serviceName, cloudPlatform)
if err != nil {
return nil, isRuleMatch, err
}
if isRuleMatch {
return &rules[index], true, nil
}
}
return nil, false, nil
}
// RefreshManifestRules writes sampling rule properties to the manifest object.
func (m *Manifest) RefreshManifestRules(ctx context.Context) (err error) {
// Get sampling rules from AWS X-Ray console.
rules, err := m.xrayClient.getSamplingRules(ctx)
if err != nil {
return err
}
// Update the retrieved sampling rules to manifest object.
m.updateRules(rules)
return
}
// RefreshManifestTargets updates sampling targets (statistics) for each rule.
func (m *Manifest) RefreshManifestTargets(ctx context.Context) (refresh bool, err error) {
// Deep copy manifest object.
manifest := m.deepCopy()
// Generate sampling statistics based on the data in temporary manifest.
statistics, err := manifest.snapshots()
if err != nil {
return false, err
}
// Return if no statistics to report.
if len(statistics) == 0 {
m.logger.V(5).Info("no statistics to report and not refreshing sampling targets")
return false, nil
}
// Get sampling targets (statistics) for every expired rule from AWS X-Ray.
targets, err := m.xrayClient.getSamplingTargets(ctx, statistics)
if err != nil {
return false, fmt.Errorf("refreshTargets: error occurred while getting sampling targets: %w", err)
}
m.logger.V(5).Info("successfully fetched sampling targets")
// Update temporary manifest with retrieved targets (statistics) for each rule.
refresh, err = manifest.updateTargets(targets)
if err != nil {
return refresh, err
}
// Find next polling interval for targets.
minPoll := manifest.minimumPollingInterval()
if minPoll > 0 {
m.SamplingTargetsPollingInterval = minPoll
}
// Update centralized manifest object.
m.mu.Lock()
m.Rules = manifest.Rules
m.mu.Unlock()
return
}
func (m *Manifest) updateRules(rules *getSamplingRulesOutput) {
tempManifest := Manifest{
Rules: []Rule{},
}
for _, records := range rules.SamplingRuleRecords {
if records.SamplingRule.RuleName == "" {
m.logger.V(5).Info("sampling rule without rule name is not supported")
continue
}
if records.SamplingRule.Version != version {
m.logger.V(5).Info("sampling rule without Version 1 is not supported", "RuleName", records.SamplingRule.RuleName)
continue
}
// Create the rule and store it in temporary manifest to avoid thread safety issues.
tempManifest.createRule(*records.SamplingRule)
}
// Re-sort to fix matching priorities.
tempManifest.sort()
var currentRuleMap = make(map[string]Rule)
m.mu.Lock()
for _, rule := range m.Rules {
currentRuleMap[rule.ruleProperties.RuleName] = rule
}
for i, newRule := range tempManifest.Rules {
if curRule, ok := currentRuleMap[newRule.ruleProperties.RuleName]; ok {
if reflect.DeepEqual(newRule.ruleProperties, curRule.ruleProperties) {
tempManifest.Rules[i] = curRule
}
}
}
m.Rules = tempManifest.Rules
m.refreshedAt = m.clock.now()
m.mu.Unlock()
}
func (m *Manifest) createRule(ruleProp ruleProperties) {
cr := reservoir{
capacity: ruleProp.ReservoirSize,
}
csr := Rule{
reservoir: &cr,
ruleProperties: ruleProp,
samplingStatistics: &samplingStatistics{},
}
m.Rules = append(m.Rules, csr)
}
func (m *Manifest) updateTargets(targets *getSamplingTargetsOutput) (refresh bool, err error) {
// Update sampling targets for each rule.
for _, t := range targets.SamplingTargetDocuments {
if err := m.updateReservoir(t); err != nil {
return false, err
}
}
// Consume unprocessed statistics messages.
for _, s := range targets.UnprocessedStatistics {
m.logger.V(5).Info(
"error occurred updating sampling target for rule, code and message", "RuleName", s.RuleName, "ErrorCode",
s.ErrorCode,
"Message", s.Message,
)
// Do not set any flags if error is unknown.
if s.ErrorCode == nil || s.RuleName == nil {
continue
}
// Set batch failure if any sampling statistics returned 5xx.
if strings.HasPrefix(*s.ErrorCode, "5") {
return false, fmt.Errorf("sampling statistics returned 5xx")
}
// Set refresh flag if any sampling statistics returned 4xx.
if strings.HasPrefix(*s.ErrorCode, "4") {
refresh = true
}
}
// Set refresh flag if modifiedAt timestamp from remote is greater than ours.
if remote := targets.LastRuleModification; remote != nil {
// Convert unix timestamp to time.Time.
lastRuleModification := time.Unix(int64(*targets.LastRuleModification), 0)
if lastRuleModification.After(m.refreshedAt) {
refresh = true
}
}
return
}
func (m *Manifest) updateReservoir(t *samplingTargetDocument) (err error) {
if t.RuleName == nil {
return fmt.Errorf("invalid sampling targe: missing rule name")
}
if t.FixedRate == nil {
return fmt.Errorf("invalid sampling target for rule %s: missing fixed rate", *t.RuleName)
}
for index := range m.Rules {
if m.Rules[index].ruleProperties.RuleName == *t.RuleName {
m.Rules[index].reservoir.refreshedAt = m.clock.now()
// Update non-optional attributes from response
m.Rules[index].ruleProperties.FixedRate = *t.FixedRate
// Update optional attributes from response
if t.ReservoirQuota != nil {
m.Rules[index].reservoir.quota = *t.ReservoirQuota
}
if t.ReservoirQuotaTTL != nil {
m.Rules[index].reservoir.expiresAt = time.Unix(int64(*t.ReservoirQuotaTTL), 0)
}
if t.Interval != nil {
m.Rules[index].reservoir.interval = time.Duration(*t.Interval)
}
}
}
return
}
// snapshots takes a snapshot of sampling statistics from all rules, resetting
// statistics counters in the process.
func (m *Manifest) snapshots() ([]*samplingStatisticsDocument, error) {
statistics := make([]*samplingStatisticsDocument, 0, len(m.Rules))
// Generate sampling statistics for user-defined rules.
for index := range m.Rules {
if m.Rules[index].stale(m.clock.now()) {
s := m.Rules[index].snapshot(m.clock.now())
s.ClientID = m.clientID
statistics = append(statistics, s)
}
}
return statistics, nil
}
// deepCopy copies the m to another manifest object.
func (m *Manifest) deepCopy() *Manifest {
m.mu.RLock()
defer m.mu.RUnlock()
manifest := Manifest{
Rules: []Rule{},
}
for _, rule := range m.Rules {
// Deep copying rules.
var tempRule Rule
tempRule.ruleProperties = rule.ruleProperties
// Deep copying reservoir (copying each fields of reservoir because we want to initialize new mutex values for each rule).
var tempRes reservoir
rule.reservoir.mu.RLock()
tempRes.expiresAt = rule.reservoir.expiresAt
tempRes.quota = rule.reservoir.quota
tempRes.quotaBalance = rule.reservoir.quotaBalance
tempRes.capacity = rule.reservoir.capacity
tempRes.refreshedAt = rule.reservoir.refreshedAt
tempRes.interval = rule.reservoir.interval
tempRes.lastTick = rule.reservoir.lastTick
rule.reservoir.mu.RUnlock()
tempRule.reservoir = &tempRes
// Shallow copying sampling statistics.
tempRule.samplingStatistics = rule.samplingStatistics
manifest.Rules = append(manifest.Rules, tempRule)
}
// Copying other manifest fields.
manifest.SamplingTargetsPollingInterval = m.SamplingTargetsPollingInterval
manifest.refreshedAt = m.refreshedAt
manifest.xrayClient = m.xrayClient
manifest.clientID = m.clientID
manifest.logger = m.logger
manifest.clock = m.clock
return &manifest
}
// sort sorts the Rules of m first by priority and then by name.
func (m *Manifest) sort() { // nolint: revive // method names are scoped by receiver.
less := func(i, j int) bool {
if m.Rules[i].ruleProperties.Priority == m.Rules[j].ruleProperties.Priority {
return strings.Compare(m.Rules[i].ruleProperties.RuleName, m.Rules[j].ruleProperties.RuleName) < 0
}
return m.Rules[i].ruleProperties.Priority < m.Rules[j].ruleProperties.Priority
}
sort.Slice(m.Rules, less)
}
// minimumPollingInterval finds the minimum polling interval for all the targets of m's Rules.
func (m *Manifest) minimumPollingInterval() time.Duration {
if len(m.Rules) == 0 {
return time.Duration(0)
}
minPoll := time.Duration(math.MaxInt64)
for _, rules := range m.Rules {
if minPoll >= rules.reservoir.interval {
minPoll = rules.reservoir.interval
}
}
return minPoll * time.Second
}
// generateClientID generates random client ID.
func generateClientID() (*string, error) {
var r [12]byte
_, err := crypto.Read(r[:])
if err != nil {
return nil, fmt.Errorf("unable to generate client ID: %w", err)
}
id := fmt.Sprintf("%02x", r)
return &id, err
}