-
Notifications
You must be signed in to change notification settings - Fork 3
/
stdout.go
97 lines (91 loc) · 2.49 KB
/
stdout.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
package stdout
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/go-bamboo/pkg/net/ip"
"github.com/go-bamboo/pkg/otel"
otelx "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
func init() {
otel.Register("Stdout:Traces", NewTracerProvider)
otel.Register("Stdout:Metrics", NewMeterProvider)
}
func NewTracerProvider(c *otel.Conf, serviceName string, uuid string) (err error) {
res := tracesdk.WithResource(resource.NewSchemaless(
semconv.ServiceNameKey.String(serviceName),
semconv.ServiceInstanceIDKey.String(uuid),
semconv.ProcessPIDKey.Int(os.Getpid()),
attribute.String("environment", "development"),
attribute.String("ip", ip.InternalIP()),
))
sampler := tracesdk.WithSampler(tracesdk.AlwaysSample())
var w io.Writer = os.Stdout
if len(c.Endpoint) > 0 {
if c.Endpoint == "no" {
var n noOutput = 0
w = &n
} else {
f, err := os.OpenFile(c.Endpoint, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModeAppend)
if err != nil {
return err
}
w = f
}
}
exp, err := stdouttrace.New(
stdouttrace.WithWriter(w),
stdouttrace.WithPrettyPrint(),
)
if err != nil {
return fmt.Errorf("creating stdout exporter: %w", err)
}
tp := tracesdk.NewTracerProvider(
sampler,
// Record information about this application in an Resource.
res,
// Always be sure to batch in production.
tracesdk.WithSpanProcessor(tracesdk.NewBatchSpanProcessor(exp)),
)
otelx.SetTracerProvider(tp)
return nil
}
func NewMeterProvider(c *otel.Conf, serviceName string, uuid string) (err error) {
var reader metric.Reader
var w io.Writer = os.Stdout
if len(c.Endpoint) > 0 {
if c.Endpoint == "no" {
var n noOutput = 0
w = &n
} else {
f, err := os.OpenFile(c.Endpoint, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModeAppend)
if err != nil {
panic(err)
}
w = f
}
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
exp, err := stdoutmetric.New(stdoutmetric.WithEncoder(enc))
if err != nil {
return err
}
reader = metric.NewPeriodicReader(exp)
sdk := metric.NewMeterProvider(
metric.WithResource(resource.NewSchemaless(
semconv.ServiceNameKey.String(serviceName),
)),
metric.WithReader(reader),
)
otelx.SetMeterProvider(sdk)
return nil
}