| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/log" |
| 19 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 20 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 21 |
"go.opentelemetry.io/otel/sdk/log/logtest" |
| 22 |
"go.opentelemetry.io/otel/sdk/resource" |
| 23 |
"go.opentelemetry.io/otel/trace" |
| 24 |
) |
| 25 |
|
| 26 |
func TestExporter(t *testing.T) { |
| 27 |
var buf bytes.Buffer |
| 28 |
now := time.Now() |
| 29 |
|
| 30 |
testCases := []struct { |
| 31 |
name string |
| 32 |
exporter *Exporter |
| 33 |
want string |
| 34 |
}{ |
| 35 |
{ |
| 36 |
name: "zero value", |
| 37 |
exporter: &Exporter{}, |
| 38 |
want: "", |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "new", |
| 42 |
exporter: func() *Exporter { |
| 43 |
defaultWriterSwap := defaultWriter |
| 44 |
defer func() { |
| 45 |
defaultWriter = defaultWriterSwap |
| 46 |
}() |
| 47 |
defaultWriter = &buf |
| 48 |
|
| 49 |
exporter, err := New() |
| 50 |
require.NoError(t, err) |
| 51 |
require.NotNil(t, exporter) |
| 52 |
|
| 53 |
return exporter |
| 54 |
}(), |
| 55 |
want: getJSON(&now), |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, tc := range testCases { |
| 60 |
t.Run(tc.name, func(t *testing.T) { |
| 61 |
// Write to buffer for testing |
| 62 |
defaultWriterSwap := defaultWriter |
| 63 |
defer func() { |
| 64 |
defaultWriter = defaultWriterSwap |
| 65 |
}() |
| 66 |
defaultWriter = &buf |
| 67 |
|
| 68 |
buf.Reset() |
| 69 |
|
| 70 |
var err error |
| 71 |
|
| 72 |
exporter := tc.exporter |
| 73 |
|
| 74 |
record := getRecord(now) |
| 75 |
|
| 76 |
// Export a record |
| 77 |
err = exporter.Export(t.Context(), []sdklog.Record{record}) |
| 78 |
assert.NoError(t, err) |
| 79 |
|
| 80 |
// Check the writer |
| 81 |
assert.Equal(t, tc.want, buf.String()) |
| 82 |
|
| 83 |
// Flush the exporter |
| 84 |
err = exporter.ForceFlush(t.Context()) |
| 85 |
assert.NoError(t, err) |
| 86 |
|
| 87 |
// Shutdown the exporter |
| 88 |
err = exporter.Shutdown(t.Context()) |
| 89 |
assert.NoError(t, err) |
| 90 |
|
| 91 |
// Export a record after shutdown, this should not be written |
| 92 |
err = exporter.Export(t.Context(), []sdklog.Record{record}) |
| 93 |
assert.NoError(t, err) |
| 94 |
|
| 95 |
// Check the writer |
| 96 |
assert.Equal(t, tc.want, buf.String()) |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestExporterExport(t *testing.T) { |
| 102 |
now := time.Now() |
| 103 |
|
| 104 |
record := getRecord(now) |
| 105 |
records := []sdklog.Record{record, record} |
| 106 |
|
| 107 |
testCases := []struct { |
| 108 |
name string |
| 109 |
options []Option |
| 110 |
ctx context.Context |
| 111 |
records []sdklog.Record |
| 112 |
wantResult string |
| 113 |
wantError error |
| 114 |
}{ |
| 115 |
{ |
| 116 |
name: "default", |
| 117 |
options: []Option{}, |
| 118 |
ctx: t.Context(), |
| 119 |
records: records, |
| 120 |
wantResult: getJSONs(&now), |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "NoRecords", |
| 124 |
options: []Option{}, |
| 125 |
ctx: t.Context(), |
| 126 |
records: nil, |
| 127 |
wantResult: "", |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "WithPrettyPrint", |
| 131 |
options: []Option{WithPrettyPrint()}, |
| 132 |
ctx: t.Context(), |
| 133 |
records: records, |
| 134 |
wantResult: getPrettyJSONs(&now), |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "WithoutTimestamps", |
| 138 |
options: []Option{WithoutTimestamps()}, |
| 139 |
ctx: t.Context(), |
| 140 |
records: records, |
| 141 |
wantResult: getJSONs(nil), |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "WithoutTimestamps and WithPrettyPrint", |
| 145 |
options: []Option{WithoutTimestamps(), WithPrettyPrint()}, |
| 146 |
ctx: t.Context(), |
| 147 |
records: records, |
| 148 |
wantResult: getPrettyJSONs(nil), |
| 149 |
}, |
| 150 |
{ |
| 151 |
name: "WithCanceledContext", |
| 152 |
ctx: func() context.Context { |
| 153 |
ctx, cancel := context.WithCancel(t.Context()) |
| 154 |
cancel() |
| 155 |
return ctx |
| 156 |
}(), |
| 157 |
records: records, |
| 158 |
wantResult: "", |
| 159 |
wantError: context.Canceled, |
| 160 |
}, |
| 161 |
} |
| 162 |
|
| 163 |
for _, tc := range testCases { |
| 164 |
t.Run(tc.name, func(t *testing.T) { |
| 165 |
// Write to buffer for testing |
| 166 |
var buf bytes.Buffer |
| 167 |
|
| 168 |
exporter, err := New(append(tc.options, WithWriter(&buf))...) |
| 169 |
assert.NoError(t, err) |
| 170 |
|
| 171 |
err = exporter.Export(tc.ctx, tc.records) |
| 172 |
assert.Equal(t, tc.wantError, err) |
| 173 |
assert.Equal(t, tc.wantResult, buf.String()) |
| 174 |
}) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func getJSON(now *time.Time) string { |
| 179 |
var timestamps string |
| 180 |
if now != nil { |
| 181 |
serializedNow, _ := json.Marshal(now) |
| 182 |
timestamps = "\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + "," |
| 183 |
} |
| 184 |
|
| 185 |
return "{" + timestamps + "\"EventName\":\"testing.event\",\"Severity\":9,\"SeverityText\":\"INFO\",\"Body\":{\"Type\":\"String\",\"Value\":\"test\"},\"Attributes\":[{\"Key\":\"key\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key2\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key3\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key4\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key5\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"bool\",\"Value\":{\"Type\":\"Bool\",\"Value\":true}}],\"TraceID\":\"0102030405060708090a0b0c0d0e0f10\",\"SpanID\":\"0102030405060708\",\"TraceFlags\":\"01\",\"Resource\":[{\"Key\":\"foo\",\"Value\":{\"Type\":\"STRING\",\"Value\":\"bar\"}}],\"Scope\":{\"Name\":\"name\",\"Version\":\"version\",\"SchemaURL\":\"https://example.com/custom-schema\",\"Attributes\":{}},\"DroppedAttributes\":10}\n" |
| 186 |
} |
| 187 |
|
| 188 |
func getJSONs(now *time.Time) string { |
| 189 |
return getJSON(now) + getJSON(now) |
| 190 |
} |
| 191 |
|
| 192 |
func getPrettyJSON(now *time.Time) string { |
| 193 |
var timestamps string |
| 194 |
if now != nil { |
| 195 |
serializedNow, _ := json.Marshal(now) |
| 196 |
timestamps = "\n\t\"Timestamp\": " + string( |
| 197 |
serializedNow, |
| 198 |
) + ",\n\t\"ObservedTimestamp\": " + string( |
| 199 |
serializedNow, |
| 200 |
) + "," |
| 201 |
} |
| 202 |
|
| 203 |
return `{` + timestamps + ` |
| 204 |
"EventName": "testing.event", |
| 205 |
"Severity": 9, |
| 206 |
"SeverityText": "INFO", |
| 207 |
"Body": { |
| 208 |
"Type": "String", |
| 209 |
"Value": "test" |
| 210 |
}, |
| 211 |
"Attributes": [ |
| 212 |
{ |
| 213 |
"Key": "key", |
| 214 |
"Value": { |
| 215 |
"Type": "String", |
| 216 |
"Value": "value" |
| 217 |
} |
| 218 |
}, |
| 219 |
{ |
| 220 |
"Key": "key2", |
| 221 |
"Value": { |
| 222 |
"Type": "String", |
| 223 |
"Value": "value" |
| 224 |
} |
| 225 |
}, |
| 226 |
{ |
| 227 |
"Key": "key3", |
| 228 |
"Value": { |
| 229 |
"Type": "String", |
| 230 |
"Value": "value" |
| 231 |
} |
| 232 |
}, |
| 233 |
{ |
| 234 |
"Key": "key4", |
| 235 |
"Value": { |
| 236 |
"Type": "String", |
| 237 |
"Value": "value" |
| 238 |
} |
| 239 |
}, |
| 240 |
{ |
| 241 |
"Key": "key5", |
| 242 |
"Value": { |
| 243 |
"Type": "String", |
| 244 |
"Value": "value" |
| 245 |
} |
| 246 |
}, |
| 247 |
{ |
| 248 |
"Key": "bool", |
| 249 |
"Value": { |
| 250 |
"Type": "Bool", |
| 251 |
"Value": true |
| 252 |
} |
| 253 |
} |
| 254 |
], |
| 255 |
"TraceID": "0102030405060708090a0b0c0d0e0f10", |
| 256 |
"SpanID": "0102030405060708", |
| 257 |
"TraceFlags": "01", |
| 258 |
"Resource": [ |
| 259 |
{ |
| 260 |
"Key": "foo", |
| 261 |
"Value": { |
| 262 |
"Type": "STRING", |
| 263 |
"Value": "bar" |
| 264 |
} |
| 265 |
} |
| 266 |
], |
| 267 |
"Scope": { |
| 268 |
"Name": "name", |
| 269 |
"Version": "version", |
| 270 |
"SchemaURL": "https://example.com/custom-schema", |
| 271 |
"Attributes": {} |
| 272 |
}, |
| 273 |
"DroppedAttributes": 10 |
| 274 |
} |
| 275 |
` |
| 276 |
} |
| 277 |
|
| 278 |
func getPrettyJSONs(now *time.Time) string { |
| 279 |
return getPrettyJSON(now) + getPrettyJSON(now) |
| 280 |
} |
| 281 |
|
| 282 |
func TestExporterShutdown(t *testing.T) { |
| 283 |
exporter, err := New() |
| 284 |
assert.NoError(t, err) |
| 285 |
|
| 286 |
assert.NoError(t, exporter.Shutdown(t.Context())) |
| 287 |
} |
| 288 |
|
| 289 |
func TestExporterForceFlush(t *testing.T) { |
| 290 |
exporter, err := New() |
| 291 |
assert.NoError(t, err) |
| 292 |
|
| 293 |
assert.NoError(t, exporter.ForceFlush(t.Context())) |
| 294 |
} |
| 295 |
|
| 296 |
func getRecord(now time.Time) sdklog.Record { |
| 297 |
traceID, _ := trace.TraceIDFromHex("0102030405060708090a0b0c0d0e0f10") |
| 298 |
spanID, _ := trace.SpanIDFromHex("0102030405060708") |
| 299 |
|
| 300 |
rf := logtest.RecordFactory{ |
| 301 |
EventName: "testing.event", |
| 302 |
Timestamp: now, |
| 303 |
ObservedTimestamp: now, |
| 304 |
Severity: log.SeverityInfo1, |
| 305 |
SeverityText: "INFO", |
| 306 |
Body: log.StringValue("test"), |
| 307 |
Attributes: []log.KeyValue{ |
| 308 |
// More than 5 attributes to test back slice |
| 309 |
log.String("key", "value"), |
| 310 |
log.String("key2", "value"), |
| 311 |
log.String("key3", "value"), |
| 312 |
log.String("key4", "value"), |
| 313 |
log.String("key5", "value"), |
| 314 |
log.Bool("bool", true), |
| 315 |
}, |
| 316 |
TraceID: traceID, |
| 317 |
SpanID: spanID, |
| 318 |
TraceFlags: trace.FlagsSampled, |
| 319 |
|
| 320 |
Resource: resource.NewWithAttributes( |
| 321 |
"https://example.com/custom-resource-schema", |
| 322 |
attribute.String("foo", "bar"), |
| 323 |
), |
| 324 |
InstrumentationScope: &instrumentation.Scope{ |
| 325 |
Name: "name", |
| 326 |
Version: "version", |
| 327 |
SchemaURL: "https://example.com/custom-schema", |
| 328 |
}, |
| 329 |
DroppedAttributes: 10, |
| 330 |
} |
| 331 |
|
| 332 |
return rf.NewRecord() |
| 333 |
} |
| 334 |
|
| 335 |
func TestExporterConcurrentSafe(t *testing.T) { |
| 336 |
testCases := []struct { |
| 337 |
name string |
| 338 |
exporter *Exporter |
| 339 |
}{ |
| 340 |
{ |
| 341 |
name: "zero value", |
| 342 |
exporter: &Exporter{}, |
| 343 |
}, |
| 344 |
{ |
| 345 |
name: "new", |
| 346 |
exporter: func() *Exporter { |
| 347 |
exporter, err := New() |
| 348 |
require.NoError(t, err) |
| 349 |
require.NotNil(t, exporter) |
| 350 |
|
| 351 |
return exporter |
| 352 |
}(), |
| 353 |
}, |
| 354 |
} |
| 355 |
|
| 356 |
for _, tc := range testCases { |
| 357 |
t.Run(tc.name, func(t *testing.T) { |
| 358 |
exporter := tc.exporter |
| 359 |
|
| 360 |
const goroutines = 10 |
| 361 |
var wg sync.WaitGroup |
| 362 |
wg.Add(goroutines) |
| 363 |
for range goroutines { |
| 364 |
go func() { |
| 365 |
defer wg.Done() |
| 366 |
err := exporter.Export(t.Context(), []sdklog.Record{{}}) |
| 367 |
assert.NoError(t, err) |
| 368 |
err = exporter.ForceFlush(t.Context()) |
| 369 |
assert.NoError(t, err) |
| 370 |
err = exporter.Shutdown(t.Context()) |
| 371 |
assert.NoError(t, err) |
| 372 |
}() |
| 373 |
} |
| 374 |
wg.Wait() |
| 375 |
}) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
func TestValueMarshalJSON(t *testing.T) { |
| 380 |
testCases := []struct { |
| 381 |
value log.Value |
| 382 |
want string |
| 383 |
}{ |
| 384 |
{ |
| 385 |
value: log.Empty("test").Value, |
| 386 |
want: `{"Type":"Empty","Value":null}`, |
| 387 |
}, |
| 388 |
{ |
| 389 |
value: log.BoolValue(true), |
| 390 |
want: `{"Type":"Bool","Value":true}`, |
| 391 |
}, |
| 392 |
{ |
| 393 |
value: log.Float64Value(3.14), |
| 394 |
want: `{"Type":"Float64","Value":3.14}`, |
| 395 |
}, |
| 396 |
{ |
| 397 |
value: log.Int64Value(42), |
| 398 |
want: `{"Type":"Int64","Value":42}`, |
| 399 |
}, |
| 400 |
{ |
| 401 |
value: log.StringValue("hello"), |
| 402 |
want: `{"Type":"String","Value":"hello"}`, |
| 403 |
}, |
| 404 |
{ |
| 405 |
value: log.BytesValue([]byte{1, 2, 3}), |
| 406 |
// The base64 encoding of []byte{1, 2, 3} is "AQID". |
| 407 |
want: `{"Type":"Bytes","Value":"AQID"}`, |
| 408 |
}, |
| 409 |
{ |
| 410 |
value: log.SliceValue( |
| 411 |
log.Empty("empty").Value, |
| 412 |
log.BoolValue(true), |
| 413 |
log.Float64Value(2.2), |
| 414 |
log.IntValue(3), |
| 415 |
log.StringValue("4"), |
| 416 |
log.BytesValue([]byte{5}), |
| 417 |
log.SliceValue( |
| 418 |
log.IntValue(6), |
| 419 |
log.MapValue( |
| 420 |
log.Int("seven", 7), |
| 421 |
), |
| 422 |
), |
| 423 |
log.MapValue( |
| 424 |
log.Int("nine", 9), |
| 425 |
), |
| 426 |
), |
| 427 |
want: `{"Type":"Slice","Value":[{"Type":"Empty","Value":null},{"Type":"Bool","Value":true},{"Type":"Float64","Value":2.2},{"Type":"Int64","Value":3},{"Type":"String","Value":"4"},{"Type":"Bytes","Value":"BQ=="},{"Type":"Slice","Value":[{"Type":"Int64","Value":6},{"Type":"Map","Value":[{"Key":"seven","Value":{"Type":"Int64","Value":7}}]}]},{"Type":"Map","Value":[{"Key":"nine","Value":{"Type":"Int64","Value":9}}]}]}`, |
| 428 |
}, |
| 429 |
{ |
| 430 |
value: log.MapValue( |
| 431 |
log.Empty("empty"), |
| 432 |
log.Bool("one", true), |
| 433 |
log.Float64("two", 2.2), |
| 434 |
log.Int("three", 3), |
| 435 |
log.String("four", "4"), |
| 436 |
log.Bytes("five", []byte{5}), |
| 437 |
log.Slice("six", |
| 438 |
log.IntValue(6), |
| 439 |
log.MapValue( |
| 440 |
log.Int("seven", 7), |
| 441 |
), |
| 442 |
), |
| 443 |
log.Map("eight", |
| 444 |
log.Int("nine", 9), |
| 445 |
), |
| 446 |
), |
| 447 |
want: `{"Type":"Map","Value":[{"Key":"empty","Value":{"Type":"Empty","Value":null}},{"Key":"one","Value":{"Type":"Bool","Value":true}},{"Key":"two","Value":{"Type":"Float64","Value":2.2}},{"Key":"three","Value":{"Type":"Int64","Value":3}},{"Key":"four","Value":{"Type":"String","Value":"4"}},{"Key":"five","Value":{"Type":"Bytes","Value":"BQ=="}},{"Key":"six","Value":{"Type":"Slice","Value":[{"Type":"Int64","Value":6},{"Type":"Map","Value":[{"Key":"seven","Value":{"Type":"Int64","Value":7}}]}]}},{"Key":"eight","Value":{"Type":"Map","Value":[{"Key":"nine","Value":{"Type":"Int64","Value":9}}]}}]}`, |
| 448 |
}, |
| 449 |
} |
| 450 |
|
| 451 |
for _, tc := range testCases { |
| 452 |
t.Run(tc.value.String(), func(t *testing.T) { |
| 453 |
got, err := json.Marshal(value{Value: tc.value}) |
| 454 |
require.NoError(t, err) |
| 455 |
assert.JSONEq(t, tc.want, string(got)) |
| 456 |
}) |
| 457 |
} |
| 458 |
} |