-
Notifications
You must be signed in to change notification settings - Fork 2.9k
signaltometricsconnector: support gauges #40113
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
Changes from 3 commits
ccd8675
14787d8
6863748
24f6208
45acb64
2005227
d250618
d2cbdea
e47e265
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/lightstep/go-expohisto/structure" | ||
"go.opentelemetry.io/collector/component" | ||
|
@@ -163,6 +165,10 @@ | |
Value string `mapstructure:"value"` | ||
} | ||
|
||
type Gauge struct { | ||
Value string `mapstructure:"value"` | ||
} | ||
|
||
// MetricInfo defines the structure of the metric produced by the connector. | ||
type MetricInfo struct { | ||
Name string `mapstructure:"name"` | ||
|
@@ -181,6 +187,7 @@ | |
Histogram *Histogram `mapstructure:"histogram"` | ||
ExponentialHistogram *ExponentialHistogram `mapstructure:"exponential_histogram"` | ||
Sum *Sum `mapstructure:"sum"` | ||
Gauge *Gauge `mapstructure:"gauge"` | ||
// prevent unkeyed literal initialization | ||
_ struct{} | ||
} | ||
|
@@ -251,6 +258,15 @@ | |
return nil | ||
} | ||
|
||
func (mi *MetricInfo) validateGauge() error { | ||
if mi.Gauge != nil { | ||
if mi.Gauge.Value == "" { | ||
return errors.New("value must be defined for gauge metrics") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// validateMetricInfo is an utility method validate all supported metric | ||
// types defined for the metric info including any ottl expressions. | ||
func validateMetricInfo[K any](mi MetricInfo, parser ottl.Parser[K]) error { | ||
|
@@ -266,6 +282,9 @@ | |
if err := mi.validateSum(); err != nil { | ||
return fmt.Errorf("sum validation failed: %w", err) | ||
} | ||
if err := mi.validateGauge(); err != nil { | ||
return fmt.Errorf("gauge validation failed: %w", err) | ||
} | ||
|
||
// Exactly one metric should be defined. Also, validate OTTL expressions, | ||
// note that, here we only evaluate if statements are valid. Check for | ||
|
@@ -299,6 +318,19 @@ | |
return fmt.Errorf("failed to parse value OTTL expression for summary: %w", err) | ||
} | ||
} | ||
if mi.Gauge != nil { | ||
metricsDefinedCount++ | ||
if _, err := parser.ParseValueExpression(mi.Gauge.Value); err != nil { | ||
return fmt.Errorf("failed to parse value OTTL expression for gauge: %w", err) | ||
} | ||
// if ExtractGrokPatterns is used, validate the key selector | ||
if strings.Contains(mi.Gauge.Value, "ExtractGrokPatterns") { | ||
// Ensure a [key] selector is present after ExtractGrokPatterns | ||
if !regexp.MustCompile(`ExtractGrokPatterns\([^)]*\)\s*\[[^\]]+\]`).MatchString(mi.Gauge.Value) { | ||
simitt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fmt.Errorf("ExtractGrokPatterns: a single key selector[key] is required for signal to gauge") | ||
Check failure on line 330 in connector/signaltometricsconnector/config/config.go
|
||
} | ||
} | ||
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 check would be true for all configured metric types so maybe in a follow-up PR we can generalize this as a common validation for 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 is not that important though and we can just create an issue and leave it for future too as we might need to think about how to (or if to) handle config validation for all funcs that return map (or other non-primitives) 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. created #40118 |
||
} | ||
if metricsDefinedCount != 1 { | ||
return fmt.Errorf("exactly one of the metrics must be defined, %d found", metricsDefinedCount) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package aggregator // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/signaltometricsconnector/internal/aggregator" | ||
|
||
import ( | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
) | ||
|
||
// gaugeDP is a data point for gauge metrics. | ||
type gaugeDP struct { | ||
attrs pcommon.Map | ||
val any | ||
} | ||
|
||
func newGaugeDP(attrs pcommon.Map) *gaugeDP { | ||
return &gaugeDP{ | ||
attrs: attrs, | ||
} | ||
} | ||
|
||
func (dp *gaugeDP) Aggregate(v any) { | ||
switch v := v.(type) { | ||
case float64, int64: | ||
dp.val = v | ||
default: | ||
panic("unexpected usage of gauge datapoint, only double or int value expected") | ||
} | ||
} | ||
|
||
// Copy copies the gauge data point to the destination number data point | ||
func (dp *gaugeDP) Copy( | ||
timestamp time.Time, | ||
dest pmetric.NumberDataPoint, | ||
) { | ||
dp.attrs.CopyTo(dest.Attributes()) | ||
switch dp.val.(type) { | ||
Check failure on line 40 in connector/signaltometricsconnector/internal/aggregator/gaugedp.go
|
||
case float64: | ||
dest.SetDoubleValue(dp.val.(float64)) | ||
case int64: | ||
dest.SetIntValue(dp.val.(int64)) | ||
} | ||
// TODO determine appropriate start time | ||
dest.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
signaltometrics: | ||
logs: | ||
- name: logs.memory_mb | ||
description: Extract memory_mb from log records | ||
gauge: | ||
value: ExtractGrokPatterns(body, "Memory usage %{NUMBER:memory_mb:int}MB") |
Uh oh!
There was an error while loading. Please reload this page.