forked from aliyun-sls/opentelemetry-go-provider-sls
/
main.go
147 lines (123 loc) · 3.86 KB
/
main.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
// Copyright The AliyunSLS Authors
//
// 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 main
import (
"context"
"errors"
"fmt"
"math/rand"
"time"
"github.com/Junglehouse/opentelemetry-go-provider-sls/provider"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
func main() {
slsConfig, err := provider.NewConfig(provider.WithServiceName("payment"),
provider.WithServiceVersion("v0.1.0"),
provider.WithTraceExporterEndpoint("stdout"),
provider.WithMetricExporterEndpoint("stdout"),
provider.WithSLSConfig("test-project", "test-otel", "access-key-id", "access-key-secret"))
// 如果初始化失败则panic,可以替换为其他错误处理方式
if err != nil {
panic(err)
}
if err := provider.Start(slsConfig); err != nil {
panic(err)
}
defer provider.Shutdown(slsConfig)
mockTrace()
mockMetrics()
}
func mockMetrics() {
// 附加的Label信息
labels := []label.KeyValue{
label.String("label1", "value1"),
}
meter := otel.Meter("ex.com/basic")
// 观测值,用于定期获取某个计量值,回调函数每个上报周期会被调用一次
_ = metric.Must(meter).NewFloat64ValueObserver(
"randval",
func(_ context.Context, result metric.Float64ObserverResult) {
result.Observe(
rand.Float64(),
labels...,
)
},
metric.WithDescription("A random value"),
)
temperature := metric.Must(meter).NewFloat64ValueRecorder("temperature")
interrupts := metric.Must(meter).NewInt64Counter("interrupts")
ctx := context.Background()
for {
temperature.Record(ctx, 100+10*rand.NormFloat64(), labels...)
interrupts.Add(ctx, int64(rand.Intn(100)), labels...)
time.Sleep(time.Second * time.Duration(rand.Intn(10)))
}
}
func mockTrace() {
tracer := otel.Tracer("ex.com/basic")
ctx0 := context.Background()
ctx1, finish1 := tracer.Start(ctx0, "foo")
defer finish1.End()
ctx2, finish2 := tracer.Start(ctx1, "bar")
defer finish2.End()
ctx3, finish3 := tracer.Start(ctx2, "baz")
defer finish3.End()
ctx := ctx3
getSpan(ctx)
addAttribute(ctx)
addEvent(ctx)
recordException(ctx)
createChild(ctx, tracer)
}
// example of getting the current span
// 获取当前的Span
func getSpan(ctx context.Context) {
span := trace.SpanFromContext(ctx)
fmt.Printf("current span: %v\n", span)
}
// example of adding an attribute to a span
// 向Span中添加属性值
func addAttribute(ctx context.Context) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(label.KeyValue{
Key: "label-key-1",
Value: label.StringValue("label-value-1")})
}
// example of adding an event to a span
// 向Span中添加事件
func addEvent(ctx context.Context) {
span := trace.SpanFromContext(ctx)
span.AddEvent("event1", trace.WithAttributes(
label.String("event-attr1", "event-string1"),
label.Int64("event-attr2", 10)))
}
// example of recording an exception
// 记录Span结果以及错误信息
func recordException(ctx context.Context) {
span := trace.SpanFromContext(ctx)
span.RecordError(errors.New("exception has occurred"))
span.SetStatus(codes.Error, "internal error")
}
// example of creating a child span
// 创建子Span
func createChild(ctx context.Context, tracer trace.Tracer) {
// span := trace.SpanFromContext(ctx)
_, childSpan := tracer.Start(ctx, "child")
defer childSpan.End()
fmt.Printf("child span: %v\n", childSpan)
}