|
| 1 | +// Copyright 2026, Pulumi Corporation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package logging |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "sync" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 26 | + "go.opentelemetry.io/collector/pdata/plog" |
| 27 | + collogspb "go.opentelemetry.io/proto/otlp/collector/logs/v1" |
| 28 | + commonpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 29 | + logspb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 30 | + resourcepb "go.opentelemetry.io/proto/otlp/resource/v1" |
| 31 | +) |
| 32 | + |
| 33 | +type mockLogExporter struct { |
| 34 | + mu sync.Mutex |
| 35 | + logs []plog.Logs |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockLogExporter) ExportLogs(_ context.Context, logs plog.Logs) error { |
| 39 | + m.mu.Lock() |
| 40 | + defer m.mu.Unlock() |
| 41 | + m.logs = append(m.logs, logs) |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func (m *mockLogExporter) Shutdown(context.Context) error { return nil } |
| 46 | + |
| 47 | +func (m *mockLogExporter) firstRecord() plog.LogRecord { |
| 48 | + m.mu.Lock() |
| 49 | + defer m.mu.Unlock() |
| 50 | + return m.logs[0].ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0) |
| 51 | +} |
| 52 | + |
| 53 | +func TestExportForwardsToExporter(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + exporter := &mockLogExporter{} |
| 57 | + svc := &service{exporter: exporter} |
| 58 | + now := uint64(time.Now().UnixNano()) //nolint:gosec // test timestamps |
| 59 | + |
| 60 | + req := &collogspb.ExportLogsServiceRequest{ |
| 61 | + ResourceLogs: []*logspb.ResourceLogs{{ |
| 62 | + Resource: &resourcepb.Resource{ |
| 63 | + Attributes: []*commonpb.KeyValue{{ |
| 64 | + Key: "service.name", |
| 65 | + Value: &commonpb.AnyValue{ |
| 66 | + Value: &commonpb.AnyValue_StringValue{StringValue: "test-svc"}, |
| 67 | + }, |
| 68 | + }}, |
| 69 | + }, |
| 70 | + ScopeLogs: []*logspb.ScopeLogs{{ |
| 71 | + LogRecords: []*logspb.LogRecord{{ |
| 72 | + TimeUnixNano: now, |
| 73 | + SeverityNumber: logspb.SeverityNumber_SEVERITY_NUMBER_INFO, |
| 74 | + Body: &commonpb.AnyValue{ |
| 75 | + Value: &commonpb.AnyValue_StringValue{StringValue: "hello"}, |
| 76 | + }, |
| 77 | + Attributes: []*commonpb.KeyValue{{ |
| 78 | + Key: "key", |
| 79 | + Value: &commonpb.AnyValue{Value: &commonpb.AnyValue_StringValue{StringValue: "value"}}, |
| 80 | + }}, |
| 81 | + }}, |
| 82 | + }}, |
| 83 | + }}, |
| 84 | + } |
| 85 | + |
| 86 | + resp, err := svc.Export(t.Context(), req) |
| 87 | + require.NoError(t, err) |
| 88 | + require.NotNil(t, resp) |
| 89 | + |
| 90 | + require.Len(t, exporter.logs, 1) |
| 91 | + lr := exporter.firstRecord() |
| 92 | + assert.Equal(t, "hello", lr.Body().AsString()) |
| 93 | + v, ok := lr.Attributes().Get("key") |
| 94 | + require.True(t, ok) |
| 95 | + assert.Equal(t, "value", v.Str()) |
| 96 | +} |
| 97 | + |
| 98 | +func TestExportPassesThroughBytesAttributes(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + |
| 101 | + // Property value bytes are passed through to the exporter as-is; |
| 102 | + // decoding is the consumer's responsibility. |
| 103 | + encoded := []byte("pulumiPv" + "some-protobuf-data") |
| 104 | + |
| 105 | + exporter := &mockLogExporter{} |
| 106 | + svc := &service{exporter: exporter} |
| 107 | + now := uint64(time.Now().UnixNano()) //nolint:gosec // test timestamps |
| 108 | + |
| 109 | + req := &collogspb.ExportLogsServiceRequest{ |
| 110 | + ResourceLogs: []*logspb.ResourceLogs{{ |
| 111 | + ScopeLogs: []*logspb.ScopeLogs{{ |
| 112 | + LogRecords: []*logspb.LogRecord{{ |
| 113 | + TimeUnixNano: now, |
| 114 | + SeverityNumber: logspb.SeverityNumber_SEVERITY_NUMBER_DEBUG, |
| 115 | + Body: &commonpb.AnyValue{ |
| 116 | + Value: &commonpb.AnyValue_StringValue{StringValue: "resource inputs"}, |
| 117 | + }, |
| 118 | + Attributes: []*commonpb.KeyValue{{ |
| 119 | + Key: "inputs", |
| 120 | + Value: &commonpb.AnyValue{Value: &commonpb.AnyValue_BytesValue{BytesValue: encoded}}, |
| 121 | + }}, |
| 122 | + }}, |
| 123 | + }}, |
| 124 | + }}, |
| 125 | + } |
| 126 | + |
| 127 | + resp, err := svc.Export(t.Context(), req) |
| 128 | + require.NoError(t, err) |
| 129 | + require.NotNil(t, resp) |
| 130 | + |
| 131 | + lr := exporter.firstRecord() |
| 132 | + v, ok := lr.Attributes().Get("inputs") |
| 133 | + require.True(t, ok) |
| 134 | + require.Equal(t, pcommon.ValueTypeBytes, v.Type()) |
| 135 | + assert.Equal(t, encoded, v.Bytes().AsRaw()) |
| 136 | +} |
| 137 | + |
| 138 | +func TestExportNilExporterDoesNotPanic(t *testing.T) { |
| 139 | + t.Parallel() |
| 140 | + |
| 141 | + svc := &service{exporter: nil} |
| 142 | + now := uint64(time.Now().UnixNano()) //nolint:gosec // test timestamps |
| 143 | + |
| 144 | + req := &collogspb.ExportLogsServiceRequest{ |
| 145 | + ResourceLogs: []*logspb.ResourceLogs{{ |
| 146 | + ScopeLogs: []*logspb.ScopeLogs{{ |
| 147 | + LogRecords: []*logspb.LogRecord{{ |
| 148 | + TimeUnixNano: now, |
| 149 | + SeverityNumber: logspb.SeverityNumber_SEVERITY_NUMBER_INFO, |
| 150 | + Body: &commonpb.AnyValue{ |
| 151 | + Value: &commonpb.AnyValue_StringValue{StringValue: "test"}, |
| 152 | + }, |
| 153 | + }}, |
| 154 | + }}, |
| 155 | + }}, |
| 156 | + } |
| 157 | + |
| 158 | + resp, err := svc.Export(t.Context(), req) |
| 159 | + require.NoError(t, err) |
| 160 | + require.NotNil(t, resp) |
| 161 | +} |
0 commit comments