| 1 |
// Copyright 2014 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package prometheus_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"log" |
| 18 |
"net/http" |
| 19 |
|
| 20 |
"github.com/prometheus/client_golang/prometheus" |
| 21 |
"github.com/prometheus/client_golang/prometheus/promhttp" |
| 22 |
) |
| 23 |
|
| 24 |
// ClusterManager is an example for a system that might have been built without |
| 25 |
// Prometheus in mind. It models a central manager of jobs running in a |
| 26 |
// cluster. Thus, we implement a custom Collector called |
| 27 |
// ClusterManagerCollector, which collects information from a ClusterManager |
| 28 |
// using its provided methods and turns them into Prometheus Metrics for |
| 29 |
// collection. |
| 30 |
// |
| 31 |
// An additional challenge is that multiple instances of the ClusterManager are |
| 32 |
// run within the same binary, each in charge of a different zone. We need to |
| 33 |
// make use of wrapping Registerers to be able to register each |
| 34 |
// ClusterManagerCollector instance with Prometheus. |
| 35 |
type ClusterManager struct { |
| 36 |
Zone string |
| 37 |
// Contains many more fields not listed in this example. |
| 38 |
} |
| 39 |
|
| 40 |
// ReallyExpensiveAssessmentOfTheSystemState is a mock for the data gathering a |
| 41 |
// real cluster manager would have to do. Since it may actually be really |
| 42 |
// expensive, it must only be called once per collection. This implementation, |
| 43 |
// obviously, only returns some made-up data. |
| 44 |
func (c *ClusterManager) ReallyExpensiveAssessmentOfTheSystemState() ( |
| 45 |
oomCountByHost map[string]int, ramUsageByHost map[string]float64, |
| 46 |
) { |
| 47 |
// Just example fake data. |
| 48 |
oomCountByHost = map[string]int{ |
| 49 |
"foo.example.org": 42, |
| 50 |
"bar.example.org": 2001, |
| 51 |
} |
| 52 |
ramUsageByHost = map[string]float64{ |
| 53 |
"foo.example.org": 6.023e23, |
| 54 |
"bar.example.org": 3.14, |
| 55 |
} |
| 56 |
return |
| 57 |
} |
| 58 |
|
| 59 |
// ClusterManagerCollector implements the Collector interface. |
| 60 |
type ClusterManagerCollector struct { |
| 61 |
ClusterManager *ClusterManager |
| 62 |
} |
| 63 |
|
| 64 |
// Descriptors used by the ClusterManagerCollector below. |
| 65 |
var ( |
| 66 |
oomCountDesc = prometheus.NewDesc( |
| 67 |
"clustermanager_oom_crashes_total", |
| 68 |
"Number of OOM crashes.", |
| 69 |
[]string{"host"}, nil, |
| 70 |
) |
| 71 |
ramUsageDesc = prometheus.NewDesc( |
| 72 |
"clustermanager_ram_usage_bytes", |
| 73 |
"RAM usage as reported to the cluster manager.", |
| 74 |
[]string{"host"}, nil, |
| 75 |
) |
| 76 |
) |
| 77 |
|
| 78 |
// Describe is implemented with DescribeByCollect. That's possible because the |
| 79 |
// Collect method will always return the same two metrics with the same two |
| 80 |
// descriptors. |
| 81 |
func (cc ClusterManagerCollector) Describe(ch chan<- *prometheus.Desc) { |
| 82 |
prometheus.DescribeByCollect(cc, ch) |
| 83 |
} |
| 84 |
|
| 85 |
// Collect first triggers the ReallyExpensiveAssessmentOfTheSystemState. Then it |
| 86 |
// creates constant metrics for each host on the fly based on the returned data. |
| 87 |
// |
| 88 |
// Note that Collect could be called concurrently, so we depend on |
| 89 |
// ReallyExpensiveAssessmentOfTheSystemState to be concurrency-safe. |
| 90 |
func (cc ClusterManagerCollector) Collect(ch chan<- prometheus.Metric) { |
| 91 |
oomCountByHost, ramUsageByHost := cc.ClusterManager.ReallyExpensiveAssessmentOfTheSystemState() |
| 92 |
for host, oomCount := range oomCountByHost { |
| 93 |
ch <- prometheus.MustNewConstMetric( |
| 94 |
oomCountDesc, |
| 95 |
prometheus.CounterValue, |
| 96 |
float64(oomCount), |
| 97 |
host, |
| 98 |
) |
| 99 |
} |
| 100 |
for host, ramUsage := range ramUsageByHost { |
| 101 |
ch <- prometheus.MustNewConstMetric( |
| 102 |
ramUsageDesc, |
| 103 |
prometheus.GaugeValue, |
| 104 |
ramUsage, |
| 105 |
host, |
| 106 |
) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// NewClusterManager first creates a Prometheus-ignorant ClusterManager |
| 111 |
// instance. Then, it creates a ClusterManagerCollector for the just created |
| 112 |
// ClusterManager. Finally, it registers the ClusterManagerCollector with a |
| 113 |
// wrapping Registerer that adds the zone as a label. In this way, the metrics |
| 114 |
// collected by different ClusterManagerCollectors do not collide. |
| 115 |
func NewClusterManager(zone string, reg prometheus.Registerer) *ClusterManager { |
| 116 |
c := &ClusterManager{ |
| 117 |
Zone: zone, |
| 118 |
} |
| 119 |
cc := ClusterManagerCollector{ClusterManager: c} |
| 120 |
prometheus.WrapRegistererWith(prometheus.Labels{"zone": zone}, reg).MustRegister(cc) |
| 121 |
return c |
| 122 |
} |
| 123 |
|
| 124 |
func ExampleCollector() { |
| 125 |
// Since we are dealing with custom Collector implementations, it might |
| 126 |
// be a good idea to try it out with a pedantic registry. |
| 127 |
reg := prometheus.NewPedanticRegistry() |
| 128 |
|
| 129 |
// Construct cluster managers. In real code, we would assign them to |
| 130 |
// variables to then do something with them. |
| 131 |
NewClusterManager("db", reg) |
| 132 |
NewClusterManager("ca", reg) |
| 133 |
|
| 134 |
// Add the standard process and Go metrics to the custom registry. |
| 135 |
reg.MustRegister( |
| 136 |
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}), |
| 137 |
prometheus.NewGoCollector(), |
| 138 |
) |
| 139 |
|
| 140 |
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) |
| 141 |
log.Fatal(http.ListenAndServe(":8080", nil)) |
| 142 |
} |