@@ -6,13 +6,19 @@ package processorhelper
6
6
import (
7
7
"context"
8
8
"errors"
9
+ "strings"
9
10
"testing"
10
11
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
14
+ "go.opentelemetry.io/otel/attribute"
15
+ sdkmetric "go.opentelemetry.io/otel/sdk/metric"
16
+ "go.opentelemetry.io/otel/sdk/metric/metricdata"
17
+ "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
13
18
14
19
"go.opentelemetry.io/collector/component"
15
20
"go.opentelemetry.io/collector/component/componenttest"
21
+ "go.opentelemetry.io/collector/config/configtelemetry"
16
22
"go.opentelemetry.io/collector/consumer"
17
23
"go.opentelemetry.io/collector/consumer/consumertest"
18
24
"go.opentelemetry.io/collector/pdata/plog"
@@ -67,3 +73,72 @@ func newTestLProcessor(retError error) ProcessLogsFunc {
67
73
return ld , retError
68
74
}
69
75
}
76
+
77
+ func TestLogsProcessor_RecordInOut (t * testing.T ) {
78
+ // Regardless of how many logs are ingested, emit just one
79
+ mockAggregate := func (_ context.Context , _ plog.Logs ) (plog.Logs , error ) {
80
+ ld := plog .NewLogs ()
81
+ ld .ResourceLogs ().AppendEmpty ().ScopeLogs ().AppendEmpty ().LogRecords ().AppendEmpty ()
82
+ return ld , nil
83
+ }
84
+
85
+ incomingLogs := plog .NewLogs ()
86
+ incomingLogRecords := incomingLogs .ResourceLogs ().AppendEmpty ().ScopeLogs ().AppendEmpty ().LogRecords ()
87
+
88
+ // Add 3 records to the incoming
89
+ incomingLogRecords .AppendEmpty ()
90
+ incomingLogRecords .AppendEmpty ()
91
+ incomingLogRecords .AppendEmpty ()
92
+
93
+ metricReader := sdkmetric .NewManualReader ()
94
+ set := processortest .NewNopSettings ()
95
+ set .TelemetrySettings .MetricsLevel = configtelemetry .LevelNormal
96
+ set .TelemetrySettings .MeterProvider = sdkmetric .NewMeterProvider (sdkmetric .WithReader (metricReader ))
97
+
98
+ lp , err := NewLogsProcessor (context .Background (), set , & testLogsCfg , consumertest .NewNop (), mockAggregate )
99
+ require .NoError (t , err )
100
+
101
+ assert .NoError (t , lp .Start (context .Background (), componenttest .NewNopHost ()))
102
+ assert .NoError (t , lp .ConsumeLogs (context .Background (), incomingLogs ))
103
+ assert .NoError (t , lp .Shutdown (context .Background ()))
104
+
105
+ ownMetrics := new (metricdata.ResourceMetrics )
106
+ require .NoError (t , metricReader .Collect (context .Background (), ownMetrics ))
107
+
108
+ require .Len (t , ownMetrics .ScopeMetrics , 1 )
109
+ require .Len (t , ownMetrics .ScopeMetrics [0 ].Metrics , 2 )
110
+
111
+ inMetric := ownMetrics .ScopeMetrics [0 ].Metrics [0 ]
112
+ outMetric := ownMetrics .ScopeMetrics [0 ].Metrics [1 ]
113
+ if strings .Contains (inMetric .Name , "outgoing" ) {
114
+ inMetric , outMetric = outMetric , inMetric
115
+ }
116
+
117
+ metricdatatest .AssertAggregationsEqual (t , metricdata.Sum [int64 ]{
118
+ Temporality : metricdata .CumulativeTemporality ,
119
+ IsMonotonic : true ,
120
+ DataPoints : []metricdata.DataPoint [int64 ]{
121
+ {
122
+ Attributes : attribute .NewSet (attribute.KeyValue {
123
+ Key : attribute .Key ("processor" ),
124
+ Value : attribute .StringValue (set .ID .String ()),
125
+ }),
126
+ Value : 3 ,
127
+ },
128
+ },
129
+ }, inMetric .Data , metricdatatest .IgnoreTimestamp ())
130
+
131
+ metricdatatest .AssertAggregationsEqual (t , metricdata.Sum [int64 ]{
132
+ Temporality : metricdata .CumulativeTemporality ,
133
+ IsMonotonic : true ,
134
+ DataPoints : []metricdata.DataPoint [int64 ]{
135
+ {
136
+ Attributes : attribute .NewSet (attribute.KeyValue {
137
+ Key : attribute .Key ("processor" ),
138
+ Value : attribute .StringValue (set .ID .String ()),
139
+ }),
140
+ Value : 1 ,
141
+ },
142
+ },
143
+ }, outMetric .Data , metricdatatest .IgnoreTimestamp ())
144
+ }
0 commit comments