-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Refactor Prometheus metric mappings #9948
Changes from 1 commit
64a5c0b
e3004d0
c2fc75e
86bc56a
411cc77
7ac6a9b
28167f9
ec1feed
229c108
694d58d
940734d
5a970c4
5657e00
b6af126
2536029
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
- key: prometheus | ||
title: "Prometheus" | ||
description: > | ||
Stats collected from Prometheus. | ||
Stats scraped from a Prometheus endpoint. | ||
short_config: false | ||
release: ga | ||
settings: ["ssl", "http"] | ||
fields: | ||
- name: prometheus | ||
type: group | ||
# Order is important here, labels will match first, the rest are double | ||
- name: prometheus.labels.* | ||
type: object | ||
object_type: keyword | ||
description: > | ||
fields: | ||
Prometheus metric labels | ||
- name: prometheus.* | ||
type: object | ||
object_type: double | ||
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. 👍 |
||
object_type_mapping_type: "*" | ||
description: > | ||
Prometheus metric |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ import ( | |
"fmt" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/common/cfgwarn" | ||
p "github.com/elastic/beats/metricbeat/helper/prometheus" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/mb/parse" | ||
|
@@ -50,20 +49,9 @@ func init() { | |
type MetricSet struct { | ||
mb.BaseMetricSet | ||
prometheus p.Prometheus | ||
namespace string | ||
} | ||
|
||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
cfgwarn.Beta("The prometheus collector metricset is beta") | ||
|
||
config := struct { | ||
Namespace string `config:"namespace" validate:"required"` | ||
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. 👍 |
||
}{} | ||
err := base.Module().UnpackConfig(&config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
prometheus, err := p.NewPrometheusClient(base) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -72,42 +60,39 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | |
return &MetricSet{ | ||
BaseMetricSet: base, | ||
prometheus: prometheus, | ||
namespace: config.Namespace, | ||
}, nil | ||
} | ||
|
||
func (m *MetricSet) Fetch() ([]common.MapStr, error) { | ||
func (m *MetricSet) Fetch(reporter mb.ReporterV2) { | ||
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. exported method MetricSet.Fetch should have comment or be unexported |
||
families, err := m.prometheus.GetFamilies() | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Unable to decode response from prometheus endpoint") | ||
reporter.Error(fmt.Errorf("Unable to decode response from prometheus endpoint")) | ||
return | ||
} | ||
|
||
eventList := map[string]common.MapStr{} | ||
|
||
for _, family := range families { | ||
promEvents := GetPromEventsFromMetricFamily(family) | ||
promEvents := getPromEventsFromMetricFamily(family) | ||
|
||
for _, promEvent := range promEvents { | ||
if _, ok := eventList[promEvent.labelHash]; !ok { | ||
eventList[promEvent.labelHash] = common.MapStr{} | ||
labelsHash := promEvent.LabelsHash() | ||
if _, ok := eventList[labelsHash]; !ok { | ||
eventList[labelsHash] = common.MapStr{} | ||
|
||
// Add labels | ||
if len(promEvent.labels) > 0 { | ||
eventList[promEvent.labelHash]["label"] = promEvent.labels | ||
eventList[labelsHash]["labels"] = promEvent.labels | ||
} | ||
} | ||
|
||
eventList[promEvent.labelHash][promEvent.key] = promEvent.value | ||
eventList[labelsHash].Update(promEvent.data) | ||
} | ||
} | ||
|
||
// Converts hash list to slice | ||
events := []common.MapStr{} | ||
for _, e := range eventList { | ||
e[mb.NamespaceKey] = m.namespace | ||
events = append(events, e) | ||
reporter.Event(mb.Event{ModuleFields: e}) | ||
} | ||
|
||
return events, err | ||
} |
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.
What about placing labels in the base level to avoid possible issues with this? This is also the recommendation in ECS.
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.
I would prefer to keep it like this for now, main reasons are:
labels.*
I want to make sure this module won't end on mapping issues and we can use these metrics reliably, not against this change once the problem is gone (for instance, if/when index per module is implemented).
WDYT?
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.
LGTM, but I wonder some things (not blocking this PR)
Why would we want to do it here?
I guess this is no different with any other module, maybe we shouldn't have a field for labels in ECS till we don't have a complete solution for these mapping issues.
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.
That could make sense, I still have some doubts on how ECS will play out in cases like this, I would prefer to play it safe on this module and learn from the process with others 😇