This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.go
82 lines (74 loc) · 2.08 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
package main
import (
"context"
"strings"
"time"
grpcc "github.com/go-micro/plugins/v4/client/grpc"
_ "github.com/go-micro/plugins/v4/registry/kubernetes"
grpcs "github.com/go-micro/plugins/v4/server/grpc"
"github.com/go-micro/plugins/v4/wrapper/trace/opentelemetry"
"go-micro.dev/v4"
"go-micro.dev/v4/logger"
"go-micro.dev/v4/server"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"github.com/go-micro/demo/emailservice/config"
"github.com/go-micro/demo/emailservice/handler"
pb "github.com/go-micro/demo/emailservice/proto"
)
var (
name = "emailservice"
version = "1.0.0"
)
func main() {
// Load conigurations
if err := config.Load(); err != nil {
logger.Fatal(err)
}
// Create service
srv := micro.NewService(
micro.Server(grpcs.NewServer()),
micro.Client(grpcc.NewClient()),
)
opts := []micro.Option{
micro.Name(name),
micro.Version(version),
micro.Address(config.Address()),
}
if cfg := config.Tracing(); cfg.Enable {
tp, err := newTracerProvider(name, srv.Server().Options().Id, cfg.Jaeger.URL)
if err != nil {
logger.Fatal(err)
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if err := tp.Shutdown(ctx); err != nil {
logger.Fatal(err)
}
}()
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
traceOpts := []opentelemetry.Option{
opentelemetry.WithHandleFilter(func(ctx context.Context, r server.Request) bool {
if e := r.Endpoint(); strings.HasPrefix(e, "Health.") {
return true
}
return false
}),
}
opts = append(opts, micro.WrapHandler(opentelemetry.NewHandlerWrapper(traceOpts...)))
}
srv.Init(opts...)
// Register handler
if err := pb.RegisterEmailServiceHandler(srv.Server(), new(handler.DummyEmailService)); err != nil {
logger.Fatal(err)
}
if err := pb.RegisterHealthHandler(srv.Server(), new(handler.Health)); err != nil {
logger.Fatal(err)
}
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}