-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
201 lines (166 loc) · 5.73 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
192
193
194
195
196
197
198
199
200
201
// Copyright 2022 Tyler Yahn (MrAlias)
//
// 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 otlpr
import (
"context"
"github.com/MrAlias/otlpr/internal"
"github.com/go-logr/logr"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/resource"
collpb "go.opentelemetry.io/proto/otlp/collector/logs/v1"
cpb "go.opentelemetry.io/proto/otlp/common/v1"
lpb "go.opentelemetry.io/proto/otlp/logs/v1"
rpb "go.opentelemetry.io/proto/otlp/resource/v1"
"google.golang.org/grpc"
)
// New returns a new logr Logger that will export logs over conn using OTLP.
// The conn is expected to be ready to use when passed. If conn is nil a
// discard logger is returned.
func New(conn *grpc.ClientConn) logr.Logger {
return NewWithOptions(conn, Options{})
}
// NewWithOptions returns a new logr Logger that will export logs over conn using OTLP. See New for details.
func NewWithOptions(conn *grpc.ClientConn, opts Options) logr.Logger {
if conn == nil {
return logr.Discard()
}
if opts.Depth < 0 {
opts.Depth = 0
}
fopts := internal.Options{
LogCaller: internal.MessageClass(opts.LogCaller),
LogCallerFunc: opts.LogCallerFunc,
}
l := &logSink{
client: collpb.NewLogsServiceClient(conn),
formatter: internal.NewFormatter(fopts),
}
l.batcher = opts.Batcher.start(l.export)
// For skip our own logSink.Info/Error.
l.formatter.AddCallDepth(1 + opts.Depth)
return logr.New(l)
}
// Options carries parameters which influence the way logs are generated.
type Options struct {
// Depth biases the assumed number of call frames to the "true" caller.
// This is useful when the calling code calls a function which then calls
// stdr (e.g. a logging shim to another API). Values less than zero will
// be treated as zero.
Depth int
// LogCaller tells otlpr to add a "caller" key to some or all log lines.
LogCaller MessageClass
// LogCallerFunc tells otlpr to also log the calling function name. This
// has no effect if caller logging is not enabled (see Options.LogCaller).
LogCallerFunc bool
// Batcher tells otlpr to batch log messages with the provided Batcher
// configuration.
Batcher Batcher
}
// MessageClass indicates which category or categories of messages to consider.
type MessageClass int
const (
// None ignores all message classes.
None MessageClass = iota
// All considers all message classes.
All
// Info only considers info messages.
Info
// Error only considers error messages.
Error
)
type logSink struct {
client collpb.LogsServiceClient
batcher *batcher
formatter internal.Formatter
level int
res *rpb.Resource
resSchema string
scope *cpb.InstrumentationScope
scopeSchema string
}
var _ logr.LogSink = &logSink{}
func (l *logSink) Init(ri logr.RuntimeInfo) {
l.formatter.Init(ri)
}
func (l *logSink) Enabled(level int) bool {
return level >= l.level
}
func (l *logSink) Info(level int, msg string, keysAndValues ...interface{}) {
l.batcher.Append(l.formatter.FormatInfo(level, msg, keysAndValues))
}
func (l *logSink) Error(err error, msg string, keysAndValues ...interface{}) {
l.batcher.Append(l.formatter.FormatError(err, msg, keysAndValues))
}
func (l *logSink) WithValues(keysAndValues ...interface{}) logr.LogSink {
l.formatter.AddValues(keysAndValues)
return l
}
func (l *logSink) WithName(name string) logr.LogSink {
l.formatter.AddName(name)
return l
}
func (l *logSink) WithContext(ctx context.Context) logr.LogSink {
l.formatter.AddContext(ctx)
return l
}
func (l *logSink) WithResource(res *resource.Resource) logr.LogSink {
l.resSchema, l.res = l.formatter.FormatResource(res)
return l
}
func (l *logSink) WithScope(s instrumentation.Scope) logr.LogSink {
l.scopeSchema, l.scope = l.formatter.FormatScope(s)
return l
}
func (l *logSink) export(rec []*lpb.LogRecord) {
sl := &lpb.ScopeLogs{LogRecords: rec}
if l.scope != nil {
sl.SchemaUrl, sl.Scope = l.scopeSchema, l.scope
}
rl := &lpb.ResourceLogs{ScopeLogs: []*lpb.ScopeLogs{sl}}
if l.res != nil {
rl.SchemaUrl, rl.Resource = l.resSchema, l.res
}
_, _ = l.client.Export(context.Background(), &collpb.ExportLogsServiceRequest{
ResourceLogs: []*lpb.ResourceLogs{rl},
})
// TODO: handle partial success response.
// TODO: handle returned error (log it?).
}
// WithContext returns an updated logger that will log information about any
// span in ctx if one exists with each log message. It does nothing for loggers
// where the sink doesn't support a context.
func WithContext(l logr.Logger, ctx context.Context) logr.Logger {
if ls, ok := l.GetSink().(*logSink); ok {
l = l.WithSink(ls.WithContext(ctx))
}
return l
}
// WithResource returns an updated logger that export log information with the
// provided resource. It does nothing for loggers where the sink doesn't
// support a resource.
func WithResource(l logr.Logger, res *resource.Resource) logr.Logger {
if ls, ok := l.GetSink().(*logSink); ok {
l = l.WithSink(ls.WithResource(res))
}
return l
}
// WithScope returns an updated logger that export log information with the
// provided instrumentation scope. It does nothing for loggers where the sink
// doesn't support a resource.
func WithScope(l logr.Logger, scope instrumentation.Scope) logr.Logger {
if ls, ok := l.GetSink().(*logSink); ok {
l = l.WithSink(ls.WithScope(scope))
}
return l
}