|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | + "google.golang.org/api/monitoring/v3" |
| 6 | + |
| 7 | + "github.com/frodenas/stackdriver_exporter/utils" |
| 8 | + "sort" |
| 9 | +) |
| 10 | + |
| 11 | +func buildFQName(timeSeries *monitoring.TimeSeries) string { |
| 12 | + // The metric name to report is composed by the 3 parts: |
| 13 | + // 1. namespace is a constant prefix (stackdriver) |
| 14 | + // 2. subsystem is the monitored resource type (ie gce_instance) |
| 15 | + // 3. name is the metric type (ie compute.googleapis.com/instance/cpu/usage_time) |
| 16 | + return prometheus.BuildFQName("stackdriver", utils.NormalizeMetricName(timeSeries.Resource.Type), utils.NormalizeMetricName(timeSeries.Metric.Type)) |
| 17 | +} |
| 18 | + |
| 19 | +type TimeSeriesMetrics struct { |
| 20 | + metricDescriptor *monitoring.MetricDescriptor |
| 21 | + ch chan<- prometheus.Metric |
| 22 | + |
| 23 | + fillMissingLabels bool |
| 24 | + constMetrics map[string][]ConstMetric |
| 25 | + histogramMetrics map[string][]HistogramMetric |
| 26 | +} |
| 27 | + |
| 28 | +func (t *TimeSeriesMetrics) newMetricDesc(fqName string, labelKeys []string) *prometheus.Desc { |
| 29 | + return prometheus.NewDesc( |
| 30 | + fqName, |
| 31 | + t.metricDescriptor.Description, |
| 32 | + labelKeys, |
| 33 | + prometheus.Labels{}, |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +type ConstMetric struct { |
| 38 | + fqName string |
| 39 | + labelKeys []string |
| 40 | + valueType prometheus.ValueType |
| 41 | + value float64 |
| 42 | + labelValues []string |
| 43 | + |
| 44 | + keysHash uint64 |
| 45 | +} |
| 46 | + |
| 47 | +type HistogramMetric struct { |
| 48 | + fqName string |
| 49 | + labelKeys []string |
| 50 | + dist *monitoring.Distribution |
| 51 | + buckets map[float64]uint64 |
| 52 | + labelValues []string |
| 53 | + |
| 54 | + keysHash uint64 |
| 55 | +} |
| 56 | + |
| 57 | +func (t *TimeSeriesMetrics) CollectNewConstHistogram(timeSeries *monitoring.TimeSeries, labelKeys []string, dist *monitoring.Distribution, buckets map[float64]uint64, labelValues []string) { |
| 58 | + fqName := buildFQName(timeSeries) |
| 59 | + |
| 60 | + if t.fillMissingLabels { |
| 61 | + vs, ok := t.histogramMetrics[fqName] |
| 62 | + if !ok { |
| 63 | + vs = make([]HistogramMetric, 0) |
| 64 | + } |
| 65 | + v := HistogramMetric{ |
| 66 | + fqName: fqName, |
| 67 | + labelKeys: labelKeys, |
| 68 | + dist: dist, |
| 69 | + buckets: buckets, |
| 70 | + labelValues: labelValues, |
| 71 | + |
| 72 | + keysHash: hashLabelKeys(labelKeys), |
| 73 | + } |
| 74 | + t.histogramMetrics[fqName] = append(vs, v) |
| 75 | + return |
| 76 | + } |
| 77 | + t.ch <- t.newConstHistogram(fqName, labelKeys, dist, buckets, labelValues) |
| 78 | +} |
| 79 | + |
| 80 | +func (t *TimeSeriesMetrics) newConstHistogram(fqName string, labelKeys []string, dist *monitoring.Distribution, buckets map[float64]uint64, labelValues []string) prometheus.Metric { |
| 81 | + return prometheus.MustNewConstHistogram( |
| 82 | + t.newMetricDesc(fqName, labelKeys), |
| 83 | + uint64(dist.Count), |
| 84 | + dist.Mean*float64(dist.Count), // Stackdriver does not provide the sum, but we can fake it |
| 85 | + buckets, |
| 86 | + labelValues..., |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +func (t *TimeSeriesMetrics) CollectNewConstMetric(timeSeries *monitoring.TimeSeries, labelKeys []string, metricValueType prometheus.ValueType, metricValue float64, labelValues []string) { |
| 91 | + fqName := buildFQName(timeSeries) |
| 92 | + |
| 93 | + if t.fillMissingLabels { |
| 94 | + vs, ok := t.constMetrics[fqName] |
| 95 | + if !ok { |
| 96 | + vs = make([]ConstMetric, 0) |
| 97 | + } |
| 98 | + v := ConstMetric{ |
| 99 | + fqName: fqName, |
| 100 | + labelKeys: labelKeys, |
| 101 | + valueType: metricValueType, |
| 102 | + value: metricValue, |
| 103 | + labelValues: labelValues, |
| 104 | + |
| 105 | + keysHash: hashLabelKeys(labelKeys), |
| 106 | + } |
| 107 | + t.constMetrics[fqName] = append(vs, v) |
| 108 | + return |
| 109 | + } |
| 110 | + t.ch <- t.newConstMetric(fqName, labelKeys, metricValueType, metricValue, labelValues) |
| 111 | +} |
| 112 | + |
| 113 | +func (t *TimeSeriesMetrics) newConstMetric(fqName string, labelKeys []string, metricValueType prometheus.ValueType, metricValue float64, labelValues []string) prometheus.Metric { |
| 114 | + return prometheus.MustNewConstMetric( |
| 115 | + t.newMetricDesc(fqName, labelKeys), |
| 116 | + metricValueType, |
| 117 | + metricValue, |
| 118 | + labelValues..., |
| 119 | + ) |
| 120 | +} |
| 121 | + |
| 122 | +func hashLabelKeys(labelKeys []string) uint64 { |
| 123 | + dh := hashNew() |
| 124 | + sortedKeys := make([]string, len(labelKeys)) |
| 125 | + copy(sortedKeys, labelKeys) |
| 126 | + sort.Strings(sortedKeys) |
| 127 | + for _, key := range sortedKeys { |
| 128 | + dh = hashAdd(dh, key) |
| 129 | + dh = hashAddByte(dh, separatorByte) |
| 130 | + } |
| 131 | + return dh |
| 132 | +} |
| 133 | + |
| 134 | +func (t *TimeSeriesMetrics) Complete() { |
| 135 | + t.completeConstMetrics() |
| 136 | + t.completeHistogramMetrics() |
| 137 | +} |
| 138 | + |
| 139 | +func (t *TimeSeriesMetrics) completeConstMetrics() { |
| 140 | + for _, vs := range t.constMetrics { |
| 141 | + if len(vs) > 1 { |
| 142 | + var needFill bool |
| 143 | + for i := 1; i < len(vs); i++ { |
| 144 | + if vs[0].keysHash != vs[i].keysHash { |
| 145 | + needFill = true |
| 146 | + } |
| 147 | + } |
| 148 | + if needFill { |
| 149 | + vs = fillConstMetricsLabels(vs) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + for _, v := range vs { |
| 154 | + t.ch <- t.newConstMetric(v.fqName, v.labelKeys, v.valueType, v.value, v.labelValues) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func (t *TimeSeriesMetrics) completeHistogramMetrics() { |
| 160 | + for _, vs := range t.histogramMetrics { |
| 161 | + if len(vs) > 1 { |
| 162 | + var needFill bool |
| 163 | + for i := 1; i < len(vs); i++ { |
| 164 | + if vs[0].keysHash != vs[i].keysHash { |
| 165 | + needFill = true |
| 166 | + } |
| 167 | + } |
| 168 | + if needFill { |
| 169 | + vs = fillHistogramMetricsLabels(vs) |
| 170 | + } |
| 171 | + } |
| 172 | + for _, v := range vs { |
| 173 | + t.ch <- t.newConstHistogram(v.fqName, v.labelKeys, v.dist, v.buckets, v.labelValues) |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func fillConstMetricsLabels(metrics []ConstMetric) []ConstMetric { |
| 179 | + allKeys := make(map[string]struct{}) |
| 180 | + for _, metric := range metrics { |
| 181 | + for _, key := range metric.labelKeys { |
| 182 | + allKeys[key] = struct{}{} |
| 183 | + } |
| 184 | + } |
| 185 | + result := make([]ConstMetric, len(metrics)) |
| 186 | + for i, metric := range metrics { |
| 187 | + if len(metric.labelKeys) != len(allKeys) { |
| 188 | + metricKeys := make(map[string]struct{}) |
| 189 | + for _, key := range metric.labelKeys { |
| 190 | + metricKeys[key] = struct{}{} |
| 191 | + } |
| 192 | + for key := range allKeys { |
| 193 | + if _, ok := metricKeys[key]; !ok { |
| 194 | + metric.labelKeys = append(metric.labelKeys, key) |
| 195 | + metric.labelValues = append(metric.labelValues, "") |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + result[i] = metric |
| 200 | + } |
| 201 | + |
| 202 | + return result |
| 203 | +} |
| 204 | + |
| 205 | +func fillHistogramMetricsLabels(metrics []HistogramMetric) []HistogramMetric { |
| 206 | + allKeys := make(map[string]struct{}) |
| 207 | + for _, metric := range metrics { |
| 208 | + for _, key := range metric.labelKeys { |
| 209 | + allKeys[key] = struct{}{} |
| 210 | + } |
| 211 | + } |
| 212 | + result := make([]HistogramMetric, len(metrics)) |
| 213 | + for i, metric := range metrics { |
| 214 | + if len(metric.labelKeys) != len(allKeys) { |
| 215 | + metricKeys := make(map[string]struct{}) |
| 216 | + for _, key := range metric.labelKeys { |
| 217 | + metricKeys[key] = struct{}{} |
| 218 | + } |
| 219 | + for key := range allKeys { |
| 220 | + if _, ok := metricKeys[key]; !ok { |
| 221 | + metric.labelKeys = append(metric.labelKeys, key) |
| 222 | + metric.labelValues = append(metric.labelValues, "") |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + result[i] = metric |
| 227 | + } |
| 228 | + |
| 229 | + return result |
| 230 | +} |
0 commit comments