-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.go
71 lines (53 loc) · 1.6 KB
/
generator.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
package generator
import (
"log/slog"
"math/rand"
"time"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
"google.golang.org/protobuf/proto"
)
type Generator struct {
*IDGenerator
staticResourceAttributes []*commonpb.KeyValue
}
// NewGenerator creates a Generator instance which can create supported OpenTelemetry signals.
func NewGenerator() *Generator {
return &Generator{
IDGenerator: &IDGenerator{
randSource: rand.New(rand.NewSource(time.Now().UnixNano())),
},
}
}
func (g *Generator) SetStaticResourceAttributes(attrs map[string]interface{}) {
g.staticResourceAttributes = ToAttributes(attrs)
}
func (g *Generator) ExportLogsServiceRequest(config LogConfig) []byte {
request := ExportLogsServiceRequest(g.staticResourceAttributes, config)
data, err := proto.Marshal(request)
if err != nil {
slog.Error("Failed to marshal ExportLogsServiceRequest", "error", err)
return nil
}
return data
}
func (g *Generator) ExportMetricsServiceRequest(config MetricConfig) []byte {
request := ExportMetricsServiceRequest(g.staticResourceAttributes, config)
data, err := proto.Marshal(request)
if err != nil {
slog.Error("Failed to marshal ExportMetricsServiceRequest", "error", err)
return nil
}
return data
}
func (g *Generator) ExportTraceServiceRequest(config TraceConfig) []byte {
request := ExportTraceServiceRequest(g.staticResourceAttributes, config)
data, err := proto.Marshal(request)
if err != nil {
slog.Error("Failed to marshal ExportTraceServiceRequest", "error", err)
return nil
}
return data
}
func (g *Generator) TimeNowUnixNano() int64 {
return time.Now().UnixNano()
}