-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
recorder.go
145 lines (131 loc) · 5.34 KB
/
recorder.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package telemetry // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray/telemetry"
import (
"errors"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/xray"
)
type Recorder interface {
// Rotate the current record by swapping it out with a new one. Returns
// the rotated record.
Rotate() *xray.TelemetryRecord
// HasRecording indicates whether any of the record functions were called
// with the current record.
HasRecording() bool
// RecordSegmentsReceived adds the count to the current record.
RecordSegmentsReceived(count int)
// RecordSegmentsSent adds the count to the current record.
RecordSegmentsSent(count int)
// RecordSegmentsSpillover adds the count to the current record.
RecordSegmentsSpillover(count int)
// RecordSegmentsRejected adds the count to the current record.
RecordSegmentsRejected(count int)
// RecordConnectionError categorizes the error and increments the count by one
// for the current record.
RecordConnectionError(err error)
}
type telemetryRecorder struct {
// record is the pointer to the count metrics for the current period.
record *xray.TelemetryRecord
// hasRecording is set to true when any count is updated. Indicates
// that telemetry data is available.
hasRecording *atomic.Bool
}
// NewRecorder creates a new Recorder with a default interval and queue size.
func NewRecorder() Recorder {
return &telemetryRecorder{
record: NewRecord(),
hasRecording: &atomic.Bool{},
}
}
// NewRecord creates a new xray.TelemetryRecord with all of its fields initialized
// and set to 0.
func NewRecord() *xray.TelemetryRecord {
return &xray.TelemetryRecord{
SegmentsReceivedCount: aws.Int64(0),
SegmentsRejectedCount: aws.Int64(0),
SegmentsSentCount: aws.Int64(0),
SegmentsSpilloverCount: aws.Int64(0),
BackendConnectionErrors: &xray.BackendConnectionErrors{
HTTPCode4XXCount: aws.Int64(0),
HTTPCode5XXCount: aws.Int64(0),
ConnectionRefusedCount: aws.Int64(0),
OtherCount: aws.Int64(0),
TimeoutCount: aws.Int64(0),
UnknownHostCount: aws.Int64(0),
},
}
}
func (tr *telemetryRecorder) HasRecording() bool {
return tr.hasRecording.Load()
}
// Rotate the current record and swaps it out with a new record.
// Sets the timestamp and returns the old record.
func (tr *telemetryRecorder) Rotate() *xray.TelemetryRecord {
snapshot := NewRecord()
snapshot.SetSegmentsSentCount(atomic.SwapInt64(tr.record.SegmentsSentCount, 0))
snapshot.SetSegmentsReceivedCount(atomic.SwapInt64(tr.record.SegmentsReceivedCount, 0))
snapshot.SetSegmentsRejectedCount(atomic.SwapInt64(tr.record.SegmentsRejectedCount, 0))
snapshot.SetSegmentsSpilloverCount(atomic.SwapInt64(tr.record.SegmentsSpilloverCount, 0))
snapshot.BackendConnectionErrors.SetHTTPCode4XXCount(atomic.SwapInt64(tr.record.BackendConnectionErrors.HTTPCode4XXCount, 0))
snapshot.BackendConnectionErrors.SetHTTPCode5XXCount(atomic.SwapInt64(tr.record.BackendConnectionErrors.HTTPCode5XXCount, 0))
snapshot.BackendConnectionErrors.SetTimeoutCount(atomic.SwapInt64(tr.record.BackendConnectionErrors.TimeoutCount, 0))
snapshot.BackendConnectionErrors.SetConnectionRefusedCount(atomic.SwapInt64(tr.record.BackendConnectionErrors.ConnectionRefusedCount, 0))
snapshot.BackendConnectionErrors.SetUnknownHostCount(atomic.SwapInt64(tr.record.BackendConnectionErrors.UnknownHostCount, 0))
snapshot.BackendConnectionErrors.SetOtherCount(atomic.SwapInt64(tr.record.BackendConnectionErrors.OtherCount, 0))
snapshot.SetTimestamp(time.Now())
tr.hasRecording.Store(false)
return snapshot
}
func (tr *telemetryRecorder) RecordSegmentsReceived(count int) {
atomic.AddInt64(tr.record.SegmentsReceivedCount, int64(count))
tr.hasRecording.Store(true)
}
func (tr *telemetryRecorder) RecordSegmentsSent(count int) {
atomic.AddInt64(tr.record.SegmentsSentCount, int64(count))
tr.hasRecording.Store(true)
}
func (tr *telemetryRecorder) RecordSegmentsSpillover(count int) {
atomic.AddInt64(tr.record.SegmentsSpilloverCount, int64(count))
tr.hasRecording.Store(true)
}
func (tr *telemetryRecorder) RecordSegmentsRejected(count int) {
atomic.AddInt64(tr.record.SegmentsRejectedCount, int64(count))
tr.hasRecording.Store(true)
}
func (tr *telemetryRecorder) RecordConnectionError(err error) {
if err == nil {
return
}
var requestFailure awserr.RequestFailure
if ok := errors.As(err, &requestFailure); ok {
switch requestFailure.StatusCode() / 100 {
case 5:
atomic.AddInt64(tr.record.BackendConnectionErrors.HTTPCode5XXCount, 1)
case 4:
atomic.AddInt64(tr.record.BackendConnectionErrors.HTTPCode4XXCount, 1)
default:
atomic.AddInt64(tr.record.BackendConnectionErrors.OtherCount, 1)
}
} else {
var awsError awserr.Error
if ok = errors.As(err, &awsError); ok {
switch awsError.Code() {
case request.ErrCodeResponseTimeout:
atomic.AddInt64(tr.record.BackendConnectionErrors.TimeoutCount, 1)
case request.ErrCodeRequestError:
atomic.AddInt64(tr.record.BackendConnectionErrors.UnknownHostCount, 1)
default:
atomic.AddInt64(tr.record.BackendConnectionErrors.OtherCount, 1)
}
} else {
atomic.AddInt64(tr.record.BackendConnectionErrors.OtherCount, 1)
}
}
tr.hasRecording.Store(true)
}