@@ -21,7 +21,9 @@ import (
21
21
"go.uber.org/zap/zapcore"
22
22
"go.uber.org/zap/zaptest/observer"
23
23
24
+ "go.opentelemetry.io/collector/component"
24
25
"go.opentelemetry.io/collector/pdata/plog/plogotlp"
26
+ "go.opentelemetry.io/collector/service/internal/resource"
25
27
)
26
28
27
29
type shutdownable interface {
@@ -126,38 +128,127 @@ func TestNewLogger(t *testing.T) {
126
128
}
127
129
128
130
func TestNewLoggerWithResource (t * testing.T ) {
129
- observerCore , observedLogs := observer .New (zap .InfoLevel )
130
-
131
- set := Settings {
132
- ZapOptions : []zap.Option {
133
- zap .WrapCore (func (core zapcore.Core ) zapcore.Core {
134
- // Combine original core and observer core to capture everything
135
- return zapcore .NewTee (core , observerCore )
136
- }),
131
+ tests := []struct {
132
+ name string
133
+ buildInfo component.BuildInfo
134
+ resourceConfig map [string ]* string
135
+ wantFields map [string ]string
136
+ }{
137
+ {
138
+ name : "auto-populated fields only" ,
139
+ buildInfo : component.BuildInfo {
140
+ Command : "mycommand" ,
141
+ Version : "1.0.0" ,
142
+ },
143
+ resourceConfig : map [string ]* string {},
144
+ wantFields : map [string ]string {
145
+ string (semconv .ServiceNameKey ): "mycommand" ,
146
+ string (semconv .ServiceVersionKey ): "1.0.0" ,
147
+ string (semconv .ServiceInstanceIDKey ): "" ,
148
+ },
137
149
},
138
- }
139
-
140
- cfg := Config {
141
- Logs : LogsConfig {
142
- Level : zapcore .InfoLevel ,
143
- Encoding : "json" ,
150
+ {
151
+ name : "override service.name" ,
152
+ buildInfo : component.BuildInfo {
153
+ Command : "mycommand" ,
154
+ Version : "1.0.0" ,
155
+ },
156
+ resourceConfig : map [string ]* string {
157
+ string (semconv .ServiceNameKey ): ptr ("custom-service" ),
158
+ },
159
+ wantFields : map [string ]string {
160
+ string (semconv .ServiceNameKey ): "custom-service" ,
161
+ string (semconv .ServiceVersionKey ): "1.0.0" ,
162
+ string (semconv .ServiceInstanceIDKey ): "" ,
163
+ },
144
164
},
145
- Resource : map [string ]* string {
146
- "myfield" : ptr ("myvalue" ),
165
+ {
166
+ name : "override service.version" ,
167
+ buildInfo : component.BuildInfo {
168
+ Command : "mycommand" ,
169
+ Version : "1.0.0" ,
170
+ },
171
+ resourceConfig : map [string ]* string {
172
+ string (semconv .ServiceVersionKey ): ptr ("2.0.0" ),
173
+ },
174
+ wantFields : map [string ]string {
175
+ string (semconv .ServiceNameKey ): "mycommand" ,
176
+ string (semconv .ServiceVersionKey ): "2.0.0" ,
177
+ string (semconv .ServiceInstanceIDKey ): "" ,
178
+ },
179
+ },
180
+ {
181
+ name : "custom field with auto-populated" ,
182
+ buildInfo : component.BuildInfo {
183
+ Command : "mycommand" ,
184
+ Version : "1.0.0" ,
185
+ },
186
+ resourceConfig : map [string ]* string {
187
+ "custom.field" : ptr ("custom-value" ),
188
+ },
189
+ wantFields : map [string ]string {
190
+ string (semconv .ServiceNameKey ): "mycommand" ,
191
+ string (semconv .ServiceVersionKey ): "1.0.0" ,
192
+ string (semconv .ServiceInstanceIDKey ): "" , // Just check presence
193
+ "custom.field" : "custom-value" ,
194
+ },
195
+ },
196
+ {
197
+ name : "resource with no attributes" ,
198
+ buildInfo : component.BuildInfo {},
199
+ resourceConfig : nil ,
200
+ wantFields : nil ,
147
201
},
148
202
}
149
203
150
- mylogger , _ , _ := newLogger (set , cfg )
204
+ for _ , tt := range tests {
205
+ t .Run (tt .name , func (t * testing.T ) {
206
+ observerCore , observedLogs := observer .New (zap .InfoLevel )
151
207
152
- mylogger .Info ("Test log message" )
153
- require .Len (t , observedLogs .All (), 1 )
208
+ set := Settings {
209
+ ZapOptions : []zap.Option {
210
+ zap .WrapCore (func (core zapcore.Core ) zapcore.Core {
211
+ return zapcore .NewTee (core , observerCore )
212
+ }),
213
+ },
214
+ }
215
+ if tt .wantFields != nil {
216
+ set .Resource = resource .New (tt .buildInfo , tt .resourceConfig )
217
+ }
154
218
155
- entry := observedLogs .All ()[0 ]
156
- assert .Equal (t , "resource" , entry .Context [0 ].Key )
157
- dict := entry .Context [0 ].Interface .(zapcore.ObjectMarshaler )
158
- enc := zapcore .NewMapObjectEncoder ()
159
- require .NoError (t , dict .MarshalLogObject (enc ))
160
- require .Equal (t , "myvalue" , enc .Fields ["myfield" ])
219
+ cfg := Config {
220
+ Logs : LogsConfig {
221
+ Level : zapcore .InfoLevel ,
222
+ Encoding : "json" ,
223
+ },
224
+ }
225
+
226
+ mylogger , _ , _ := newLogger (set , cfg )
227
+ mylogger .Info ("Test log message" )
228
+ require .Len (t , observedLogs .All (), 1 )
229
+
230
+ entry := observedLogs .All ()[0 ]
231
+ if tt .wantFields == nil {
232
+ assert .Empty (t , entry .Context )
233
+ return
234
+ }
235
+
236
+ assert .Equal (t , "resource" , entry .Context [0 ].Key )
237
+ dict := entry .Context [0 ].Interface .(zapcore.ObjectMarshaler )
238
+ enc := zapcore .NewMapObjectEncoder ()
239
+ require .NoError (t , dict .MarshalLogObject (enc ))
240
+
241
+ // Verify all expected fields
242
+ for k , v := range tt .wantFields {
243
+ if k == string (semconv .ServiceInstanceIDKey ) {
244
+ // For service.instance.id just verify it exists since it's auto-generated
245
+ assert .Contains (t , enc .Fields , k )
246
+ } else {
247
+ assert .Equal (t , v , enc .Fields [k ])
248
+ }
249
+ }
250
+ })
251
+ }
161
252
}
162
253
163
254
func TestOTLPLogExport (t * testing.T ) {
0 commit comments