forked from agoda-com/opentelemetry-logs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
191 lines (169 loc) · 6.59 KB
/
logger.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
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 logs
import (
"github.com/agoda-com/opentelemetry-logs-go/logs"
"github.com/agoda-com/opentelemetry-logs-go/semconv"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/trace"
"sync"
"time"
)
type logger struct {
provider *LoggerProvider
instrumentationScope instrumentation.Scope
}
var _ logs.Logger = &logger{}
func (l logger) Emit(logRecord logs.LogRecord) {
lps := l.provider.getLogsProcessors()
if len(lps) == 0 {
return
}
pr, err := resource.Merge(l.provider.resource, logRecord.Resource())
if err != nil {
return
}
elr := &exportableLogRecord{
timestamp: logRecord.Timestamp(),
observedTimestamp: logRecord.ObservedTimestamp(),
traceId: logRecord.TraceId(),
spanId: logRecord.SpanId(),
traceFlags: logRecord.TraceFlags(),
severityText: logRecord.SeverityText(),
severityNumber: logRecord.SeverityNumber(),
body: logRecord.Body(),
resource: pr,
instrumentationScope: logRecord.InstrumentationScope(),
attributes: logRecord.Attributes(),
}
for _, lp := range lps {
lp.lp.OnEmit(elr)
}
}
// ReadableLogRecord Log structure
// see https://opentelemetry.io/docs/specs/otel/logs/data-model/#log-and-event-record-definition
// see https://opentelemetry.io/docs/specs/otel/logs/sdk/#readablelogrecord
type ReadableLogRecord interface {
// Timestamp Time when the event occurred.
Timestamp() *time.Time
// ObservedTimestamp Time when the event was observed.
ObservedTimestamp() time.Time
// TraceId Request trace id.
TraceId() *trace.TraceID
// SpanId Request span id.
SpanId() *trace.SpanID
// TraceFlags W3C trace flag.
TraceFlags() *trace.TraceFlags
// SeverityText This is the original string representation of the severityNumber as it is known at the source
SeverityText() *string
// SeverityNumber Numerical value of the severityNumber.
SeverityNumber() *logs.SeverityNumber
// Body The body of the log record.
Body() *string
// Resource Describes the source of the log.
Resource() *resource.Resource
// InstrumentationScope returns information about the instrumentation
// scope that created the log.
InstrumentationScope() *instrumentation.Scope
// Attributes describe the aspects of the event.
Attributes() *[]attribute.KeyValue
// A private method to prevent users implementing the
// interface and so future additions to it will not
// violate compatibility.
private()
}
type ReadWriteLogRecord interface {
SetResource(resource *resource.Resource)
// RecordException message, stacktrace, type
RecordException(*string, *string, *string)
ReadableLogRecord
}
// exportableLogRecord is an implementation of the OpenTelemetry Log API
// representing the individual component of a log.
type exportableLogRecord struct {
// mu protects the contents of this log.
mu sync.Mutex
timestamp *time.Time
observedTimestamp time.Time
traceId *trace.TraceID
spanId *trace.SpanID
traceFlags *trace.TraceFlags
severityText *string
severityNumber *logs.SeverityNumber
body *string
resource *resource.Resource
instrumentationScope *instrumentation.Scope
attributes *[]attribute.KeyValue
}
// newReadWriteLogRecord create
// This method may change in the future
func newReadWriteLogRecord(
ctx *trace.SpanContext,
body *string,
severityText *string,
severityNumber *logs.SeverityNumber,
resource *resource.Resource,
instrumentationScope *instrumentation.Scope,
attributes *[]attribute.KeyValue,
timestamp *time.Time) ReadWriteLogRecord {
traceId := ctx.TraceID()
spanId := ctx.SpanID()
traceFlags := ctx.TraceFlags()
return &exportableLogRecord{
timestamp: timestamp,
observedTimestamp: time.Now(),
traceId: &traceId,
spanId: &spanId,
traceFlags: &traceFlags,
severityText: severityText,
severityNumber: severityNumber,
body: body,
resource: resource,
instrumentationScope: instrumentationScope,
attributes: attributes,
}
}
func (r *exportableLogRecord) SetResource(resource *resource.Resource) { r.resource = resource }
// RecordException helper to add Exception related information as attributes of Log Record
// see https://opentelemetry.io/docs/specs/otel/logs/semantic_conventions/exceptions/#recording-an-exception
func (r *exportableLogRecord) RecordException(message *string, stacktrace *string, exceptionType *string) {
if message == nil && exceptionType == nil {
// one of the fields must present
return
}
if message != nil {
*r.attributes = append(*r.attributes, semconv.ExceptionMessage(*message))
}
if stacktrace != nil {
*r.attributes = append(*r.attributes, semconv.ExceptionStacktrace(*stacktrace))
}
if exceptionType != nil {
*r.attributes = append(*r.attributes, semconv.ExceptionType(*exceptionType))
}
}
func (r *exportableLogRecord) Timestamp() *time.Time { return r.timestamp }
func (r *exportableLogRecord) ObservedTimestamp() time.Time { return r.observedTimestamp }
func (r *exportableLogRecord) TraceId() *trace.TraceID { return r.traceId }
func (r *exportableLogRecord) SpanId() *trace.SpanID { return r.spanId }
func (r *exportableLogRecord) TraceFlags() *trace.TraceFlags { return r.traceFlags }
func (r *exportableLogRecord) InstrumentationScope() *instrumentation.Scope {
return r.instrumentationScope
}
func (r *exportableLogRecord) SeverityText() *string { return r.severityText }
func (r *exportableLogRecord) SeverityNumber() *logs.SeverityNumber { return r.severityNumber }
func (r *exportableLogRecord) Body() *string { return r.body }
func (r *exportableLogRecord) Resource() *resource.Resource { return r.resource }
func (r *exportableLogRecord) Attributes() *[]attribute.KeyValue { return r.attributes }
func (r *exportableLogRecord) private() {}