Skip to content

Commit 5312452

Browse files
committed
update unit tests
1 parent e6078a0 commit 5312452

File tree

4 files changed

+295
-156
lines changed

4 files changed

+295
-156
lines changed

collector/internal/telemetryapi/listener_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ func setupListener(t *testing.T) (*Listener, string) {
4545

4646
address, err := listener.Start()
4747
require.NoError(t, err)
48-
49-
t.Cleanup(func() {
50-
listener.Shutdown()
51-
})
52-
5348
return listener, address
5449
}
5550

@@ -183,6 +178,7 @@ func TestListenOnAddress(t *testing.T) {
183178

184179
func TestListener_StartAndShutdown(t *testing.T) {
185180
listener, address := setupListener(t)
181+
defer listener.Shutdown()
186182
require.NotEqual(t, address, "", "Start() should not return an empty address")
187183
require.True(t, strings.HasPrefix(address, "http://"), "Address should start with http://")
188184
require.NotNil(t, listener.httpServer, "httpServer should not be nil")
@@ -241,6 +237,7 @@ func TestListener_httpHandler(t *testing.T) {
241237
for _, test := range testCases {
242238
t.Run(test.name, func(t *testing.T) {
243239
listener, address := setupListener(t)
240+
defer listener.Shutdown()
244241
submitEvents(t, address, test.events)
245242
require.EventuallyWithT(t, func(c *assert.CollectT) {
246243
require.Equal(c, test.expectedCount, listener.queue.Len())
@@ -302,6 +299,7 @@ func TestListener_Wait_Success(t *testing.T) {
302299
for _, test := range testCases {
303300
t.Run(test.name, func(t *testing.T) {
304301
listener, address := setupListener(t)
302+
defer listener.Shutdown()
305303

306304
waitDone := make(chan error, 1)
307305
go func() {

collector/receiver/telemetryapireceiver/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Config struct {
2323
extensionID string
2424
Port int `mapstructure:"port"`
2525
Types []string `mapstructure:"types"`
26-
LogReport bool `mapstructure:"log_report"`
26+
LogReport *bool `mapstructure:"log_report"`
2727
}
2828

2929
// Validate validates the configuration by checking for missing or invalid fields

collector/receiver/telemetryapireceiver/receiver.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ func (r *telemetryAPIReceiver) createLogs(slice []event) (plog.Logs, error) {
205205
scopeLog.Scope().SetName(scopeName)
206206
for _, el := range slice {
207207
r.logger.Debug(fmt.Sprintf("Event: %s", el.Type), zap.Any("event", el))
208+
if !r.logReport && el.Type == string(telemetryapi.PlatformReport) {
209+
continue
210+
}
208211
logRecord := scopeLog.LogRecords().AppendEmpty()
209212
logRecord.Attributes().PutStr("type", el.Type)
210213
if t, err := time.Parse(time.RFC3339, el.Time); err == nil {
@@ -215,6 +218,17 @@ func (r *telemetryAPIReceiver) createLogs(slice []event) (plog.Logs, error) {
215218
return plog.Logs{}, err
216219
}
217220
if record, ok := el.Record.(map[string]interface{}); ok {
221+
requestId := r.getRecordRequestId(record)
222+
if requestId != "" {
223+
logRecord.Attributes().PutStr(semconv.AttributeFaaSInvocationID, requestId)
224+
225+
// If this is the first event in the invocation with a request id (i.e. the "platform.start" event),
226+
// set the current invocation id to this request id.
227+
if el.Type == string(telemetryapi.PlatformStart) {
228+
r.currentFaasInvocationID = requestId
229+
}
230+
}
231+
218232
// in JSON format https://docs.aws.amazon.com/lambda/latest/dg/telemetry-schema-reference.html#telemetry-api-function
219233
if timestamp, ok := record["timestamp"].(string); ok {
220234
if t, err := time.Parse(time.RFC3339, timestamp); err == nil {
@@ -229,24 +243,13 @@ func (r *telemetryAPIReceiver) createLogs(slice []event) (plog.Logs, error) {
229243
logRecord.SetSeverityText(logRecord.SeverityNumber().String())
230244
}
231245

232-
requestId := r.getRecordRequestId(record)
233-
if requestId != "" {
234-
logRecord.Attributes().PutStr(semconv.AttributeFaaSInvocationID, requestId)
235-
236-
// If this is the first event in the invocation with a request id (i.e. the "platform.start" event),
237-
// set the current invocation id to this request id.
238-
if el.Type == string(telemetryapi.PlatformStart) {
239-
r.currentFaasInvocationID = requestId
240-
}
241-
}
242-
243-
if line, ok := record["message"].(string); ok {
244-
logRecord.Body().SetStr(line)
245-
} else if el.Type == string(telemetryapi.PlatformReport) {
246+
if el.Type == string(telemetryapi.PlatformReport) {
246247
platformReportMessage := createPlatformReportMessage(requestId, record)
247248
if platformReportMessage != "" {
248249
logRecord.Body().SetStr(platformReportMessage)
249250
}
251+
} else if line, ok := record["message"].(string); ok {
252+
logRecord.Body().SetStr(line)
250253
}
251254
} else {
252255
if r.currentFaasInvocationID != "" {
@@ -406,7 +409,7 @@ func newTelemetryAPIReceiver(
406409
}
407410
}
408411

409-
subscribedTypes := []telemetryapi.EventType{}
412+
var subscribedTypes []telemetryapi.EventType
410413
for _, val := range cfg.Types {
411414
switch val {
412415
case "platform":
@@ -418,14 +421,19 @@ func newTelemetryAPIReceiver(
418421
}
419422
}
420423

424+
logReport := true
425+
if cfg.LogReport != nil {
426+
logReport = *cfg.LogReport
427+
}
428+
421429
return &telemetryAPIReceiver{
422430
logger: set.Logger,
423431
queue: queue.New(initialQueueSize),
424432
extensionID: cfg.extensionID,
425433
port: cfg.Port,
426434
types: subscribedTypes,
427435
resource: r,
428-
logReport: cfg.LogReport,
436+
logReport: logReport,
429437
}, nil
430438
}
431439

0 commit comments

Comments
 (0)