|
| 1 | +package collector |
| 2 | + |
| 3 | +// |
| 4 | +// collector.go |
| 5 | +// Copyright (C) 2020 gaspar_d </var/spool/mail/gaspar_d> |
| 6 | +// |
| 7 | +// Distributed under terms of the MIT license. |
| 8 | +// |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "strconv" |
| 13 | + "sync" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/prometheus/client_golang/prometheus" |
| 17 | + log "github.com/sirupsen/logrus" |
| 18 | +) |
| 19 | + |
| 20 | +// SchemaRegistryCCloudCollector is a custom prometheus collector to collect data from |
| 21 | +// Confluent Cloud Metrics API. It fetches schema_registry resources types metrics |
| 22 | +type SchemaRegistryCCloudCollector struct { |
| 23 | + metrics map[string]CCloudCollectorMetric |
| 24 | + rules []Rule |
| 25 | + ccloud CCloudCollector |
| 26 | + resource ResourceDescription |
| 27 | +} |
| 28 | + |
| 29 | +// Describe collect all metrics for ccloudexporter |
| 30 | +func (cc SchemaRegistryCCloudCollector) Describe(ch chan<- *prometheus.Desc) { |
| 31 | + for _, desc := range cc.metrics { |
| 32 | + ch <- desc.desc |
| 33 | + desc.duration.Describe(ch) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Collect all metrics for Prometheus |
| 38 | +// to avoid reaching the scrape_timeout, metrics are fetched in multiple goroutine |
| 39 | +func (cc SchemaRegistryCCloudCollector) Collect(ch chan<- prometheus.Metric, wg *sync.WaitGroup) { |
| 40 | + for _, rule := range cc.rules { |
| 41 | + for _, metric := range rule.Metrics { |
| 42 | + _, present := cc.metrics[metric] |
| 43 | + if !present { |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + if len(rule.SchemaRegistries) <= 0 { |
| 48 | + log.WithFields(log.Fields{"rule": rule}).Errorln("SchemaRegistries rule has no SchemaRegistry ID specified") |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + wg.Add(1) |
| 53 | + go cc.CollectMetricsForRule(wg, ch, rule, cc.metrics[metric]) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// CollectMetricsForRule collects all metrics for a specific rule |
| 59 | +func (cc SchemaRegistryCCloudCollector) CollectMetricsForRule(wg *sync.WaitGroup, ch chan<- prometheus.Metric, rule Rule, ccmetric CCloudCollectorMetric) { |
| 60 | + defer wg.Done() |
| 61 | + query := BuildSchemaRegistryQuery(ccmetric.metric, rule.SchemaRegistries, cc.resource) |
| 62 | + log.WithFields(log.Fields{"query": query}).Traceln("The following query has been created") |
| 63 | + optimizedQuery, additionalLabels := OptimizeQuery(query) |
| 64 | + log.WithFields(log.Fields{"optimizedQuery": optimizedQuery, "additionalLabels": additionalLabels}).Traceln("Query has been optimized") |
| 65 | + durationMetric, _ := ccmetric.duration.GetMetricWithLabelValues(strconv.Itoa(rule.id)) |
| 66 | + timer := prometheus.NewTimer(prometheus.ObserverFunc(durationMetric.Set)) |
| 67 | + response, err := SendQuery(optimizedQuery) |
| 68 | + timer.ObserveDuration() |
| 69 | + ch <- durationMetric |
| 70 | + if err != nil { |
| 71 | + log.WithError(err).WithFields(log.Fields{"optimizedQuery": optimizedQuery, "response": response}).Errorln("Query did not succeed") |
| 72 | + return |
| 73 | + } |
| 74 | + log.WithFields(log.Fields{"response": response}).Traceln("Response has been received") |
| 75 | + cc.handleResponse(response, ccmetric, ch, rule, additionalLabels) |
| 76 | +} |
| 77 | + |
| 78 | +func (cc SchemaRegistryCCloudCollector) handleResponse(response QueryResponse, ccmetric CCloudCollectorMetric, ch chan<- prometheus.Metric, rule Rule, additionalLabels map[string]string) { |
| 79 | + desc := ccmetric.desc |
| 80 | + for _, dataPoint := range response.Data { |
| 81 | + value, ok := dataPoint["value"].(float64) |
| 82 | + if !ok { |
| 83 | + log.WithField("datapoint", dataPoint["value"]).Errorln("Can not convert result to float") |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + labels := []string{} |
| 88 | + for _, label := range ccmetric.labels { |
| 89 | + name := cc.resource.datapointFieldNameForLabel(label) |
| 90 | + |
| 91 | + // Could be remove when fix is done in descriptor.go line 95 |
| 92 | + if name == "resource.schema.registry.id" { |
| 93 | + name = "resource.schema_registry.id" |
| 94 | + } |
| 95 | + |
| 96 | + labelValue, labelValuePresent := dataPoint[name].(string) |
| 97 | + if !labelValuePresent { |
| 98 | + labelValue, labelValuePresent = additionalLabels[name] |
| 99 | + } |
| 100 | + labels = append(labels, labelValue) |
| 101 | + } |
| 102 | + metric := prometheus.MustNewConstMetric( |
| 103 | + desc, |
| 104 | + prometheus.GaugeValue, |
| 105 | + value, |
| 106 | + labels..., |
| 107 | + ) |
| 108 | + |
| 109 | + if Context.NoTimestamp { |
| 110 | + ch <- metric |
| 111 | + } else { |
| 112 | + timestamp, err := time.Parse(time.RFC3339, fmt.Sprint(dataPoint["timestamp"])) |
| 113 | + if err != nil { |
| 114 | + log.WithError(err).Errorln("Can not parse timestamp, ignoring the response") |
| 115 | + return |
| 116 | + } |
| 117 | + metricWithTime := prometheus.NewMetricWithTimestamp(timestamp, metric) |
| 118 | + ch <- metricWithTime |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// NewSchemaRegistryCCloudCollector create a new Confluent Cloud SchemaRegistry collector |
| 124 | +func NewSchemaRegistryCCloudCollector(ccloudcollecter CCloudCollector, resource ResourceDescription) SchemaRegistryCCloudCollector { |
| 125 | + collector := SchemaRegistryCCloudCollector{ |
| 126 | + rules: Context.GetSchemaRegistryRules(), |
| 127 | + metrics: make(map[string]CCloudCollectorMetric), |
| 128 | + ccloud: ccloudcollecter, |
| 129 | + resource: resource, |
| 130 | + } |
| 131 | + descriptorResponse := SendDescriptorQuery(resource.Type) |
| 132 | + log.WithField("descriptor response", descriptorResponse).Traceln("The following response for the descriptor endpoint has been received") |
| 133 | + mapOfWhiteListedMetrics := Context.GetMapOfMetrics("io.confluent.kafka.schema_registry") |
| 134 | + |
| 135 | + for _, metr := range descriptorResponse.Data { |
| 136 | + _, metricPresent := mapOfWhiteListedMetrics[metr.Name] |
| 137 | + if !metricPresent { |
| 138 | + continue |
| 139 | + } |
| 140 | + delete(mapOfWhiteListedMetrics, metr.Name) |
| 141 | + var labels []string |
| 142 | + for _, metrLabel := range metr.Labels { |
| 143 | + labels = append(labels, metrLabel.Key) |
| 144 | + } |
| 145 | + |
| 146 | + for _, rsrcLabel := range resource.Labels { |
| 147 | + labels = append(labels, GetPrometheusNameForLabel(rsrcLabel.Key)) |
| 148 | + } |
| 149 | + desc := prometheus.NewDesc( |
| 150 | + "ccloud_metric_schema_registry_"+GetNiceNameForMetric(metr), |
| 151 | + metr.Description, |
| 152 | + labels, |
| 153 | + nil, |
| 154 | + ) |
| 155 | + |
| 156 | + requestDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 157 | + Name: "ccloud_metrics_api_request_latency", |
| 158 | + Help: "Metrics API request latency", |
| 159 | + ConstLabels: map[string]string{"metric": metr.Name}, |
| 160 | + }, []string{"ruleNumber"}) |
| 161 | + |
| 162 | + metric := CCloudCollectorMetric{ |
| 163 | + metric: metr, |
| 164 | + desc: desc, |
| 165 | + duration: requestDuration, |
| 166 | + labels: labels, |
| 167 | + } |
| 168 | + collector.metrics[metr.Name] = metric |
| 169 | + } |
| 170 | + |
| 171 | + if len(mapOfWhiteListedMetrics) > 0 { |
| 172 | + log.WithField("Ignored metrics", mapOfWhiteListedMetrics).Warnln("The following metrics will not be gathered as they are not exposed by the Metrics API") |
| 173 | + } |
| 174 | + |
| 175 | + return collector |
| 176 | +} |
0 commit comments