@@ -6,12 +6,14 @@ package elasticsearchexporter
6
6
import (
7
7
"bytes"
8
8
"encoding/json"
9
+ "fmt"
9
10
"testing"
10
11
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
13
14
"go.opentelemetry.io/collector/pdata/pcommon"
14
15
"go.opentelemetry.io/collector/pdata/plog"
16
+ "go.opentelemetry.io/collector/pdata/pmetric"
15
17
)
16
18
17
19
func TestSerializeLog (t * testing.T ) {
@@ -159,6 +161,48 @@ func TestSerializeLog(t *testing.T) {
159
161
}
160
162
}
161
163
164
+ func TestSerializeMetricsConflict (t * testing.T ) {
165
+ resourceMetrics := pmetric .NewResourceMetrics ()
166
+ scopeMetrics := resourceMetrics .ScopeMetrics ().AppendEmpty ()
167
+ var dataPoints []dataPoint
168
+ metric1 := scopeMetrics .Metrics ().AppendEmpty ()
169
+ metric2 := scopeMetrics .Metrics ().AppendEmpty ()
170
+ for _ , m := range []pmetric.Metric {metric1 , metric2 } {
171
+ m .SetName ("foo" )
172
+ dp := m .SetEmptyGauge ().DataPoints ().AppendEmpty ()
173
+ dp .SetIntValue (42 )
174
+ dataPoints = append (dataPoints , newNumberDataPoint (m , dp ))
175
+ }
176
+
177
+ var validationErrors []error
178
+ var buf bytes.Buffer
179
+ _ , err := serializeMetrics (resourceMetrics .Resource (), "" , scopeMetrics .Scope (), "" , dataPoints , & validationErrors , & buf )
180
+ if err != nil {
181
+ t .Errorf ("serializeMetrics() error = %v" , err )
182
+ }
183
+ b := buf .Bytes ()
184
+ eventAsJSON := string (b )
185
+ var result any
186
+ decoder := json .NewDecoder (bytes .NewBuffer (b ))
187
+ decoder .UseNumber ()
188
+ if err := decoder .Decode (& result ); err != nil {
189
+ t .Error (err )
190
+ }
191
+
192
+ assert .Len (t , validationErrors , 1 )
193
+ assert .Equal (t , fmt .Errorf ("metric with name 'foo' has already been serialized in document with timestamp 1970-01-01T00:00:00.000000000Z" ), validationErrors [0 ])
194
+
195
+ assert .Equal (t , map [string ]any {
196
+ "@timestamp" : "1970-01-01T00:00:00.000000000Z" ,
197
+ "data_stream" : map [string ]any {},
198
+ "resource" : map [string ]any {},
199
+ "scope" : map [string ]any {},
200
+ "metrics" : map [string ]any {
201
+ "foo" : json .Number ("42" ),
202
+ },
203
+ }, result , eventAsJSON )
204
+ }
205
+
162
206
func TestMergeGeolocation (t * testing.T ) {
163
207
attributes := map [string ]any {
164
208
"geo.location.lon" : 1.1 ,
0 commit comments