-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheusannouncer.go
97 lines (87 loc) · 2.66 KB
/
prometheusannouncer.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
package prometheusannouncer
import (
"context"
"fmt"
"time"
"google.golang.org/grpc"
"github.com/duncanvanzyl/prometheus-announcer/pb"
)
// DialAndAnnounce announces a prometheus metrics endpoint.
// Will run until the context is canceled.
// Connects to the grpc server with insecure. Use "Announce" and provide your own
// connection to use a secure connection.
// Re-announces the host at "interval".
// "id" is a unique id (perhaps a uuid) for the client announcement. Supplied by the client.
// ""targets" and "labels" are the target definitions for http_sd.
func DialAndAnnounce(
ctx context.Context,
grpcServer string,
interval time.Duration,
id string,
targets []string,
labels map[string]string,
) error {
conn, err := grpc.Dial(grpcServer, grpc.WithInsecure())
if err != nil {
return fmt.Errorf("could not dial grpc server: %v", err)
}
return AnnounceWithConnection(ctx, conn, interval, id, targets, labels)
}
// AnnounceWithConnection announces a prometheus metrics endpoint.
// Will run until the context is canceled.
// Provide an existing connection to the server.
// Re-announces the host at "interval".
// "id" is a unique id (perhaps a uuid) for the client announcement. Supplied by the client.
// ""targets" and "labels" are the target definitions for http_sd.
func AnnounceWithConnection(
ctx context.Context,
conn *grpc.ClientConn,
interval time.Duration,
id string,
targets []string,
labels map[string]string,
) error {
req := Announcement(id, targets, labels)
return Announce(ctx, conn, interval, req)
}
// Announce announces a prometheus metrics endpoint.
// Will run until the context is canceled.
// Provide an existing connection to the server.
// Re-announces the host at "interval".
// "req" is a RegisterRequest containing an id, targets and labels. Create with "Announcement"
func Announce(ctx context.Context,
conn *grpc.ClientConn,
interval time.Duration,
req *pb.RegisterRequest,
) error {
cli := pb.NewServiceDiscoveryClient(conn)
tick := time.NewTicker(interval)
for {
_, err := cli.Announce(ctx, req)
if err != nil {
return fmt.Errorf("could not announce service: %v", err)
}
select {
case <-tick.C:
case <-ctx.Done():
return nil
}
}
}
// Announcement creates a RegisterRequest.
// "id" is a unique id (perhaps a uuid) for the client announcement. Supplied by the client.
// ""targets" and "labels" are the target definitions for http_sd.
func Announcement(id string, targets []string, labels map[string]string) *pb.RegisterRequest {
ls := []*pb.Label{}
for l, v := range labels {
ls = append(ls, &pb.Label{
Name: l,
Value: v,
})
}
return &pb.RegisterRequest{
UUID: id,
Targets: targets,
Labels: ls,
}
}