-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
63 lines (55 loc) · 1.98 KB
/
examples_test.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
package grpcprom_test
import (
"net"
"net/http"
"bursavich.dev/grpcprom"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
bepb "bursavich.dev/grpcprom/testdata/backend"
fepb "bursavich.dev/grpcprom/testdata/frontend"
)
func Example() {
registry := prometheus.NewRegistry()
registry.MustRegister(collectors.NewGoCollector())
registry.MustRegister(collectors.NewBuildInfoCollector())
registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
// Create gRPC client metrics and register with Prometheus.
clientMetrics := grpcprom.NewClientMetrics()
registry.MustRegister(clientMetrics)
// Instrument gRPC client(s).
backendConn, err := grpc.Dial(backendAddr,
grpc.WithStatsHandler(clientMetrics.StatsHandler()),
grpc.WithStreamInterceptor(clientMetrics.StreamInterceptor()),
grpc.WithUnaryInterceptor(clientMetrics.UnaryInterceptor()),
grpc.WithDefaultCallOptions(
grpc.WaitForReady(true),
),
)
check(err)
// Create gRPC server metrics and register with Prometheus.
serverMetrics := grpcprom.NewServerMetrics()
registry.MustRegister(serverMetrics)
// Instrument gRPC server and initialize metrics.
grpcSrv := grpc.NewServer(
grpc.StatsHandler(serverMetrics.StatsHandler()),
grpc.StreamInterceptor(serverMetrics.StreamInterceptor()),
grpc.UnaryInterceptor(serverMetrics.UnaryInterceptor()),
)
fepb.RegisterFrontendServer(grpcSrv, &FrontendServer{
BackendClient: bepb.NewBackendClient(backendConn),
})
serverMetrics.Init(grpcSrv, codes.OK)
// Serve metrics.
httpLis, err := net.Listen("tcp", httpAddr)
check(err)
httpSrv := http.NewServeMux()
httpSrv.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
go http.Serve(httpLis, httpSrv)
// Serve gRPC.
grpcLis, err := net.Listen("tcp", grpcAddr)
check(err)
check(grpcSrv.Serve(grpcLis))
}