@@ -6,12 +6,19 @@ package telemetry // import "go.opentelemetry.io/collector/service/telemetry"
6
6
import (
7
7
"context"
8
8
"errors"
9
+ "io"
10
+ "net/http"
11
+ "net/http/httptest"
9
12
"reflect"
10
13
"testing"
11
14
15
+ "github.com/stretchr/testify/assert"
12
16
"github.com/stretchr/testify/require"
13
17
config "go.opentelemetry.io/contrib/otelconf/v0.3.0"
14
18
"go.uber.org/zap/zapcore"
19
+
20
+ "go.opentelemetry.io/collector/pdata/plog/plogotlp"
21
+ semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
15
22
)
16
23
17
24
func TestNewLogger (t * testing.T ) {
@@ -79,13 +86,114 @@ func TestNewLogger(t *testing.T) {
79
86
require .NoError (t , err )
80
87
gotType := reflect .TypeOf (l .Core ()).String ()
81
88
require .Equal (t , tt .wantCoreType , gotType )
82
- type shutdownable interface {
83
- Shutdown (context.Context ) error
84
- }
85
89
if prov , ok := lp .(shutdownable ); ok {
86
90
require .NoError (t , prov .Shutdown (context .Background ()))
87
91
}
88
92
}
89
93
})
90
94
}
91
95
}
96
+
97
+ func TestOTELZapIntegration (t * testing.T ) {
98
+ version := "1.2.3"
99
+ service := "test-service"
100
+ testAttribute := "test-attribute"
101
+
102
+ receivedLogs := 0
103
+ totalLogs := 10
104
+
105
+ // Create a backend to receive the logs and assert the content
106
+ srv := createBackend ("/v1/logs" , func (writer http.ResponseWriter , request * http.Request ) {
107
+ body , err := io .ReadAll (request .Body )
108
+ require .NoError (t , err )
109
+ defer request .Body .Close ()
110
+
111
+ // Unmarshal the protobuf body into logs
112
+ req := plogotlp .NewExportRequest ()
113
+ err = req .UnmarshalProto (body )
114
+ require .NoError (t , err )
115
+
116
+ logs := req .Logs ()
117
+ rl := logs .ResourceLogs ().At (0 )
118
+ sl := rl .ScopeLogs ().At (0 )
119
+ logRecord := sl .LogRecords ().At (0 )
120
+ attrs := logRecord .Attributes ().AsRaw ()
121
+
122
+ assert .Contains (t , attrs , semconv .AttributeServiceName )
123
+ assert .Contains (t , attrs , semconv .AttributeServiceVersion )
124
+ assert .Contains (t , attrs , testAttribute )
125
+ receivedLogs ++
126
+
127
+ writer .WriteHeader (http .StatusOK )
128
+ })
129
+ defer srv .Close ()
130
+
131
+ processors := []config.LogRecordProcessor {
132
+ {
133
+ Simple : & config.SimpleLogRecordProcessor {
134
+ Exporter : config.LogRecordExporter {
135
+ OTLP : & config.OTLP {
136
+ Endpoint : ptr (srv .URL ),
137
+ Protocol : ptr ("http/protobuf" ),
138
+ Insecure : ptr (true ),
139
+ },
140
+ },
141
+ },
142
+ },
143
+ }
144
+
145
+ sdk , _ := config .NewSDK (
146
+ config .WithOpenTelemetryConfiguration (
147
+ config.OpenTelemetryConfiguration {
148
+ LoggerProvider : & config.LoggerProvider {
149
+ Processors : processors ,
150
+ },
151
+ },
152
+ ),
153
+ )
154
+
155
+ cfg := Config {
156
+ Logs : LogsConfig {
157
+ Level : zapcore .DebugLevel ,
158
+ Development : true ,
159
+ Encoding : "json" ,
160
+ Processors : processors ,
161
+ },
162
+ Resource : map [string ]* string {
163
+ semconv .AttributeServiceName : ptr (service ),
164
+ semconv .AttributeServiceVersion : ptr (version ),
165
+ testAttribute : ptr ("test-value" ),
166
+ },
167
+ }
168
+ l , lp , err := newLogger (Settings {SDK : & sdk }, cfg )
169
+ require .NoError (t , err )
170
+ require .NotNil (t , l )
171
+ require .NotNil (t , lp )
172
+
173
+ defer func () {
174
+ if prov , ok := lp .(shutdownable ); ok {
175
+ require .NoError (t , prov .Shutdown (context .Background ()))
176
+ }
177
+ }()
178
+
179
+ // Generate some logs to send to the backend
180
+ for i := 0 ; i < totalLogs ; i ++ {
181
+ l .Info ("Test log message" )
182
+ }
183
+
184
+ // Ensure the correct number of logs were received
185
+ assert .Equal (t , totalLogs , receivedLogs )
186
+ }
187
+
188
+ type shutdownable interface {
189
+ Shutdown (context.Context ) error
190
+ }
191
+
192
+ func createBackend (endpoint string , handler func (writer http.ResponseWriter , request * http.Request )) * httptest.Server {
193
+ mux := http .NewServeMux ()
194
+ mux .HandleFunc (endpoint , handler )
195
+
196
+ srv := httptest .NewServer (mux )
197
+
198
+ return srv
199
+ }
0 commit comments