forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsink.go
More file actions
167 lines (134 loc) · 4.06 KB
/
sink.go
File metadata and controls
167 lines (134 loc) · 4.06 KB
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
// 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 consumertest
import (
"context"
"sync"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/model/pdata"
)
// TracesSink is a consumer.Traces that acts like a sink that
// stores all traces and allows querying them for testing.
type TracesSink struct {
nonMutatingConsumer
mu sync.Mutex
traces []pdata.Traces
spansCount int
}
var _ consumer.Traces = (*TracesSink)(nil)
// ConsumeTraces stores traces to this sink.
func (ste *TracesSink) ConsumeTraces(_ context.Context, td pdata.Traces) error {
ste.mu.Lock()
defer ste.mu.Unlock()
ste.traces = append(ste.traces, td)
ste.spansCount += td.SpanCount()
return nil
}
// AllTraces returns the traces stored by this sink since last Reset.
func (ste *TracesSink) AllTraces() []pdata.Traces {
ste.mu.Lock()
defer ste.mu.Unlock()
copyTraces := make([]pdata.Traces, len(ste.traces))
copy(copyTraces, ste.traces)
return copyTraces
}
// SpansCount returns the number of spans sent to this sink.
func (ste *TracesSink) SpansCount() int {
ste.mu.Lock()
defer ste.mu.Unlock()
return ste.spansCount
}
// Reset deletes any stored data.
func (ste *TracesSink) Reset() {
ste.mu.Lock()
defer ste.mu.Unlock()
ste.traces = nil
ste.spansCount = 0
}
// MetricsSink is a consumer.Metrics that acts like a sink that
// stores all metrics and allows querying them for testing.
type MetricsSink struct {
nonMutatingConsumer
mu sync.Mutex
metrics []pdata.Metrics
metricsCount int
}
var _ consumer.Metrics = (*MetricsSink)(nil)
// ConsumeMetrics stores metrics to this sink.
func (sme *MetricsSink) ConsumeMetrics(_ context.Context, md pdata.Metrics) error {
sme.mu.Lock()
defer sme.mu.Unlock()
sme.metrics = append(sme.metrics, md)
sme.metricsCount += md.MetricCount()
return nil
}
// AllMetrics returns the metrics stored by this sink since last Reset.
func (sme *MetricsSink) AllMetrics() []pdata.Metrics {
sme.mu.Lock()
defer sme.mu.Unlock()
copyMetrics := make([]pdata.Metrics, len(sme.metrics))
copy(copyMetrics, sme.metrics)
return copyMetrics
}
// MetricsCount returns the number of metrics stored by this sink since last Reset.
func (sme *MetricsSink) MetricsCount() int {
sme.mu.Lock()
defer sme.mu.Unlock()
return sme.metricsCount
}
// Reset deletes any stored data.
func (sme *MetricsSink) Reset() {
sme.mu.Lock()
defer sme.mu.Unlock()
sme.metrics = nil
sme.metricsCount = 0
}
// LogsSink is a consumer.Logs that acts like a sink that
// stores all logs and allows querying them for testing.
type LogsSink struct {
nonMutatingConsumer
mu sync.Mutex
logs []pdata.Logs
logRecordsCount int
}
var _ consumer.Logs = (*LogsSink)(nil)
// ConsumeLogs stores logs to this sink.
func (sle *LogsSink) ConsumeLogs(_ context.Context, ld pdata.Logs) error {
sle.mu.Lock()
defer sle.mu.Unlock()
sle.logs = append(sle.logs, ld)
sle.logRecordsCount += ld.LogRecordCount()
return nil
}
// AllLogs returns the logs stored by this sink since last Reset.
func (sle *LogsSink) AllLogs() []pdata.Logs {
sle.mu.Lock()
defer sle.mu.Unlock()
copyLogs := make([]pdata.Logs, len(sle.logs))
copy(copyLogs, sle.logs)
return copyLogs
}
// LogRecordsCount returns the number of log records stored by this sink since last Reset.
func (sle *LogsSink) LogRecordsCount() int {
sle.mu.Lock()
defer sle.mu.Unlock()
return sle.logRecordsCount
}
// Reset deletes any stored data.
func (sle *LogsSink) Reset() {
sle.mu.Lock()
defer sle.mu.Unlock()
sle.logs = nil
sle.logRecordsCount = 0
}