| 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 |
"encoding/json" |
| 8 |
"errors" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/log" |
| 12 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 13 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 14 |
"go.opentelemetry.io/otel/sdk/resource" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
) |
| 17 |
|
| 18 |
func newValue(v log.Value) value { |
| 19 |
return value{Value: v} |
| 20 |
} |
| 21 |
|
| 22 |
type value struct { |
| 23 |
log.Value |
| 24 |
} |
| 25 |
|
| 26 |
// MarshalJSON implements a custom marshal function to encode log.Value. |
| 27 |
func (v value) MarshalJSON() ([]byte, error) { |
| 28 |
var jsonVal struct { |
| 29 |
Type string |
| 30 |
Value any |
| 31 |
} |
| 32 |
jsonVal.Type = v.Kind().String() |
| 33 |
|
| 34 |
switch v.Kind() { |
| 35 |
case log.KindString: |
| 36 |
jsonVal.Value = v.AsString() |
| 37 |
case log.KindInt64: |
| 38 |
jsonVal.Value = v.AsInt64() |
| 39 |
case log.KindFloat64: |
| 40 |
jsonVal.Value = v.AsFloat64() |
| 41 |
case log.KindBool: |
| 42 |
jsonVal.Value = v.AsBool() |
| 43 |
case log.KindBytes: |
| 44 |
jsonVal.Value = v.AsBytes() |
| 45 |
case log.KindMap: |
| 46 |
m := v.AsMap() |
| 47 |
values := make([]keyValue, 0, len(m)) |
| 48 |
for _, kv := range m { |
| 49 |
values = append(values, keyValue{ |
| 50 |
Key: kv.Key, |
| 51 |
Value: newValue(kv.Value), |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
jsonVal.Value = values |
| 56 |
case log.KindSlice: |
| 57 |
s := v.AsSlice() |
| 58 |
values := make([]value, 0, len(s)) |
| 59 |
for _, e := range s { |
| 60 |
values = append(values, newValue(e)) |
| 61 |
} |
| 62 |
|
| 63 |
jsonVal.Value = values |
| 64 |
case log.KindEmpty: |
| 65 |
jsonVal.Value = nil |
| 66 |
default: |
| 67 |
return nil, errors.New("invalid Kind") |
| 68 |
} |
| 69 |
|
| 70 |
return json.Marshal(jsonVal) |
| 71 |
} |
| 72 |
|
| 73 |
type keyValue struct { |
| 74 |
Key string |
| 75 |
Value value |
| 76 |
} |
| 77 |
|
| 78 |
// recordJSON is a JSON-serializable representation of a Record. |
| 79 |
type recordJSON struct { |
| 80 |
Timestamp *time.Time `json:",omitempty"` |
| 81 |
ObservedTimestamp *time.Time `json:",omitempty"` |
| 82 |
EventName string `json:",omitempty"` |
| 83 |
Severity log.Severity |
| 84 |
SeverityText string |
| 85 |
Body value |
| 86 |
Attributes []keyValue |
| 87 |
TraceID trace.TraceID |
| 88 |
SpanID trace.SpanID |
| 89 |
TraceFlags trace.TraceFlags |
| 90 |
Resource *resource.Resource |
| 91 |
Scope instrumentation.Scope |
| 92 |
DroppedAttributes int |
| 93 |
} |
| 94 |
|
| 95 |
func (e *Exporter) newRecordJSON(r sdklog.Record) recordJSON { |
| 96 |
res := r.Resource() |
| 97 |
newRecord := recordJSON{ |
| 98 |
EventName: r.EventName(), |
| 99 |
Severity: r.Severity(), |
| 100 |
SeverityText: r.SeverityText(), |
| 101 |
Body: newValue(r.Body()), |
| 102 |
|
| 103 |
TraceID: r.TraceID(), |
| 104 |
SpanID: r.SpanID(), |
| 105 |
TraceFlags: r.TraceFlags(), |
| 106 |
|
| 107 |
Attributes: make([]keyValue, 0, r.AttributesLen()), |
| 108 |
|
| 109 |
Resource: res, |
| 110 |
Scope: r.InstrumentationScope(), |
| 111 |
|
| 112 |
DroppedAttributes: r.DroppedAttributes(), |
| 113 |
} |
| 114 |
|
| 115 |
r.WalkAttributes(func(kv log.KeyValue) bool { |
| 116 |
newRecord.Attributes = append(newRecord.Attributes, keyValue{ |
| 117 |
Key: kv.Key, |
| 118 |
Value: newValue(kv.Value), |
| 119 |
}) |
| 120 |
return true |
| 121 |
}) |
| 122 |
|
| 123 |
if e.timestamps { |
| 124 |
timestamp := r.Timestamp() |
| 125 |
newRecord.Timestamp = ×tamp |
| 126 |
|
| 127 |
observedTimestamp := r.ObservedTimestamp() |
| 128 |
newRecord.ObservedTimestamp = &observedTimestamp |
| 129 |
} |
| 130 |
|
| 131 |
return newRecord |
| 132 |
} |