-
Notifications
You must be signed in to change notification settings - Fork 231
/
main.go
86 lines (73 loc) · 2.32 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
// Copyright 2021 CloudWeGo 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"
"math/rand"
"os"
"strconv"
"time"
"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
"github.com/kitex-contrib/obs-opentelemetry/provider"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
"go.opentelemetry.io/otel"
)
func main() {
klog.SetLogger(kitexlogrus.NewLogger())
klog.SetLevel(klog.LevelDebug)
serviceName := "echo-client"
p := provider.NewOpenTelemetryProvider(
provider.WithServiceName(serviceName),
// Support setting ExportEndpoint via environment variables: OTEL_EXPORTER_OTLP_ENDPOINT
provider.WithExportEndpoint(":4317"),
provider.WithInsecure(),
)
defer p.Shutdown(context.Background())
demoServerAddr, ok := os.LookupEnv("DEMO_SERVER_ENDPOINT")
if !ok {
demoServerAddr = "0.0.0.0:8181"
}
c, err := echo.NewClient(
"echo",
client.WithHostPorts(demoServerAddr),
client.WithSuite(tracing.NewClientSuite()),
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}),
)
if err != nil {
klog.Fatal(err)
}
// Yields a constantly-changing number
rand.New(rand.NewSource(time.Now().UnixNano()))
for {
call(c)
<-time.After(time.Second)
}
}
func call(c echo.Client) {
ctx, span := otel.Tracer("client").Start(context.Background(), "root")
defer span.End()
randomInt := rand.Intn(1000)
req := &api.Request{Message: "my request " + strconv.Itoa(randomInt)}
resp, err := c.Echo(ctx, req)
if err != nil {
klog.CtxErrorf(ctx, "err %v", err)
}
klog.CtxInfof(ctx, "req:%v, res:%v", req, resp)
}