-
Notifications
You must be signed in to change notification settings - Fork 6
/
metrics_exporter.go
81 lines (68 loc) · 2.21 KB
/
metrics_exporter.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
import (
"fmt"
"log"
"time"
"contrib.go.opencensus.io/exporter/stackdriver"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
// The restaurant rating in number of stars.
readLatency = stats.Float64("readLatency", "Complete read latency", stats.UnitMilliseconds)
firstByteReadLatency = stats.Float64("firstByteReadLatency", "First byte read latency", stats.UnitMilliseconds)
)
var sdExporter *stackdriver.Exporter
func registerLatencyView() {
v := &view.View{
Name: "princer_go_client_read_latency",
Measure: readLatency,
Description: "Complete read latency for a given go-client",
TagKeys: []tag.Key{tag.MustNewKey("princer_read_latency")},
Aggregation: ochttp.DefaultLatencyDistribution,
}
if err := view.Register(v); err != nil {
log.Fatalf("Failed to register the readLatency view: %v", err)
}
}
func registerFirstByteLatencyView() {
v := &view.View{
Name: "princer_go_client_first_byte_read_latency",
Measure: firstByteReadLatency,
Description: "First byte read latency for a given go-client",
TagKeys: []tag.Key{tag.MustNewKey("princer_first_byte_read_latency")},
Aggregation: ochttp.DefaultLatencyDistribution,
}
if err := view.Register(v); err != nil {
log.Fatalf("Failed to register the firstByteReadLatency view: %v", err)
}
}
func enableSDExporter() (err error) {
sdExporter, err := stackdriver.NewExporter(stackdriver.Options{
// ProjectID <change this value>
ProjectID: *ProjectName,
// MetricPrefix helps uniquely identify your metrics. <change this value>
MetricPrefix: "custom.googleapis.com/custom-go-client/",
// ReportingInterval sets the frequency of reporting metrics
// to the Cloud Monitoring backend.
ReportingInterval: 30 * time.Second,
})
if err != nil {
err = fmt.Errorf("while creating stackdriver exporter: %w", err)
return
}
if err = sdExporter.StartMetricsExporter(); err != nil {
return fmt.Errorf("start stackdriver exporter: %w", err)
}
fmt.Println("Stack driver agent started successfully!!")
return nil
}
func closeSDExporter() {
if sdExporter != nil {
sdExporter.StopMetricsExporter()
sdExporter.Flush()
}
sdExporter = nil
}