@@ -5,16 +5,19 @@ package prometheusremotewrite
5
5
6
6
import (
7
7
"testing"
8
+ "time"
8
9
9
10
"github.com/prometheus/common/model"
10
11
"github.com/prometheus/prometheus/prompb"
11
12
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
15
"go.opentelemetry.io/collector/pdata/pcommon"
16
+ "go.opentelemetry.io/collector/pdata/pmetric"
15
17
conventions "go.opentelemetry.io/otel/semconv/v1.25.0"
16
18
17
19
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
20
+ prometheustranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus"
18
21
)
19
22
20
23
func TestAddResourceTargetInfoV2 (t * testing.T ) {
@@ -153,3 +156,124 @@ func TestAddResourceTargetInfoV2(t *testing.T) {
153
156
})
154
157
}
155
158
}
159
+
160
+ func TestPrometheusConverterV2_AddSummaryDataPoints (t * testing.T ) {
161
+ ts := pcommon .Timestamp (time .Now ().UnixNano ())
162
+ tests := []struct {
163
+ name string
164
+ metric func () pmetric.Metric
165
+ want func () map [uint64 ]* writev2.TimeSeries
166
+ }{
167
+ {
168
+ name : "summary with start time" ,
169
+ metric : func () pmetric.Metric {
170
+ metric := pmetric .NewMetric ()
171
+ metric .SetName ("test_summary" )
172
+ metric .SetEmptySummary ()
173
+
174
+ dp := metric .Summary ().DataPoints ().AppendEmpty ()
175
+ dp .SetTimestamp (ts )
176
+ dp .SetStartTimestamp (ts )
177
+
178
+ return metric
179
+ },
180
+ want : func () map [uint64 ]* writev2.TimeSeries {
181
+ labels := []prompb.Label {
182
+ {Name : model .MetricNameLabel , Value : "test_summary" + countStr },
183
+ }
184
+ sumLabels := []prompb.Label {
185
+ {Name : model .MetricNameLabel , Value : "test_summary" + sumStr },
186
+ }
187
+ return map [uint64 ]* writev2.TimeSeries {
188
+ timeSeriesSignature (labels ): {
189
+ LabelsRefs : []uint32 {1 , 3 },
190
+ Samples : []writev2.Sample {
191
+ {Value : 0 , Timestamp : convertTimeStamp (ts )},
192
+ },
193
+ Metadata : writev2.Metadata {
194
+ Type : writev2 .Metadata_METRIC_TYPE_SUMMARY ,
195
+ HelpRef : 0 ,
196
+ },
197
+ },
198
+ timeSeriesSignature (sumLabels ): {
199
+ LabelsRefs : []uint32 {1 , 2 },
200
+ Samples : []writev2.Sample {
201
+ {Value : 0 , Timestamp : convertTimeStamp (ts )},
202
+ },
203
+ Metadata : writev2.Metadata {
204
+ Type : writev2 .Metadata_METRIC_TYPE_SUMMARY ,
205
+ HelpRef : 0 ,
206
+ },
207
+ },
208
+ }
209
+ },
210
+ },
211
+ {
212
+ name : "summary without start time" ,
213
+ metric : func () pmetric.Metric {
214
+ metric := pmetric .NewMetric ()
215
+ metric .SetName ("test_summary" )
216
+ metric .SetEmptySummary ()
217
+
218
+ dp := metric .Summary ().DataPoints ().AppendEmpty ()
219
+ dp .SetTimestamp (ts )
220
+
221
+ return metric
222
+ },
223
+ want : func () map [uint64 ]* writev2.TimeSeries {
224
+ labels := []prompb.Label {
225
+ {Name : model .MetricNameLabel , Value : "test_summary" + countStr },
226
+ }
227
+ sumLabels := []prompb.Label {
228
+ {Name : model .MetricNameLabel , Value : "test_summary" + sumStr },
229
+ }
230
+ return map [uint64 ]* writev2.TimeSeries {
231
+ timeSeriesSignature (labels ): {
232
+ LabelsRefs : []uint32 {1 , 3 },
233
+ Samples : []writev2.Sample {
234
+ {Value : 0 , Timestamp : convertTimeStamp (ts )},
235
+ },
236
+ Metadata : writev2.Metadata {
237
+ Type : writev2 .Metadata_METRIC_TYPE_SUMMARY ,
238
+ HelpRef : 0 ,
239
+ },
240
+ },
241
+ timeSeriesSignature (sumLabels ): {
242
+ LabelsRefs : []uint32 {1 , 2 },
243
+ Samples : []writev2.Sample {
244
+ {Value : 0 , Timestamp : convertTimeStamp (ts )},
245
+ },
246
+ Metadata : writev2.Metadata {
247
+ Type : writev2 .Metadata_METRIC_TYPE_SUMMARY ,
248
+ HelpRef : 0 ,
249
+ },
250
+ },
251
+ }
252
+ },
253
+ },
254
+ }
255
+ for _ , tt := range tests {
256
+ t .Run (tt .name , func (t * testing.T ) {
257
+ metric := tt .metric ()
258
+ converter := newPrometheusConverterV2 ()
259
+
260
+ m := metadata {
261
+ Type : otelMetricTypeToPromMetricTypeV2 (metric ),
262
+ Help : metric .Description (),
263
+ Unit : prometheustranslator .BuildCompliantPrometheusUnit (metric .Unit ()),
264
+ }
265
+
266
+ converter .addSummaryDataPoints (
267
+ metric .Summary ().DataPoints (),
268
+ pcommon .NewResource (),
269
+ Settings {},
270
+ metric .Name (),
271
+ m ,
272
+ )
273
+
274
+ assert .Equal (t , tt .want (), converter .unique )
275
+ // TODO check when conflicts handling is implemented
276
+ // assert.Empty(t, converter.conflicts)
277
+ })
278
+ }
279
+ }
0 commit comments