/
exporter.go
160 lines (135 loc) · 3.85 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright Agoda Services Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stdoutlogs
import (
"context"
sdk "github.com/agoda-com/opentelemetry-logs-go/sdk/logs"
"io"
"strings"
"sync"
"time"
)
var _ sdk.LogRecordExporter = &Exporter{}
// NewExporter creates an Exporter with the passed options.
func NewExporter(options ...Option) (*Exporter, error) {
cfg, err := newConfig(options...)
if err != nil {
return nil, err
}
return &Exporter{
writer: cfg.Writer,
}, nil
}
// Exporter is an implementation of logs.LogRecordSyncer that writes spans to stdout.
type Exporter struct {
writer io.Writer
encoderMu sync.Mutex
stoppedMu sync.RWMutex
stopped bool
}
// Export writes logs in json format to stdout.
func (e *Exporter) Export(ctx context.Context, logs []sdk.ReadableLogRecord) error {
e.stoppedMu.RLock()
stopped := e.stopped
e.stoppedMu.RUnlock()
if stopped {
return nil
}
if len(logs) == 0 {
return nil
}
logRecords := logRecordsFromReadableLogRecords(logs)
e.encoderMu.Lock()
defer e.encoderMu.Unlock()
for _, lr := range logRecords {
var logMessageBuilder strings.Builder
logMessageBuilder.WriteString(lr.ObservedTimestamp.Format(time.RFC3339))
logMessageBuilder.WriteString(" ")
logMessageBuilder.WriteString(lr.getSeverityText())
logMessageBuilder.WriteString(" ")
if lr.Body != nil {
logMessageBuilder.WriteString(*lr.Body)
logMessageBuilder.WriteString(" ")
}
if lr.TraceId != nil || lr.SpanId != nil {
logMessageBuilder.WriteString(": ")
if lr.TraceId != nil {
traceId := *lr.TraceId
logMessageBuilder.WriteString("traceId=")
logMessageBuilder.WriteString(traceId.String())
logMessageBuilder.WriteString(" ")
}
if lr.SpanId != nil {
spanId := *lr.SpanId
logMessageBuilder.WriteString("spanId=")
logMessageBuilder.WriteString(spanId.String())
logMessageBuilder.WriteString(" ")
}
}
if lr.InstrumentationScope != nil {
logMessageBuilder.WriteString("[scopeInfo: ")
scope := *lr.InstrumentationScope
logMessageBuilder.WriteString(scope.Name)
if scope.Version != "" {
logMessageBuilder.WriteString(":")
logMessageBuilder.WriteString(scope.Version)
}
logMessageBuilder.WriteString("] ")
}
attributes := lr.Resource.Attributes()
if lr.Attributes != nil {
attributes = append(attributes, *lr.Attributes...)
}
if len(attributes) > 0 {
logMessageBuilder.WriteString("{")
for i, a := range attributes {
logMessageBuilder.WriteString(string(a.Key))
logMessageBuilder.WriteString("=")
logMessageBuilder.WriteString(a.Value.AsString())
if i < len(attributes)-1 {
logMessageBuilder.WriteString(", ")
}
}
logMessageBuilder.WriteString("}")
}
// Write EOL
logMessageBuilder.Write([]byte("\n"))
// Print logRecords, one by one
_, err := e.writer.Write([]byte(logMessageBuilder.String()))
if err != nil {
return err
}
}
return nil
}
// Shutdown is called to stop the exporter, it performs no action.
func (e *Exporter) Shutdown(ctx context.Context) error {
e.stoppedMu.Lock()
e.stopped = true
e.stoppedMu.Unlock()
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}
// MarshalLog is the marshaling function used by the logging system to represent this exporter.
func (e *Exporter) MarshalLog() interface{} {
return struct {
Type string
WithTimestamps bool
}{
Type: "stdout",
}
}