-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
81 lines (72 loc) · 2.17 KB
/
server.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
// Package main implements a server for Greeter service.
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
cloudtrace "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/status"
)
// Serve is used to implement helloworld.GreeterServer.
type Server struct {
pb.UnimplementedGreeterServer
shouldErr bool
}
// SayHello implements helloworld.GreeterServer
func (s *Server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
if s.shouldErr {
return &pb.HelloReply{}, status.Error(codes.Internal, "force error")
}
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func (s *Server) Start(port int, service pb.GreeterServer) error {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
srv := grpc.NewServer(
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
)
pb.RegisterGreeterServer(srv, service)
log.Printf("server listening at %v", lis.Addr())
return srv.Serve(lis)
}
func initTracer() (func(), error) {
projectID := os.Getenv("PROJECT_ID")
// Create Google Cloud Trace exporter to be able to retrieve
// the collected spans.
exporter, err := cloudtrace.New(cloudtrace.WithProjectID(projectID))
if err != nil {
return nil, err
}
resource, err := resource.New(
context.Background(),
resource.WithAttributes(semconv.ServiceName("GreeterService")))
if err != nil {
return nil, err
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resource))
otel.SetTracerProvider(tp)
return func() {
err := tp.Shutdown(context.Background())
if err != nil {
fmt.Printf("error shutting down trace provider: %+v", err)
}
}, nil
}