@@ -16,6 +16,7 @@ package telemetryapireceiver // import "github.com/open-telemetry/opentelemetry-
16
16
17
17
import (
18
18
"context"
19
+ "go.opentelemetry.io/collector/pdata/pmetric"
19
20
"net/http/httptest"
20
21
"strings"
21
22
"testing"
@@ -174,39 +175,187 @@ func TestCreateMetrics(t *testing.T) {
174
175
t .Parallel ()
175
176
176
177
testCases := []struct {
177
- desc string
178
- slice []telemetryapi.Event
179
- expectedMetrics int
180
- expectedType string
181
- expectedTimestamp string
182
- expectedBody string
183
- expectedSeverityText string
184
- expectedContainsRequestId bool
185
- expectedRequestId string
186
- expectedSeverityNumber plog.SeverityNumber
187
- expectError bool
178
+ desc string
179
+ slice []telemetryapi.Event
180
+ expectedType string
181
+ expectedMetrics []map [string ]any
182
+ expectError bool
188
183
}{
189
184
{
190
- desc : "no slice" ,
191
- expectedMetrics : 0 ,
192
- expectError : false ,
185
+ desc : "no slice" ,
186
+ expectError : false ,
193
187
},
194
188
{
195
- desc : "no slice " ,
189
+ desc : "platform.initReport " ,
196
190
slice : []telemetryapi.Event {
197
191
{
198
- Time : "2022-10-12T00:03:50 .000Z" ,
199
- Type : "function " ,
192
+ Time : "2022-10-12T00:01:15 .000Z" ,
193
+ Type : "platform.initReport " ,
200
194
Record : map [string ]any {
201
- "timestamp" : "2022-10-12T00:03:50.000Z" ,
202
- "level" : "INFO" ,
203
- "requestId" : "79b4f56e-95b1-4643-9700-2807f4e68189" ,
204
- "message" : "Hello world, I am a function!" ,
195
+ "initializationType" : "on-demand" ,
196
+ "status" : "success" ,
197
+ "phase" : "init" ,
198
+ "metrics" : map [string ]any {
199
+ "durationMs" : 125.33 ,
200
+ },
201
+ "spans" : []map [string ]any {
202
+ {
203
+ "name" : "someTimeSpan" ,
204
+ "start" : "2022-06-02T12:02:33.913Z" ,
205
+ "durationMs" : 90.1 ,
206
+ },
207
+ },
208
+ },
209
+ },
210
+ },
211
+ expectedMetrics : []map [string ]any {
212
+ {
213
+ "Name" : semconv .AttributeFaaSColdstart ,
214
+ "Value" : int64 (1 ),
215
+ },
216
+ },
217
+ expectedType : "platform.initReport" ,
218
+ expectError : false ,
219
+ },
220
+ {
221
+ desc : "platform.Report success" ,
222
+ slice : []telemetryapi.Event {
223
+ {
224
+ Time : "2022-10-12T00:01:15.000Z" ,
225
+ Type : "platform.report" ,
226
+ Record : map [string ]any {
227
+ "status" : "success" ,
228
+ "requestId" : "6d68ca91-49c9-448d-89b8-7ca3e6dc66aa" ,
229
+ "metrics" : map [string ]any {
230
+ "billedDurationMs" : 694 ,
231
+ "durationMs" : 693.92 ,
232
+ "initDurationMs" : 397.68 ,
233
+ "maxMemoryUsedMB" : 84 ,
234
+ "memorySizeMB" : 128 ,
235
+ },
236
+ "spans" : []map [string ]any {
237
+ {
238
+ "name" : "someTimeSpan" ,
239
+ "start" : "2022-06-02T12:02:33.913Z" ,
240
+ "durationMs" : 90.1 ,
241
+ },
242
+ },
205
243
},
206
244
},
207
245
},
208
- expectedMetrics : 0 ,
209
- expectError : false ,
246
+ expectedType : "platform.report" ,
247
+ expectedMetrics : []map [string ]any {
248
+ {
249
+ "Name" : "faas.invocations" ,
250
+ "Value" : int64 (1 ),
251
+ },
252
+ },
253
+ expectError : false ,
254
+ },
255
+ {
256
+ desc : "platform.Report error" ,
257
+ slice : []telemetryapi.Event {
258
+ {
259
+ Time : "2022-10-12T00:01:15.000Z" ,
260
+ Type : "platform.report" ,
261
+ Record : map [string ]any {
262
+ "status" : "error" ,
263
+ "errorType" : "error type" ,
264
+ "requestId" : "6d68ca91-49c9-448d-89b8-7ca3e6dc66aa" ,
265
+ "metrics" : map [string ]any {
266
+ "billedDurationMs" : 694 ,
267
+ "durationMs" : 693.92 ,
268
+ "initDurationMs" : 397.68 ,
269
+ "maxMemoryUsedMB" : 84 ,
270
+ "memorySizeMB" : 128 ,
271
+ },
272
+ "spans" : []map [string ]any {},
273
+ },
274
+ },
275
+ },
276
+ expectedType : "platform.report" ,
277
+ expectedMetrics : []map [string ]any {
278
+ {
279
+ "Name" : "faas.invocations" ,
280
+ "Value" : int64 (1 ),
281
+ },
282
+ {
283
+ "Name" : "faas.errors" ,
284
+ "Value" : int64 (1 ),
285
+ },
286
+ },
287
+ expectError : false ,
288
+ },
289
+ {
290
+ desc : "platform.Report failure" ,
291
+ slice : []telemetryapi.Event {
292
+ {
293
+ Time : "2022-10-12T00:01:15.000Z" ,
294
+ Type : "platform.report" ,
295
+ Record : map [string ]any {
296
+ "status" : "failure" ,
297
+ "errorType" : "error type" ,
298
+ "requestId" : "6d68ca91-49c9-448d-89b8-7ca3e6dc66aa" ,
299
+ "metrics" : map [string ]any {
300
+ "billedDurationMs" : 694 ,
301
+ "durationMs" : 693.92 ,
302
+ "initDurationMs" : 397.68 ,
303
+ "maxMemoryUsedMB" : 84 ,
304
+ "memorySizeMB" : 128 ,
305
+ },
306
+ "spans" : []map [string ]any {},
307
+ },
308
+ },
309
+ },
310
+ expectedType : "platform.report" ,
311
+ expectedMetrics : []map [string ]any {
312
+ {
313
+ "Name" : "faas.invocations" ,
314
+ "Value" : int64 (1 ),
315
+ },
316
+ {
317
+ "Name" : "faas.errors" ,
318
+ "Value" : int64 (1 ),
319
+ },
320
+ },
321
+ expectError : false ,
322
+ },
323
+ {
324
+ desc : "platform.Report timeout" ,
325
+ slice : []telemetryapi.Event {
326
+ {
327
+ Time : "2022-10-12T00:01:15.000Z" ,
328
+ Type : "platform.report" ,
329
+ Record : map [string ]any {
330
+ "status" : "timeout" ,
331
+ "requestId" : "6d68ca91-49c9-448d-89b8-7ca3e6dc66aa" ,
332
+ "metrics" : map [string ]any {
333
+ "billedDurationMs" : 694 ,
334
+ "durationMs" : 693.92 ,
335
+ "initDurationMs" : 397.68 ,
336
+ "maxMemoryUsedMB" : 84 ,
337
+ "memorySizeMB" : 128 ,
338
+ },
339
+ "spans" : []map [string ]any {},
340
+ },
341
+ },
342
+ },
343
+ expectedType : "platform.report" ,
344
+ expectedMetrics : []map [string ]any {
345
+ {
346
+ "Name" : "faas.invocations" ,
347
+ "Value" : int64 (1 ),
348
+ },
349
+ {
350
+ "Name" : "faas.errors" ,
351
+ "Value" : int64 (1 ),
352
+ },
353
+ {
354
+ "Name" : "faas.timeouts" ,
355
+ "Value" : int64 (1 ),
356
+ },
357
+ },
358
+ expectError : false ,
210
359
},
211
360
}
212
361
for _ , tc := range testCases {
@@ -224,25 +373,17 @@ func TestCreateMetrics(t *testing.T) {
224
373
require .Equal (t , 1 , resourceMetric .ScopeMetrics ().Len ())
225
374
scopeMetric := resourceMetric .ScopeMetrics ().At (0 )
226
375
require .Equal (t , scopeName , scopeMetric .Scope ().Name ())
227
- require .Equal (t , tc .expectedMetrics , scopeMetric .Metrics ().Len ())
228
- if scopeMetric .Metrics ().Len () > 0 {
229
- // metric := scopeMetric.Metrics().At(0)
230
-
231
- // logRecord := scopeLog.LogRecords().At(0)
232
- // attr, ok := logRecord.Attributes().Get("type")
233
- // require.True(t, ok)
234
- // require.Equal(t, tc.expectedType, attr.Str())
235
- // expectedTime, err := time.Parse(time.RFC3339, tc.expectedTimestamp)
236
- // require.NoError(t, err)
237
- // require.Equal(t, pcommon.NewTimestampFromTime(expectedTime), logRecord.Timestamp())
238
- // requestId, ok := logRecord.Attributes().Get(semconv.AttributeFaaSInvocationID)
239
- // require.Equal(t, tc.expectedContainsRequestId, ok)
240
- // if ok {
241
- // require.Equal(t, tc.expectedRequestId, requestId.Str())
242
- // }
243
- // require.Equal(t, tc.expectedSeverityText, logRecord.SeverityText())
244
- // require.Equal(t, tc.expectedSeverityNumber, logRecord.SeverityNumber())
245
- // require.Equal(t, tc.expectedBody, logRecord.Body().Str())
376
+ require .Equal (t , len (tc .expectedMetrics ), scopeMetric .Metrics ().Len ())
377
+ for idx , m := range tc .expectedMetrics {
378
+ metric := scopeMetric .Metrics ().At (idx )
379
+ attr , ok := metric .Metadata ().Get ("type" )
380
+ require .True (t , ok )
381
+ require .Equal (t , tc .expectedType , attr .Str ())
382
+ require .Equal (t , m ["Name" ], metric .Name ())
383
+ require .True (t , metric .Sum ().IsMonotonic ())
384
+ require .Equal (t , pmetric .AggregationTemporalityCumulative , metric .Sum ().AggregationTemporality ())
385
+ require .Equal (t , 1 , metric .Sum ().DataPoints ().Len ())
386
+ require .Equal (t , m ["Value" ], metric .Sum ().DataPoints ().At (0 ).IntValue ())
246
387
}
247
388
}
248
389
})
0 commit comments