-
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
Add multiplier option to prometheus helper #10994
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 |
---|---|---|
|
@@ -57,6 +57,13 @@ func OpLowercaseValue() MetricOption { | |
return opLowercaseValue{} | ||
} | ||
|
||
// OpMultiplyBuckets multiplies bucket labels in histograms, useful to change units | ||
func OpMultiplyBuckets(multiplier float64) MetricOption { | ||
return opMultiplyBuckets{ | ||
multiplier: multiplier, | ||
} | ||
} | ||
|
||
// Metric directly maps a Prometheus metric to a Metricbeat field | ||
func Metric(field string, options ...MetricOption) MetricMap { | ||
return &commonMetric{ | ||
|
@@ -267,3 +274,30 @@ func (o opLowercaseValue) Process(field string, value interface{}, labels common | |
} | ||
return field, value, labels | ||
} | ||
|
||
type opMultiplyBuckets struct { | ||
multiplier float64 | ||
} | ||
|
||
// Process will multiply the bucket labels if it is an histogram with numeric labels | ||
func (o opMultiplyBuckets) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { | ||
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's the reason for not using pointers here? 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. Good question, I didn't think about this, I just followed what was being done for other options. In any case I wouldn't change it now, we can consider doing it for all options in the future. |
||
histogram, ok := value.(common.MapStr) | ||
if !ok { | ||
return field, value, labels | ||
} | ||
bucket, ok := histogram["bucket"].(common.MapStr) | ||
if !ok { | ||
return field, value, labels | ||
} | ||
multiplied := common.MapStr{} | ||
for k, v := range bucket { | ||
if f, err := strconv.ParseFloat(k, 64); err == nil { | ||
key := strconv.FormatFloat(f*o.multiplier, 'f', -1, 64) | ||
multiplied[key] = v | ||
} else { | ||
multiplied[k] = v | ||
} | ||
} | ||
histogram["bucket"] = multiplied | ||
return field, histogram, labels | ||
} |
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'd call this
NewOpMultiplyBucketsOption
unless we have some internal convention for thisThere 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 don't have an strong opinion on this but yes, I am following the naming schemas used here.
Take into account that they are not so bad as they are used as options in prometheus metrics: