Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/receiver/prometheus/internal/logger.go
5414 views
1
// Copyright The OpenTelemetry Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
16
17
import (
18
gokitLog "github.com/go-kit/log"
19
"github.com/go-kit/log/level"
20
"go.uber.org/zap"
21
)
22
23
const (
24
levelKey = "level"
25
msgKey = "msg"
26
errKey = "err"
27
)
28
29
// NewZapToGokitLogAdapter create an adapter for zap.Logger to gokitLog.Logger
30
func NewZapToGokitLogAdapter(logger *zap.Logger) gokitLog.Logger {
31
// need to skip two levels in order to get the correct caller
32
// one for this method, the other for gokitLog
33
logger = logger.WithOptions(zap.AddCallerSkip(2))
34
return &zapToGokitLogAdapter{l: logger.Sugar()}
35
}
36
37
type zapToGokitLogAdapter struct {
38
l *zap.SugaredLogger
39
}
40
41
type logData struct {
42
level level.Value
43
msg string
44
otherFields []interface{}
45
}
46
47
func (w *zapToGokitLogAdapter) Log(keyvals ...interface{}) error {
48
// expecting key value pairs, the number of items need to be even
49
if len(keyvals)%2 == 0 {
50
// Extract log level and message and log them using corresponding zap function
51
ld := extractLogData(keyvals)
52
logFunc := levelToFunc(w.l, ld.level)
53
logFunc(ld.msg, ld.otherFields...)
54
} else {
55
// in case something goes wrong
56
w.l.Info(keyvals...)
57
}
58
return nil
59
}
60
61
func extractLogData(keyvals []interface{}) logData {
62
ld := logData{
63
level: level.InfoValue(), // default
64
}
65
66
for i := 0; i < len(keyvals); i += 2 {
67
key := keyvals[i]
68
val := keyvals[i+1]
69
70
if l, ok := matchLogLevel(key, val); ok {
71
ld.level = l
72
continue
73
}
74
75
if m, ok := matchLogMessage(key, val); ok {
76
ld.msg = m
77
continue
78
}
79
80
if err, ok := matchError(key, val); ok {
81
ld.otherFields = append(ld.otherFields, zap.Error(err))
82
continue
83
}
84
85
ld.otherFields = append(ld.otherFields, key, val)
86
}
87
88
return ld
89
}
90
91
// check if a given key-value pair represents go-kit log message and return it
92
func matchLogMessage(key interface{}, val interface{}) (string, bool) {
93
if strKey, ok := key.(string); !ok || strKey != msgKey {
94
return "", false
95
}
96
97
msg, ok := val.(string)
98
if !ok {
99
return "", false
100
}
101
return msg, true
102
}
103
104
// check if a given key-value pair represents go-kit log level and return it
105
func matchLogLevel(key interface{}, val interface{}) (level.Value, bool) {
106
strKey, ok := key.(string)
107
if !ok || strKey != levelKey {
108
return nil, false
109
}
110
111
levelVal, ok := val.(level.Value)
112
if !ok {
113
return nil, false
114
}
115
return levelVal, true
116
}
117
118
//revive:disable:error-return
119
120
// check if a given key-value pair represents an error and return it
121
func matchError(key interface{}, val interface{}) (error, bool) {
122
strKey, ok := key.(string)
123
if !ok || strKey != errKey {
124
return nil, false
125
}
126
127
err, ok := val.(error)
128
if !ok {
129
return nil, false
130
}
131
return err, true
132
}
133
134
//revive:enable:error-return
135
136
// find a matching zap logging function to be used for a given level
137
func levelToFunc(logger *zap.SugaredLogger, lvl level.Value) func(string, ...interface{}) {
138
switch lvl {
139
case level.DebugValue():
140
return logger.Debugw
141
case level.InfoValue():
142
return logger.Infow
143
case level.WarnValue():
144
return logger.Warnw
145
case level.ErrorValue():
146
return logger.Errorw
147
}
148
149
// default
150
return logger.Infow
151
}
152
153
var _ gokitLog.Logger = (*zapToGokitLogAdapter)(nil)
154
155