/
event.go
194 lines (164 loc) · 4.7 KB
/
event.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
// © 2023 SolarWinds Worldwide, LLC. All rights reserved.
//
// 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 reporter provides a low-level API for creating and reporting events for
// distributed tracing with SolarWinds Observability.
package reporter
import (
"encoding/hex"
"fmt"
"github.com/solarwinds/apm-go/internal/bson"
"github.com/solarwinds/apm-go/internal/constants"
"github.com/solarwinds/apm-go/internal/host"
"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/rand"
"github.com/solarwinds/apm-go/internal/swotel/semconv"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"strings"
"time"
)
type opID [8]byte
// Label is a required event attribute.
type Label int
const (
LabelUnset Label = iota
LabelEntry
LabelExit
LabelInfo
LabelError
)
func (l Label) AsString() string {
switch l {
case LabelEntry:
return constants.EntryLabel
case LabelError:
return constants.ErrorLabel
case LabelExit:
return constants.ExitLabel
case LabelInfo:
return constants.InfoLabel
}
return constants.UnknownLabel
}
type Event interface {
AddKV(attribute.KeyValue)
AddKVs([]attribute.KeyValue)
SetLabel(Label)
SetLayer(string)
SetParent(trace.SpanID)
GetXTrace() string
GetSwTraceContext() string
ToBson() []byte
}
type event struct {
taskID trace.TraceID
opID [8]byte
t time.Time
kvs []attribute.KeyValue
label Label
layer string
parent trace.SpanID
}
func NewEvent(tid trace.TraceID, oid opID, t time.Time) Event {
return &event{
taskID: tid,
opID: oid,
t: t,
}
}
func NewEventWithRandomOpID(tid trace.TraceID, t time.Time) Event {
oid := opID{0}
rand.Random(oid[:])
return NewEvent(tid, oid, t)
}
func (e *event) SetLabel(label Label) {
e.label = label
}
func (e *event) SetLayer(layer string) {
e.layer = layer
}
func (e *event) SetParent(spanID trace.SpanID) {
e.parent = spanID
}
func (e *event) AddKV(kv attribute.KeyValue) {
e.kvs = append(e.kvs, kv)
}
func (e *event) AddKVs(kvs []attribute.KeyValue) {
e.kvs = append(e.kvs, kvs...)
}
func (e *event) GetSwTraceContext() string {
// For now the version and flags are always 00 and 01, respectively
return fmt.Sprintf("00-%s-%s-01", e.taskID.String(), hex.EncodeToString(e.opID[:]))
}
func (e *event) GetXTrace() string {
tid := strings.ToUpper(e.taskID.String())
oid := strings.ToUpper(hex.EncodeToString(e.opID[:]))
return fmt.Sprintf("2B%s00000000%s01", tid, oid)
}
func (e *event) ToBson() []byte {
buf := bson.NewBuffer()
buf.AppendString("sw.trace_context", e.GetSwTraceContext())
buf.AppendString("X-Trace", e.GetXTrace())
buf.AppendInt64("Timestamp_u", e.t.UnixMicro())
buf.AppendString("Hostname", host.Hostname())
buf.AppendInt("PID", host.PID())
if e.label != LabelUnset {
buf.AppendString(constants.Label, e.label.AsString())
}
if e.layer != "" {
buf.AppendString(constants.Layer, e.layer)
}
if e.parent.IsValid() {
hx := e.parent.String()
buf.AppendString(constants.Edge, strings.ToUpper(hx))
buf.AppendString("sw.parent_span_id", hx)
}
for _, kv := range e.kvs {
if err := buf.AddKV(kv); err != nil {
log.Warningf("could not add kv", kv, err)
}
}
buf.Finish()
return buf.GetBuf()
}
func CreateEntryEvent(ctx trace.SpanContext, t time.Time, parent trace.SpanContext) Event {
evt := NewEvent(ctx.TraceID(), opID(ctx.SpanID()), t)
if parent.IsValid() {
evt.SetParent(parent.SpanID())
}
evt.SetLabel(LabelEntry)
return evt
}
func createNonEntryEvent(ctx trace.SpanContext, t time.Time, label Label) Event {
evt := NewEventWithRandomOpID(ctx.TraceID(), t)
evt.SetParent(ctx.SpanID())
evt.SetLabel(label)
return evt
}
func CreateExitEvent(ctx trace.SpanContext, t time.Time) Event {
return createNonEntryEvent(ctx, t, LabelExit)
}
func EventFromOtelEvent(ctx trace.SpanContext, evt sdktrace.Event) Event {
if evt.Name == semconv.ExceptionEventName {
return CreateExceptionEvent(ctx, evt.Time)
}
return CreateInfoEvent(ctx, evt.Time)
}
func CreateInfoEvent(ctx trace.SpanContext, t time.Time) Event {
return createNonEntryEvent(ctx, t, LabelInfo)
}
func CreateExceptionEvent(ctx trace.SpanContext, t time.Time) Event {
return createNonEntryEvent(ctx, t, LabelError)
}