forked from grpc-up-and-running/samples
/
main.go
76 lines (66 loc) · 2.21 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
// Go to ${grpc-up-and-running}/samples/ch02/productinfo
// Optional: Execute protoc -I proto proto/product_info.proto --go_out=plugins=grpc:go/product_info
// Execute go get -v github.com/grpc-up-and-running/samples/ch02/productinfo/golang/product_info
// Execute go run go/client/main.go
package main
import (
"context"
"github.com/daneshk/samples/ch07/grpc-opentracing/go/tracer"
wrapper "github.com/golang/protobuf/ptypes/wrappers"
grpcopentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
pb "github.com/grpc-up-and-running/samples/ch07/grpc-opentracing/go/proto"
"google.golang.org/grpc"
"log"
"time"
)
const (
address = "localhost:50051"
)
func main() {
// Set up a connection to the server.
conn, err := NewClientConn(address)
if err != nil {
log.Fatalf("did not connect: %v", err)
return
}
defer conn.Close()
c := pb.NewProductInfoClient(conn)
for {
// Contact the server and print out its response.
name := "Sumsung S10"
description := "Samsung Galaxy S10 is the latest smart phone, launched in February 2019"
price := float32(700.0)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.AddProduct(ctx, &pb.Product{Name: name, Description: description, Price: price})
if err != nil {
log.Fatalf("Could not add product: %v", err)
}
log.Printf("Product ID: %s added successfully", r.Value)
product, err := c.GetProduct(ctx, &wrapper.StringValue{Value: r.Value})
if err != nil {
log.Fatalf("Could not get product: %v", err)
}
log.Printf("Product: " + product.String())
time.Sleep(3 * time.Second)
}
}
func NewClientConn(address string) (*grpc.ClientConn, error) {
// initialize jaegertracer
jaegertracer, closer, err := tracer.NewTracer("product_mgt")
if err != nil {
return grpc.Dial(address, grpc.WithInsecure())
}
defer closer.Close()
// initialize client with tracing interceptor using grpc client side chaining
return grpc.Dial(
address,
grpc.WithInsecure(),
grpc.WithStreamInterceptor(
grpcopentracing.StreamClientInterceptor(grpcopentracing.WithTracer(jaegertracer)),
),
grpc.WithUnaryInterceptor(
grpcopentracing.UnaryClientInterceptor(grpcopentracing.WithTracer(jaegertracer)),
),
)
}