| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package receivertest |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"strconv" |
| 9 |
"sync/atomic" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component" |
| 15 |
"go.opentelemetry.io/collector/consumer" |
| 16 |
"go.opentelemetry.io/collector/consumer/consumererror" |
| 17 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 18 |
"go.opentelemetry.io/collector/pdata/plog" |
| 19 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 20 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 21 |
"go.opentelemetry.io/collector/pipeline" |
| 22 |
"go.opentelemetry.io/collector/receiver" |
| 23 |
) |
| 24 |
|
| 25 |
// This file is an example that demonstrates how to use the CheckConsumeContract() function. |
| 26 |
// We declare a trivial example receiver, a data generator and then use them in TestConsumeContract(). |
| 27 |
|
| 28 |
type exampleReceiver struct { |
| 29 |
nextLogsConsumer consumer.Logs |
| 30 |
nextTracesConsumer consumer.Traces |
| 31 |
nextMetricsConsumer consumer.Metrics |
| 32 |
} |
| 33 |
|
| 34 |
func (s *exampleReceiver) Start(context.Context, component.Host) error { |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
func (s *exampleReceiver) Shutdown(context.Context) error { |
| 39 |
return nil |
| 40 |
} |
| 41 |
|
| 42 |
func (s *exampleReceiver) ReceiveLogs(data plog.Logs) { |
| 43 |
// This very simple implementation demonstrates how a single items receiving should happen. |
| 44 |
for { |
| 45 |
err := s.nextLogsConsumer.ConsumeLogs(context.Background(), data) |
| 46 |
if err != nil { |
| 47 |
// The next consumer returned an error. |
| 48 |
if !consumererror.IsPermanent(err) { |
| 49 |
// It is not a permanent error, so we must retry sending it again. In network-based |
| 50 |
// receivers instead we can ask our sender to re-retry the same data again later. |
| 51 |
// We may also pause here a bit if we don't want to hammer the next consumer. |
| 52 |
continue |
| 53 |
} |
| 54 |
} |
| 55 |
// If we are hear either the ConsumeLogs returned success or it returned a permanent error. |
| 56 |
// In either case we don't need to retry the same data, we are done. |
| 57 |
return |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func (s *exampleReceiver) ReceiveMetrics(data pmetric.Metrics) { |
| 62 |
// This very simple implementation demonstrates how a single items receiving should happen. |
| 63 |
for { |
| 64 |
err := s.nextMetricsConsumer.ConsumeMetrics(context.Background(), data) |
| 65 |
if err != nil { |
| 66 |
// The next consumer returned an error. |
| 67 |
if !consumererror.IsPermanent(err) { |
| 68 |
// It is not a permanent error, so we must retry sending it again. In network-based |
| 69 |
// receivers instead we can ask our sender to re-retry the same data again later. |
| 70 |
// We may also pause here a bit if we don't want to hammer the next consumer. |
| 71 |
continue |
| 72 |
} |
| 73 |
} |
| 74 |
// If we are hear either the ConsumeLogs returned success or it returned a permanent error. |
| 75 |
// In either case we don't need to retry the same data, we are done. |
| 76 |
return |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func (s *exampleReceiver) ReceiveTraces(data ptrace.Traces) { |
| 81 |
// This very simple implementation demonstrates how a single items receiving should happen. |
| 82 |
for { |
| 83 |
err := s.nextTracesConsumer.ConsumeTraces(context.Background(), data) |
| 84 |
if err != nil { |
| 85 |
// The next consumer returned an error. |
| 86 |
if !consumererror.IsPermanent(err) { |
| 87 |
// It is not a permanent error, so we must retry sending it again. In network-based |
| 88 |
// receivers instead we can ask our sender to re-retry the same data again later. |
| 89 |
// We may also pause here a bit if we don't want to hammer the next consumer. |
| 90 |
continue |
| 91 |
} |
| 92 |
} |
| 93 |
// If we are hear either the ConsumeLogs returned success or it returned a permanent error. |
| 94 |
// In either case we don't need to retry the same data, we are done. |
| 95 |
return |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// A config for exampleReceiver. |
| 100 |
type exampleReceiverConfig struct { |
| 101 |
generator Generator |
| 102 |
} |
| 103 |
|
| 104 |
// A generator that can send data to exampleReceiver. |
| 105 |
type exampleLogGenerator struct { |
| 106 |
t *testing.T |
| 107 |
receiver *exampleReceiver |
| 108 |
sequenceNum int64 |
| 109 |
} |
| 110 |
|
| 111 |
func (g *exampleLogGenerator) Start() { |
| 112 |
g.sequenceNum = 0 |
| 113 |
} |
| 114 |
|
| 115 |
func (g *exampleLogGenerator) Stop() {} |
| 116 |
|
| 117 |
func (g *exampleLogGenerator) Generate() []UniqueIDAttrVal { |
| 118 |
// Make sure the id is atomically incremented. Generate() may be called concurrently. |
| 119 |
id := UniqueIDAttrVal(strconv.FormatInt(atomic.AddInt64(&g.sequenceNum, 1), 10)) |
| 120 |
|
| 121 |
data := CreateOneLogWithID(id) |
| 122 |
|
| 123 |
// Send the generated data to the receiver. |
| 124 |
g.receiver.ReceiveLogs(data) |
| 125 |
|
| 126 |
// And return the ids for bookkeeping by the test. |
| 127 |
return []UniqueIDAttrVal{id} |
| 128 |
} |
| 129 |
|
| 130 |
// A generator that can send data to exampleReceiver. |
| 131 |
type exampleTraceGenerator struct { |
| 132 |
t *testing.T |
| 133 |
receiver *exampleReceiver |
| 134 |
sequenceNum int64 |
| 135 |
} |
| 136 |
|
| 137 |
func (g *exampleTraceGenerator) Start() { |
| 138 |
g.sequenceNum = 0 |
| 139 |
} |
| 140 |
|
| 141 |
func (g *exampleTraceGenerator) Stop() {} |
| 142 |
|
| 143 |
func (g *exampleTraceGenerator) Generate() []UniqueIDAttrVal { |
| 144 |
// Make sure the id is atomically incremented. Generate() may be called concurrently. |
| 145 |
id := UniqueIDAttrVal(strconv.FormatInt(atomic.AddInt64(&g.sequenceNum, 1), 10)) |
| 146 |
|
| 147 |
data := CreateOneSpanWithID(id) |
| 148 |
|
| 149 |
// Send the generated data to the receiver. |
| 150 |
g.receiver.ReceiveTraces(data) |
| 151 |
|
| 152 |
// And return the ids for bookkeeping by the test. |
| 153 |
return []UniqueIDAttrVal{id} |
| 154 |
} |
| 155 |
|
| 156 |
// A generator that can send data to exampleReceiver. |
| 157 |
type exampleMetricGenerator struct { |
| 158 |
t *testing.T |
| 159 |
receiver *exampleReceiver |
| 160 |
sequenceNum int64 |
| 161 |
} |
| 162 |
|
| 163 |
func (g *exampleMetricGenerator) Start() { |
| 164 |
g.sequenceNum = 0 |
| 165 |
} |
| 166 |
|
| 167 |
func (g *exampleMetricGenerator) Stop() {} |
| 168 |
|
| 169 |
func (g *exampleMetricGenerator) Generate() []UniqueIDAttrVal { |
| 170 |
// Make sure the id is atomically incremented. Generate() may be called concurrently. |
| 171 |
next := atomic.AddInt64(&g.sequenceNum, 1) |
| 172 |
id := UniqueIDAttrVal(strconv.FormatInt(next, 10)) |
| 173 |
|
| 174 |
var data pmetric.Metrics |
| 175 |
switch next % 5 { |
| 176 |
case 0: |
| 177 |
data = CreateGaugeMetricWithID(id) |
| 178 |
case 1: |
| 179 |
data = CreateSumMetricWithID(id) |
| 180 |
case 2: |
| 181 |
data = CreateSummaryMetricWithID(id) |
| 182 |
case 3: |
| 183 |
data = CreateHistogramMetricWithID(id) |
| 184 |
case 4: |
| 185 |
data = CreateExponentialHistogramMetricWithID(id) |
| 186 |
} |
| 187 |
|
| 188 |
// Send the generated data to the receiver. |
| 189 |
g.receiver.ReceiveMetrics(data) |
| 190 |
|
| 191 |
// And return the ids for bookkeeping by the test. |
| 192 |
return []UniqueIDAttrVal{id} |
| 193 |
} |
| 194 |
|
| 195 |
func newExampleFactory() receiver.Factory { |
| 196 |
return receiver.NewFactory( |
| 197 |
component.MustNewType("example_receiver"), |
| 198 |
func() component.Config { |
| 199 |
return &exampleReceiverConfig{} |
| 200 |
}, |
| 201 |
receiver.WithLogs(createLog, component.StabilityLevelBeta), |
| 202 |
receiver.WithMetrics(createMetric, component.StabilityLevelBeta), |
| 203 |
receiver.WithTraces(createTrace, component.StabilityLevelBeta), |
| 204 |
) |
| 205 |
} |
| 206 |
|
| 207 |
func createTrace(_ context.Context, _ receiver.Settings, cfg component.Config, consumer consumer.Traces) (receiver.Traces, error) { |
| 208 |
rcv := &exampleReceiver{nextTracesConsumer: consumer} |
| 209 |
cfg.(*exampleReceiverConfig).generator.(*exampleTraceGenerator).receiver = rcv |
| 210 |
return rcv, nil |
| 211 |
} |
| 212 |
|
| 213 |
func createMetric(_ context.Context, _ receiver.Settings, cfg component.Config, consumer consumer.Metrics) (receiver.Metrics, error) { |
| 214 |
rcv := &exampleReceiver{nextMetricsConsumer: consumer} |
| 215 |
cfg.(*exampleReceiverConfig).generator.(*exampleMetricGenerator).receiver = rcv |
| 216 |
return rcv, nil |
| 217 |
} |
| 218 |
|
| 219 |
func createLog( |
| 220 |
_ context.Context, |
| 221 |
_ receiver.Settings, |
| 222 |
cfg component.Config, |
| 223 |
consumer consumer.Logs, |
| 224 |
) (receiver.Logs, error) { |
| 225 |
rcv := &exampleReceiver{nextLogsConsumer: consumer} |
| 226 |
cfg.(*exampleReceiverConfig).generator.(*exampleLogGenerator).receiver = rcv |
| 227 |
return rcv, nil |
| 228 |
} |
| 229 |
|
| 230 |
// TestConsumeContract is an example of testing of the receiver for the contract between the |
| 231 |
// receiver and next consumer. |
| 232 |
func TestConsumeContract(t *testing.T) { |
| 233 |
// Number of log records to send per scenario. |
| 234 |
const logsPerTest = 100 |
| 235 |
|
| 236 |
generator := &exampleLogGenerator{t: t} |
| 237 |
cfg := &exampleReceiverConfig{generator: generator} |
| 238 |
|
| 239 |
params := CheckConsumeContractParams{ |
| 240 |
T: t, |
| 241 |
Factory: newExampleFactory(), |
| 242 |
Signal: pipeline.SignalLogs, |
| 243 |
Config: cfg, |
| 244 |
Generator: generator, |
| 245 |
GenerateCount: logsPerTest, |
| 246 |
} |
| 247 |
|
| 248 |
// Run the contract checker. This will trigger test failures if any problems are found. |
| 249 |
CheckConsumeContract(params) |
| 250 |
} |
| 251 |
|
| 252 |
// TestConsumeMetricsContract is an example of testing of the receiver for the contract between the |
| 253 |
// receiver and next consumer. |
| 254 |
func TestConsumeMetricsContract(t *testing.T) { |
| 255 |
// Number of metric data points to send per scenario. |
| 256 |
const metricsPerTest = 100 |
| 257 |
|
| 258 |
generator := &exampleMetricGenerator{t: t} |
| 259 |
cfg := &exampleReceiverConfig{generator: generator} |
| 260 |
|
| 261 |
params := CheckConsumeContractParams{ |
| 262 |
T: t, |
| 263 |
Factory: newExampleFactory(), |
| 264 |
Signal: pipeline.SignalMetrics, |
| 265 |
Config: cfg, |
| 266 |
Generator: generator, |
| 267 |
GenerateCount: metricsPerTest, |
| 268 |
} |
| 269 |
|
| 270 |
// Run the contract checker. This will trigger test failures if any problems are found. |
| 271 |
CheckConsumeContract(params) |
| 272 |
} |
| 273 |
|
| 274 |
// TestConsumeTracesContract is an example of testing of the receiver for the contract between the |
| 275 |
// receiver and next consumer. |
| 276 |
func TestConsumeTracesContract(t *testing.T) { |
| 277 |
// Number of trace spans to send per scenario. |
| 278 |
const spansPerTest = 100 |
| 279 |
|
| 280 |
generator := &exampleTraceGenerator{t: t} |
| 281 |
cfg := &exampleReceiverConfig{generator: generator} |
| 282 |
|
| 283 |
params := CheckConsumeContractParams{ |
| 284 |
T: t, |
| 285 |
Factory: newExampleFactory(), |
| 286 |
Signal: pipeline.SignalTraces, |
| 287 |
Config: cfg, |
| 288 |
Generator: generator, |
| 289 |
GenerateCount: spansPerTest, |
| 290 |
} |
| 291 |
|
| 292 |
// Run the contract checker. This will trigger test failures if any problems are found. |
| 293 |
CheckConsumeContract(params) |
| 294 |
} |
| 295 |
|
| 296 |
func TestIDSetFromDataPoint(t *testing.T) { |
| 297 |
require.Error(t, idSetFromDataPoint(map[UniqueIDAttrVal]bool{}, pcommon.NewMap())) |
| 298 |
m := pcommon.NewMap() |
| 299 |
m.PutStr("foo", "bar") |
| 300 |
require.Error(t, idSetFromDataPoint(map[UniqueIDAttrVal]bool{}, m)) |
| 301 |
m.PutInt(UniqueIDAttrName, 64) |
| 302 |
require.Error(t, idSetFromDataPoint(map[UniqueIDAttrVal]bool{}, m)) |
| 303 |
m.PutStr(UniqueIDAttrName, "myid") |
| 304 |
result := map[UniqueIDAttrVal]bool{} |
| 305 |
require.NoError(t, idSetFromDataPoint(result, m)) |
| 306 |
require.True(t, result["myid"]) |
| 307 |
} |
| 308 |
|
| 309 |
func TestBadMetricPoint(t *testing.T) { |
| 310 |
for _, test := range []struct { |
| 311 |
name string |
| 312 |
metrics pmetric.Metrics |
| 313 |
}{ |
| 314 |
{ |
| 315 |
name: "gauge", |
| 316 |
metrics: func() pmetric.Metrics { |
| 317 |
m := pmetric.NewMetrics() |
| 318 |
m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty() |
| 319 |
return m |
| 320 |
}(), |
| 321 |
}, |
| 322 |
{ |
| 323 |
name: "sum", |
| 324 |
metrics: func() pmetric.Metrics { |
| 325 |
m := pmetric.NewMetrics() |
| 326 |
m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty() |
| 327 |
return m |
| 328 |
}(), |
| 329 |
}, |
| 330 |
{ |
| 331 |
name: "summary", |
| 332 |
metrics: func() pmetric.Metrics { |
| 333 |
m := pmetric.NewMetrics() |
| 334 |
m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySummary().DataPoints().AppendEmpty() |
| 335 |
return m |
| 336 |
}(), |
| 337 |
}, |
| 338 |
{ |
| 339 |
name: "histogram", |
| 340 |
metrics: func() pmetric.Metrics { |
| 341 |
m := pmetric.NewMetrics() |
| 342 |
m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyHistogram().DataPoints().AppendEmpty() |
| 343 |
return m |
| 344 |
}(), |
| 345 |
}, |
| 346 |
{ |
| 347 |
name: "exponential histogram", |
| 348 |
metrics: func() pmetric.Metrics { |
| 349 |
m := pmetric.NewMetrics() |
| 350 |
m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyExponentialHistogram().DataPoints().AppendEmpty() |
| 351 |
return m |
| 352 |
}(), |
| 353 |
}, |
| 354 |
} { |
| 355 |
t.Run(test.name, func(t *testing.T) { |
| 356 |
_, err := idSetFromMetrics(test.metrics) |
| 357 |
require.Error(t, err) |
| 358 |
}) |
| 359 |
} |
| 360 |
} |