-
Notifications
You must be signed in to change notification settings - Fork 324
/
helper.go
138 lines (122 loc) · 4.38 KB
/
helper.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Package metrics is an internal package that provides helper functions for
OpenCensus instrumentation in Open Match.
Copyright 2018 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
https://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 metrics
import (
"context"
"fmt"
"net/http"
"time"
"github.com/GoogleCloudPlatform/open-match/config"
"github.com/GoogleCloudPlatform/open-match/internal/util/netlistener"
log "github.com/sirupsen/logrus"
"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
metricsLogFields = log.Fields{
"app": "openmatch",
"component": "metrics",
}
mhLog = log.WithFields(metricsLogFields)
)
// ConfigureOpenCensusPrometheusExporter reads from the provided
// config's 'metrics' section to set up a metrics endpoint that can be scraped
// by Promethus for metrics gathering. The calling code can select any views
// it wants to register, from any number of libraries, and pass them in as an
// array.
func ConfigureOpenCensusPrometheusExporter(lh *netlistener.ListenerHolder, cfg config.View, views []*view.View) {
//var infoCtx, err = tag.New(context.Background(), tag.Insert(KeySeverity, "info"))
metricsPort := lh.Number()
metricsEP := cfg.GetString("metrics.endpoint")
metricsRP := cfg.GetInt("metrics.reportingPeriod")
// Set OpenCensus to export to Prometheus
pe, err := prometheus.NewExporter(prometheus.Options{Namespace: "open_match"})
if err != nil {
mhLog.WithFields(log.Fields{"error": err}).Fatal(
"Failed to initialize OpenCensus exporter to Prometheus")
}
mhLog.Info("OpenCensus exporter to Promethus initialized")
view.RegisterExporter(pe)
// Register the OpenCensus views we want to export to Prometheus
err = view.Register(views...)
if err != nil {
mhLog.Fatalf("Failed to register OpenCensus views for metrics gathering: %v", err)
}
mhLog.Info("Opencensus views registered")
// Change the frequency of updates to the metrics endpoint
view.SetReportingPeriod(time.Duration(metricsRP) * time.Second)
mhLog.WithFields(log.Fields{
"port": metricsPort,
"endpoint": metricsEP,
"retentionPeriod": metricsRP,
}).Info("Opencensus measurement serving to Prometheus configured")
// Start serving the /metrics http endpoint for Prometheus
go func() {
mux := http.NewServeMux()
mux.Handle(metricsEP, pe)
mhLog.WithFields(log.Fields{
"port": metricsPort,
"endpoint": metricsEP,
}).Info("Attempting to start http server for OpenCensus metrics on localhost")
listener, err := lh.Obtain()
if err != nil {
mhLog.WithFields(log.Fields{
"error": err,
"port": metricsPort,
"endpoint": metricsEP,
}).Fatal("Failed to run Prometheus endpoint")
}
err = http.Serve(listener, mux)
if err != nil {
mhLog.WithFields(log.Fields{
"error": err,
"port": metricsPort,
"endpoint": metricsEP,
}).Fatal("Failed to run Prometheus endpoint")
}
mhLog.WithFields(log.Fields{
"port": metricsPort,
"endpoint": metricsEP,
}).Info("Successfully started http server for OpenCensus metrics on localhost")
}()
}
// Hook is a log hook that for counting log lines using OpenCensus.
type Hook struct {
count *stats.Int64Measure
keySeverity tag.Key
}
// NewHook returns a new log.Hook for counting log lines using OpenCensus.
func NewHook(m *stats.Int64Measure, ks tag.Key) log.Hook {
return Hook{
count: m,
keySeverity: ks,
}
}
// Fire is run every time log logs a line and increments the OpenCensus counter
func (h Hook) Fire(e *log.Entry) error {
// get the entry's log level, and tag this opencensus counter increment with that level
ctx, err := tag.New(context.Background(), tag.Insert(h.keySeverity, e.Level.String()))
if err != nil {
fmt.Println(err)
}
stats.Record(ctx, h.count.M(1))
return err
}
// Levels returns all log levels, because we want the hook to always fire when a line is logged.
func (h Hook) Levels() []log.Level {
return log.AllLevels
}