Skip to content

Commit 4dd17cd

Browse files
committed
add init duration
1 parent 78b91e8 commit 4dd17cd

File tree

2 files changed

+81
-18
lines changed

2 files changed

+81
-18
lines changed

collector/receiver/telemetryapireceiver/receiver.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ const (
4545
initialQueueSize = 5
4646
scopeName = "github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapi"
4747

48-
logReportFmt = "REPORT RequestId: %s Duration: %.2f ms Billed Duration: %d ms Memory Size: %d MB Max Memory Used: %d MB"
48+
logReportFmt = "REPORT RequestId: %s Duration: %.2f ms Billed Duration: %.0f ms Memory Size: %.0f MB Max Memory Used: %.0f MB"
4949
metricBilledDurationMs = "billedDurationMs"
5050
metricDurationMs = "durationMs"
5151
metricMaxMemoryUsedMB = "maxMemoryUsedMB"
5252
metricMemorySizeMB = "memorySizeMB"
53+
metricInitDurationMs = "initDurationMs"
5354
)
5455

5556
type telemetryAPIReceiver struct {
@@ -277,21 +278,28 @@ func createReportLogRecord(scopeLog *plog.ScopeLogs, record map[string]interface
277278
if !ok {
278279
return nil
279280
}
280-
var durationMs float64
281-
var billedDurationMs, memorySizeMB, maxMemoryUsedMB int
281+
var durationMs, billedDurationMs, memorySizeMB, maxMemoryUsedMB float64
282282
if durationMs, ok = metrics[metricDurationMs].(float64); !ok {
283283
return nil
284284
}
285-
if billedDurationMs, ok = metrics[metricBilledDurationMs].(int); !ok {
285+
if billedDurationMs, ok = metrics[metricBilledDurationMs].(float64); !ok {
286286
return nil
287287
}
288-
if memorySizeMB, ok = metrics[metricMemorySizeMB].(int); !ok {
288+
if memorySizeMB, ok = metrics[metricMemorySizeMB].(float64); !ok {
289289
return nil
290290
}
291-
if maxMemoryUsedMB, ok = metrics[metricMaxMemoryUsedMB].(int); !ok {
291+
if maxMemoryUsedMB, ok = metrics[metricMaxMemoryUsedMB].(float64); !ok {
292292
return nil
293293
}
294294

295+
// optionally gather information about cold start time
296+
var initDurationMs float64
297+
if initDurationMsVal, exists := metrics[metricInitDurationMs]; exists {
298+
if val, ok := initDurationMsVal.(float64); ok {
299+
initDurationMs = val
300+
}
301+
}
302+
295303
// gathering requestId
296304
requestId := ""
297305
if requestId, ok = record["requestId"].(string); !ok {
@@ -300,16 +308,21 @@ func createReportLogRecord(scopeLog *plog.ScopeLogs, record map[string]interface
300308

301309
// we have all information available, we can create the log record
302310
logRecord := scopeLog.LogRecords().AppendEmpty()
303-
logRecord.Body().SetStr(
304-
fmt.Sprintf(
305-
logReportFmt,
306-
requestId,
307-
durationMs,
308-
billedDurationMs,
309-
memorySizeMB,
310-
maxMemoryUsedMB,
311-
),
311+
logRecord.Attributes().PutStr(semconv.AttributeFaaSInvocationID, requestId)
312+
313+
// building the body of the log record, optionally adding the init duration
314+
body := fmt.Sprintf(
315+
logReportFmt,
316+
requestId,
317+
durationMs,
318+
billedDurationMs,
319+
memorySizeMB,
320+
maxMemoryUsedMB,
312321
)
322+
if initDurationMs > 0 {
323+
body += fmt.Sprintf(" Init Duration: %.2f ms", initDurationMs)
324+
}
325+
logRecord.Body().SetStr(body)
313326

314327
return &logRecord
315328
}

collector/receiver/telemetryapireceiver/receiver_test.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,9 @@ func TestCreateLogsWithLogReport(t *testing.T) {
539539
"requestId": "test-request-id-123",
540540
"metrics": map[string]any{
541541
"durationMs": 123.45,
542-
"billedDurationMs": 124,
543-
"memorySizeMB": 512,
544-
"maxMemoryUsedMB": 256,
542+
"billedDurationMs": float64(124),
543+
"memorySizeMB": float64(512),
544+
"maxMemoryUsedMB": float64(256),
545545
},
546546
},
547547
},
@@ -664,6 +664,56 @@ func TestCreateLogsWithLogReport(t *testing.T) {
664664
expectedLogRecords: 0,
665665
expectError: false,
666666
},
667+
{
668+
desc: "platform.report with logReport enabled - with initDurationMs",
669+
slice: []event{
670+
{
671+
Time: "2022-10-12T00:03:50.000Z",
672+
Type: "platform.report",
673+
Record: map[string]any{
674+
"requestId": "test-request-id-123",
675+
"metrics": map[string]any{
676+
"durationMs": 123.45,
677+
"billedDurationMs": 124.0,
678+
"memorySizeMB": 512.0,
679+
"maxMemoryUsedMB": 256.0,
680+
"initDurationMs": 50.5,
681+
},
682+
},
683+
},
684+
},
685+
logReport: true,
686+
expectedLogRecords: 1,
687+
expectedType: "platform.report",
688+
expectedTimestamp: "2022-10-12T00:03:50.000Z",
689+
expectedBody: "REPORT RequestId: test-request-id-123 Duration: 123.45 ms Billed Duration: 124 ms Memory Size: 512 MB Max Memory Used: 256 MB Init Duration: 50.50 ms",
690+
expectError: false,
691+
},
692+
{
693+
desc: "platform.report with logReport enabled - with invalid initDurationMs type",
694+
slice: []event{
695+
{
696+
Time: "2022-10-12T00:03:50.000Z",
697+
Type: "platform.report",
698+
Record: map[string]any{
699+
"requestId": "test-request-id-123",
700+
"metrics": map[string]any{
701+
"durationMs": 123.45,
702+
"billedDurationMs": 124.0,
703+
"memorySizeMB": 512.0,
704+
"maxMemoryUsedMB": 256.0,
705+
"initDurationMs": "invalid-string",
706+
},
707+
},
708+
},
709+
},
710+
logReport: true,
711+
expectedLogRecords: 1,
712+
expectedType: "platform.report",
713+
expectedTimestamp: "2022-10-12T00:03:50.000Z",
714+
expectedBody: "REPORT RequestId: test-request-id-123 Duration: 123.45 ms Billed Duration: 124 ms Memory Size: 512 MB Max Memory Used: 256 MB",
715+
expectError: false,
716+
},
667717
}
668718
for _, tc := range testCases {
669719
t.Run(tc.desc, func(t *testing.T) {

0 commit comments

Comments
 (0)