-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metric metadata to message (#102) #112
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
prometheus-kafka-adapter-libc | ||
prometheus-kafka-adapter-musl | ||
prometheus-kafka-adapter | ||
mycode.go.txt |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,25 @@ Prometheus-kafka-adapter is a service which receives [Prometheus](https://github | |
|
||
## output | ||
|
||
It is able to write JSON or Avro-JSON messages in a kafka topic, depending on the `SERIALIZATION_FORMAT` configuration variable. | ||
It is able to write JSON or Avro-JSON messages in a kafka topic, depending on the `SERIALIZATION_FORMAT` configuration variable. | ||
|
||
Metric metadata can be included in the JSON output, if the `PROM_METADATA_ENDPOINT` is set to correct API endpoint of the prometheus service, eg http://localhost:9090/api/v1/metadata. | ||
Only the metrics which are available at application startup will have the metadata included. To inlcude metadata for newer metrics, the application will need to be restarted | ||
### JSON | ||
|
||
```json | ||
{ | ||
"timestamp": "1970-01-01T00:00:00Z", | ||
"value": "9876543210", | ||
"name": "up", | ||
|
||
"labels": { | ||
"__name__": "up", | ||
"label1": "value1", | ||
"label2": "value2" | ||
} | ||
}, | ||
"type": "type", | ||
"help": "help", | ||
"unit": "unit" | ||
} | ||
``` | ||
|
||
|
@@ -38,6 +42,8 @@ There is a docker image `telefonica/prometheus-kafka-adapter:1.8.0` [available o | |
|
||
Prometheus-kafka-adapter listens for metrics coming from Prometheus and sends them to Kafka. This behaviour can be configured with the following environment variables: | ||
|
||
- `PROM_METADATA_ENDPOINT`: defines prometheus metric metadata endpoint , not set by default and hence metadata wont be included. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we just made this a boolean instead (e.g. |
||
- `INLCUDED_METADATA`: specifies which attributes to be exported. The attributes should be comma separated. Permitted values are _type_, _help_ and _unit_. Only _type_ is included by default | ||
- `KAFKA_BROKER_LIST`: defines kafka endpoint and port, defaults to `kafka:9092`. | ||
- `KAFKA_TOPIC`: defines kafka topic to be used, defaults to `metrics`. Could use go template, labels are passed (as a map) to the template: e.g: `metrics.{{ index . "__name__" }}` to use per-metric topic. Two template functions are available: replace (`{{ index . "__name__" | replace "message" "msg" }}`) and substring (`{{ index . "__name__" | substring 0 5 }}`) | ||
- `KAFKA_COMPRESSION`: defines the compression type to be used, defaults to `none`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func GetAllMetricMetadata(promMetaDataEndPoint string, metricsList map[string]MetricMetadata) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels pretty hacky. Is there any way we could instead interface with the metrics object in memory instead of making an internal call? |
||
|
||
// Make a GET request to the Prometheus metadata API | ||
response, err := http.Get(promMetaDataEndPoint) | ||
if err != nil { | ||
logrus.WithError(err).Errorln("Error making request") | ||
return | ||
} | ||
defer response.Body.Close() | ||
|
||
// Read the response body | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
logrus.WithError(err).Errorln("Error reading response body") | ||
// logrus.error("Error reading response body: %s\n", err.Error()) | ||
return | ||
} | ||
|
||
// Parse the JSON data into a map | ||
var data map[string]interface{} | ||
err = json.Unmarshal([]byte(body), &data) | ||
if err != nil { | ||
logrus.WithError(err).Errorln("Error parsing json") | ||
return | ||
} | ||
// var metricList = make(map[string]MetricMetadata) | ||
for key, metrics := range data["data"].(map[string]interface{}) { | ||
for _, metric := range metrics.([]interface{}) { | ||
var metricMetadata MetricMetadata | ||
logrus.Debugf("Processing Metric %s, Metadata to be included %s", key, includedMetaData) | ||
|
||
if strings.Contains(strings.ToLower(includedMetaData), "type") { | ||
metricMetadata.metricType = metric.(map[string]interface{})["type"].(string) | ||
logrus.Debugf("Type is %s", metricMetadata.metricType) | ||
} | ||
if strings.Contains(strings.ToLower(includedMetaData), "help") { | ||
metricMetadata.metricHelp = metric.(map[string]interface{})["help"].(string) | ||
logrus.Debugf("Help is %s", metricMetadata.metricHelp) | ||
} | ||
if strings.Contains(strings.ToLower(includedMetaData), "unit") { | ||
metricMetadata.metricUnit = metric.(map[string]interface{})["unit"].(string) | ||
logrus.Debugf("Unit is %s", metricMetadata.metricUnit) | ||
} | ||
metricsList[key] = metricMetadata | ||
// fmt.Printf("Metric: %s, Type: %s, Help: %s, Unit: %s", key, metricMetadata.metricType, metricMetadata.metricHelp, metricMetadata.metricUnit) | ||
} | ||
} | ||
logrus.Debugf("Total number of metrics parsed is %v", len(metricsList)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean up extra spaces, please.