-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotel_sugar.go
173 lines (147 loc) · 4.97 KB
/
otel_sugar.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
// MIT License
//
// Copyright (c) 2023 Wilbur Carmon II
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package otzap
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// AddDebugEvent simplifies span.debug(msg)
// AddDebugEvent adds an event to the span
// Any related attributes should be added to the span
func AddDebugEvent(span trace.Span, msg string, err ...error) {
span.AddEvent(
msg,
trace.WithAttributes(attribute.String(defaultLevelKey, "debug")))
RecordErrors(span, err...)
}
// AddInfoEvent simplifies span.info(msg)
// AddInfoEvent adds an event to the span
// Any related attributes should be added to the span
func AddInfoEvent(span trace.Span, msg string, err ...error) {
span.AddEvent(
msg,
trace.WithAttributes(attribute.String(defaultLevelKey, "info")))
RecordErrors(span, err...)
}
// AddWarnEvent simplifies span.warn(msg)
// AddWarnEvent adds an event to the span
// Any related attributes should be added to the span
func AddWarnEvent(span trace.Span, msg string, err ...error) {
span.AddEvent(
msg,
trace.WithAttributes(attribute.String(defaultLevelKey, "warn")))
RecordErrors(span, err...)
}
// AddErrorEvent simplifies span.error(msg)
// AddErrorEvent adds an event to the span
// Any related attributes should be added to the span
func AddErrorEvent(span trace.Span, msg string, err ...error) {
span.AddEvent(
msg,
trace.WithAttributes(attribute.String(defaultLevelKey, "error")))
RecordErrors(span, err...)
}
// AddFatalEvent simplifies span.fatal(msg)
// AddFatalEvent adds an event to the span
// Any related attributes should be added to the span
func AddFatalEvent(span trace.Span, msg string, err ...error) {
span.AddEvent(
msg,
trace.WithAttributes(attribute.String(defaultLevelKey, "fatal")))
RecordErrors(span, err...)
}
// AddDebugEventToSpan retrieves span from context and
// adds error to span when present
//
// AddDebugEventToSpan sets level to debug
func AddDebugEventToSpan(ctx context.Context, msg string, err ...error) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
// eg. noop span or missing
return
}
AddDebugEvent(span, msg, err...)
}
// AddInfoEventToSpan retrieves span from context and
// adds error to span when present
//
// AddInfoEventToSpan sets level to info
func AddInfoEventToSpan(ctx context.Context, msg string, err ...error) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
// eg. noop span or missing
return
}
AddInfoEvent(span, msg, err...)
}
// AddWarnEventToSpan retrieves span from context and
// adds error to span when present
//
// AddWarnEventToSpan sets level to warn
func AddWarnEventToSpan(ctx context.Context, msg string, err ...error) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
// eg. noop span or missing
return
}
AddWarnEvent(span, msg, err...)
}
// AddErrorEventToSpan retrieves span from context and
// adds error to span when present
//
// AddErrorEventToSpan sets level to error
func AddErrorEventToSpan(ctx context.Context, msg string, err ...error) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
// eg. noop span or missing
return
}
AddErrorEvent(span, msg, err...)
}
// AddFatalEventToSpan retrieves span from context and
// adds error to span when present
//
// AddFatalEventToSpan sets level to fatal
func AddFatalEventToSpan(ctx context.Context, msg string, err ...error) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
// eg. noop span or missing
return
}
AddFatalEvent(span, msg, err...)
}
// RecordErrors is a low-level method, prefer the methods above
// RecordErrors simplifies recording errors correctly for Jaeger,
// Google Cloud Trace, AWS XRay, etc
func RecordErrors(span trace.Span, err ...error) {
if err == nil || len(err) == 0 {
return
}
span.SetStatus(codes.Error, "")
// Jaeger needs this
span.SetAttributes(attribute.Bool("error", true))
for _, err := range err {
span.RecordError(err)
}
}