-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
105 lines (85 loc) · 2.81 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
/*
Copyright The containerd 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.
*/
// Copyright The OpenTelemetry 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"
"log"
"net"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/containerd/otelttrpc"
"github.com/containerd/otelttrpc/example/api"
"github.com/containerd/otelttrpc/example/config"
"github.com/containerd/ttrpc"
)
const (
port = ":7777"
)
var tracer = otel.Tracer("ttrpc-example")
// server is used to implement api.HelloServiceService.
type server struct {
api.HelloServiceService
}
// SayHello implements api.HelloServiceServer.
func (s *server) SayHello(ctx context.Context, in *api.HelloRequest) (*api.HelloResponse, error) {
log.Printf("Received: %v\n", in.GetGreeting())
s.workHard(ctx)
time.Sleep(50 * time.Millisecond)
return &api.HelloResponse{Reply: "Hello " + in.Greeting}, nil
}
func (s *server) workHard(ctx context.Context) {
_, span := tracer.Start(ctx, "workHard",
trace.WithAttributes(attribute.String("extra.key", "extra.value")))
defer span.End()
time.Sleep(50 * time.Millisecond)
}
func main() {
tp, err := config.Init()
if err != nil {
log.Fatal(err)
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s, err := ttrpc.NewServer(
ttrpc.WithUnaryServerInterceptor(otelttrpc.UnaryServerInterceptor()),
)
if err != nil {
log.Fatalf("failed to create ttRPC server: %v", err)
}
api.RegisterHelloServiceService(s, &server{})
if err := s.Serve(context.Background(), lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}