|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package receiver_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "time" |
| 10 | + |
| 11 | + "go.opentelemetry.io/collector/component" |
| 12 | + "go.opentelemetry.io/collector/consumer" |
| 13 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 14 | + "go.opentelemetry.io/collector/pdata/ptrace" |
| 15 | + "go.opentelemetry.io/collector/receiver" |
| 16 | +) |
| 17 | + |
| 18 | +var typeStr = component.MustNewType("example") |
| 19 | + |
| 20 | +type exampleConfig struct { |
| 21 | + Interval time.Duration |
| 22 | +} |
| 23 | + |
| 24 | +// Reciver must implement the Start() and Shutdown() methods so the receiver type can be compliant |
| 25 | +// with the receiver.Traces interface. |
| 26 | +type exampleReceiver struct { |
| 27 | + host component.Host |
| 28 | + cancel context.CancelFunc |
| 29 | + nextConsumer consumer.Traces |
| 30 | + config exampleConfig |
| 31 | +} |
| 32 | + |
| 33 | +func (rcvr *exampleReceiver) Start(ctx context.Context, host component.Host) error { |
| 34 | + rcvr.host = host |
| 35 | + ctx, rcvr.cancel = context.WithCancel(ctx) |
| 36 | + |
| 37 | + go func() { |
| 38 | + ticker := time.NewTicker(rcvr.config.Interval) |
| 39 | + defer ticker.Stop() |
| 40 | + |
| 41 | + for { |
| 42 | + select { |
| 43 | + case <-ticker.C: |
| 44 | + // Your receiver processing code should come here |
| 45 | + err := rcvr.nextConsumer.ConsumeTraces(ctx, generateTrace()) |
| 46 | + if err != nil { |
| 47 | + fmt.Println("Error when consuming trace: %w", err) |
| 48 | + } |
| 49 | + case <-ctx.Done(): |
| 50 | + return |
| 51 | + } |
| 52 | + } |
| 53 | + }() |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (rcvr *exampleReceiver) Shutdown(_ context.Context) error { |
| 59 | + if rcvr.cancel != nil { |
| 60 | + rcvr.cancel() |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func generateTrace() ptrace.Traces { |
| 66 | + traces := ptrace.NewTraces() |
| 67 | + |
| 68 | + // In reallity you may need to fetch or receive and transform |
| 69 | + // some telemetry data. |
| 70 | + // For this example we will just generate some dummy data |
| 71 | + resourceSpan := traces.ResourceSpans().AppendEmpty() |
| 72 | + resource := resourceSpan.Resource() |
| 73 | + |
| 74 | + attrs := resource.Attributes() |
| 75 | + attrs.PutInt("id", 1) |
| 76 | + |
| 77 | + scopeSpans := resourceSpan.ScopeSpans().AppendEmpty() |
| 78 | + scopeSpans.Scope().SetName("example-system") |
| 79 | + scopeSpans.Scope().SetVersion("v1.0") |
| 80 | + |
| 81 | + traceID := pcommon.TraceID([]byte("1")) |
| 82 | + spanID := pcommon.SpanID([]byte("2")) |
| 83 | + |
| 84 | + span := scopeSpans.Spans().AppendEmpty() |
| 85 | + span.SetTraceID(traceID) |
| 86 | + span.SetSpanID(spanID) |
| 87 | + span.SetName("Operation 1") |
| 88 | + span.SetKind(ptrace.SpanKindClient) |
| 89 | + span.Status().SetCode(ptrace.StatusCodeOk) |
| 90 | + span.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now())) |
| 91 | + span.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(4 * time.Millisecond))) |
| 92 | + |
| 93 | + // Other resources and spans could also be added. |
| 94 | + |
| 95 | + return traces |
| 96 | +} |
| 97 | + |
| 98 | +func createDefaultConfig() component.Config { |
| 99 | + return &exampleConfig{ |
| 100 | + Interval: time.Minute, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func createExampleReceiver(_ context.Context, _ receiver.Settings, |
| 105 | + baseCfg component.Config, consumer consumer.Traces, |
| 106 | +) (receiver.Traces, error) { |
| 107 | + exampleCfg := baseCfg.(*exampleConfig) |
| 108 | + |
| 109 | + rcvr := &exampleReceiver{ |
| 110 | + nextConsumer: consumer, |
| 111 | + config: *exampleCfg, |
| 112 | + } |
| 113 | + |
| 114 | + return rcvr, nil |
| 115 | +} |
| 116 | + |
| 117 | +func NewFactory() receiver.Factory { |
| 118 | + return receiver.NewFactory( |
| 119 | + typeStr, |
| 120 | + createDefaultConfig, |
| 121 | + receiver.WithTraces(createExampleReceiver, component.StabilityLevelAlpha)) |
| 122 | +} |
| 123 | + |
| 124 | +func Example() { |
| 125 | + // The NewFactory method can then be used on the Collector's initialization process |
| 126 | + // on your components.go file. |
| 127 | + |
| 128 | + // In this example we will just instantiate and print it's type |
| 129 | + |
| 130 | + exampleReceiver := NewFactory() |
| 131 | + fmt.Println(exampleReceiver.Type()) |
| 132 | + |
| 133 | + // Output: |
| 134 | + // example |
| 135 | +} |
0 commit comments