-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
120 lines (105 loc) · 2.96 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"time"
stackdriver "github.com/google/go-metrics-stackdriver"
"cloud.google.com/go/compute/metadata"
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
metrics "github.com/hashicorp/go-metrics"
)
func main() {
// setup client
ctx, cancel := context.WithCancel(context.Background())
client, err := monitoring.NewMetricClient(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Close()
projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
if projectID == "" {
if projectID, err = metadata.ProjectID(); err != nil {
log.Fatal(err)
}
}
log.Printf("initializing sink, project_id: %q", projectID)
// create sink
ss := stackdriver.NewSink(client, &stackdriver.Config{
ProjectID: projectID,
Location: "us-east1-c",
DebugLogs: true,
ReportingInterval: 35 * time.Second,
})
defer ss.Close(context.Background())
cfg := metrics.DefaultConfig("go-metrics-stackdriver")
cfg.EnableHostname = false
metrics.NewGlobal(cfg, ss)
// start listener
log.Printf("starting server")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer metrics.MeasureSince([]string{"handler"}, time.Now())
metrics.IncrCounter([]string{"requests"}, 1.0)
fmt.Fprintf(w, "Hello from go-metrics-stackdriver")
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
srv := http.Server{
Addr: ":" + port,
}
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("server error: %s", err)
}
}()
// capture ctrl+c
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Printf("ctrl+c detected... shutting down")
cancel()
srv.Shutdown(context.Background())
}()
// generate data
log.Printf("sending data")
exercise(ctx, ss)
}
func exercise(ctx context.Context, m metrics.MetricSink) {
for {
select {
case <-time.After(100 * time.Millisecond):
m.SetGauge([]string{"foo"}, 42)
m.IncrCounter([]string{"baz"}, 1)
m.AddSample([]string{"method", "rand"}, 500*rand.Float32())
m.AddSample([]string{"method", "const"}, 200)
m.AddSample([]string{"method", "dist"}, 50)
m.AddSample([]string{"method", "dist"}, 100)
m.AddSample([]string{"method", "dist"}, 150)
m.AddSample([]string{"foo"}, 100)
case <-ctx.Done():
log.Printf("terminating")
return
}
}
}