Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/app/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ func (a *App) promFormat(rrevs [][]*formatters.EventMsg, outName string) ([]byte
return nil, fmt.Errorf("output %q must be of type 'prometheus' or 'remote_write'", outName)
}
mb := &promcom.MetricBuilder{
Prefix: a.Config.FileConfig.GetString(outputPath + "/metric-prefix"),
AppendSubscriptionName: a.Config.FileConfig.GetBool(outputPath + "/append-subscription-name"),
StringsAsLabels: a.Config.FileConfig.GetBool(outputPath + "/strings-as-labels"),
OverrideTimestamps: a.Config.FileConfig.GetBool(outputPath + "/override-timestamps"),
ExportTimestamps: a.Config.FileConfig.GetBool(outputPath + "/export-timestamps"),
Prefix: a.Config.FileConfig.GetString(outputPath + "/metric-prefix"),
AppendSubscriptionName: a.Config.FileConfig.GetBool(outputPath + "/append-subscription-name"),
StringsAsLabels: a.Config.FileConfig.GetBool(outputPath + "/strings-as-labels"),
StringsAsSingleMetricLabels: a.Config.FileConfig.GetBool(outputPath + "/strings-as-single-metric-labels"),
OverrideTimestamps: a.Config.FileConfig.GetBool(outputPath + "/override-timestamps"),
ExportTimestamps: a.Config.FileConfig.GetBool(outputPath + "/export-timestamps"),
}

b := new(bytes.Buffer)
Expand Down
24 changes: 17 additions & 7 deletions pkg/outputs/prometheus_output/prometheus_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,24 @@ func (mb *MetricBuilder) MetricsFromEvent(ev *formatters.EventMsg, now time.Time
labels := mb.GetLabels(ev)
for vName, val := range ev.Values {
v, err := toFloat(val)
metricLabels := labels
if err != nil {
if !mb.StringsAsLabels {
if !mb.StringsAsLabels && !mb.StringsAsSingleMetricLabels {
continue
}
v = 1.0
if mb.StringsAsSingleMetricLabels {
if s, ok := val.(string); ok {
labelName := MetricNameRegex.ReplaceAllString(path.Base(vName), "_")
metricLabels = make([]prompb.Label, 0, len(labels)+1)
metricLabels = append(metricLabels, labels...)
metricLabels = append(metricLabels, prompb.Label{Name: labelName, Value: s})
}
}
}
pm := &PromMetric{
Name: mb.MetricName(ev.Name, vName),
labels: labels,
labels: metricLabels,
value: v,
AddedAt: now,
}
Expand All @@ -171,11 +180,12 @@ func (mb *MetricBuilder) MetricsFromEvent(ev *formatters.EventMsg, now time.Time
}

type MetricBuilder struct {
Prefix string
AppendSubscriptionName bool
StringsAsLabels bool
OverrideTimestamps bool
ExportTimestamps bool
Prefix string
AppendSubscriptionName bool
StringsAsLabels bool
StringsAsSingleMetricLabels bool
OverrideTimestamps bool
ExportTimestamps bool
}

func (m *MetricBuilder) GetLabels(ev *formatters.EventMsg) []prompb.Label {
Expand Down
56 changes: 52 additions & 4 deletions pkg/outputs/prometheus_output/prometheus_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,10 @@ func TestMetricBuilder_MetricsFromEvent(t *testing.T) {
tests := []struct {
name string // description of this test case
// Named input parameters for target function.
ev *formatters.EventMsg
now time.Time
want []*PromMetric
ev *formatters.EventMsg
now time.Time
stringsAsSingleMetricLabels bool
want []*PromMetric
}{
{
name: "no_duplicates",
Expand Down Expand Up @@ -385,11 +386,58 @@ func TestMetricBuilder_MetricsFromEvent(t *testing.T) {
},
},
},
{
name: "string_value_label_only_on_own_metric",
stringsAsSingleMetricLabels: true,
ev: &formatters.EventMsg{
Name: "sub1",
Timestamp: 1777889507942770536,
Tags: map[string]string{
"interface_name": "onu 1/1/95",
"source": "lab-olt",
},
Values: map[string]any{
"/interfaces-state/interface/admin-status": "1",
"/interfaces-state/interface/name": "onu 1/1/95",
"/interfaces-state/interface/onu/clei-code": "CLEI-1234",
},
},
now: time.Unix(0, 1777889507942770536),
want: []*PromMetric{
{
Name: "interfaces_state_interface_admin_status",
value: 1,
labels: []prompb.Label{
{Name: "interface_name", Value: "onu 1/1/95"},
{Name: "source", Value: "lab-olt"},
},
},
{
Name: "interfaces_state_interface_name",
value: 1,
labels: []prompb.Label{
{Name: "interface_name", Value: "onu 1/1/95"},
{Name: "source", Value: "lab-olt"},
{Name: "name", Value: "onu 1/1/95"},
},
},
{
Name: "interfaces_state_interface_onu_clei_code",
value: 1,
labels: []prompb.Label{
{Name: "interface_name", Value: "onu 1/1/95"},
{Name: "source", Value: "lab-olt"},
{Name: "clei_code", Value: "CLEI-1234"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mb := &MetricBuilder{
StringsAsLabels: true,
StringsAsLabels: !tt.stringsAsSingleMetricLabels,
StringsAsSingleMetricLabels: tt.stringsAsSingleMetricLabels,
}
got := mb.MetricsFromEvent(tt.ev, tt.now)
if len(got) != len(tt.want) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ type config struct {
OverrideTimestamps bool `mapstructure:"override-timestamps,omitempty" json:"override-timestamps,omitempty"`
AddTarget string `mapstructure:"add-target,omitempty" json:"add-target,omitempty"`
TargetTemplate string `mapstructure:"target-template,omitempty" json:"target-template,omitempty"`
StringsAsLabels bool `mapstructure:"strings-as-labels,omitempty" json:"strings-as-labels,omitempty"`
StringsAsLabels bool `mapstructure:"strings-as-labels,omitempty" json:"strings-as-labels,omitempty"`
StringsAsSingleMetricLabels bool `mapstructure:"strings-as-single-metric-labels,omitempty" json:"strings-as-single-metric-labels,omitempty"`
Debug bool `mapstructure:"debug,omitempty" json:"debug,omitempty"`
EventProcessors []string `mapstructure:"event-processors,omitempty" json:"event-processors,omitempty"`
ServiceRegistration *serviceRegistration `mapstructure:"service-registration,omitempty" json:"service-registration,omitempty"`
Expand Down Expand Up @@ -219,11 +220,12 @@ func (p *prometheusOutput) Init(ctx context.Context, name string, cfg map[string
}

dc.mb = &promcom.MetricBuilder{
Prefix: newCfg.MetricPrefix,
AppendSubscriptionName: newCfg.AppendSubscriptionName,
StringsAsLabels: newCfg.StringsAsLabels,
OverrideTimestamps: newCfg.OverrideTimestamps,
ExportTimestamps: newCfg.ExportTimestamps,
Prefix: newCfg.MetricPrefix,
AppendSubscriptionName: newCfg.AppendSubscriptionName,
StringsAsLabels: newCfg.StringsAsLabels,
StringsAsSingleMetricLabels: newCfg.StringsAsSingleMetricLabels,
OverrideTimestamps: newCfg.OverrideTimestamps,
ExportTimestamps: newCfg.ExportTimestamps,
}

p.dynCfg.Store(dc)
Expand Down Expand Up @@ -361,11 +363,12 @@ func (p *prometheusOutput) Update(ctx context.Context, cfg map[string]any) error

// metric builder
dc.mb = &promcom.MetricBuilder{
Prefix: tmp.MetricPrefix,
AppendSubscriptionName: tmp.AppendSubscriptionName,
StringsAsLabels: tmp.StringsAsLabels,
OverrideTimestamps: tmp.OverrideTimestamps,
ExportTimestamps: tmp.ExportTimestamps,
Prefix: tmp.MetricPrefix,
AppendSubscriptionName: tmp.AppendSubscriptionName,
StringsAsLabels: tmp.StringsAsLabels,
StringsAsSingleMetricLabels: tmp.StringsAsSingleMetricLabels,
OverrideTimestamps: tmp.OverrideTimestamps,
ExportTimestamps: tmp.ExportTimestamps,
}

p.dynCfg.Store(dc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ type config struct {
AppendSubscriptionName bool `mapstructure:"append-subscription-name,omitempty" json:"append-subscription-name,omitempty"`
AddTarget string `mapstructure:"add-target,omitempty" json:"add-target,omitempty"`
TargetTemplate string `mapstructure:"target-template,omitempty" json:"target-template,omitempty"`
StringsAsLabels bool `mapstructure:"strings-as-labels,omitempty" json:"strings-as-labels,omitempty"`
StringsAsLabels bool `mapstructure:"strings-as-labels,omitempty" json:"strings-as-labels,omitempty"`
StringsAsSingleMetricLabels bool `mapstructure:"strings-as-single-metric-labels,omitempty" json:"strings-as-single-metric-labels,omitempty"`
EventProcessors []string `mapstructure:"event-processors,omitempty" json:"event-processors,omitempty"`
NumWorkers int `mapstructure:"num-workers,omitempty" json:"num-workers,omitempty"`
NumWriters int `mapstructure:"num-writers,omitempty" json:"num-writers,omitempty"`
Expand Down Expand Up @@ -226,9 +227,10 @@ func (p *promWriteOutput) Init(ctx context.Context, name string, cfg map[string]
}

dc.mb = &promcom.MetricBuilder{
Prefix: ncfg.MetricPrefix,
AppendSubscriptionName: ncfg.AppendSubscriptionName,
StringsAsLabels: ncfg.StringsAsLabels,
Prefix: ncfg.MetricPrefix,
AppendSubscriptionName: ncfg.AppendSubscriptionName,
StringsAsLabels: ncfg.StringsAsLabels,
StringsAsSingleMetricLabels: ncfg.StringsAsSingleMetricLabels,
}

p.dynCfg.Store(dc)
Expand Down Expand Up @@ -304,9 +306,10 @@ func (p *promWriteOutput) Update(ctx context.Context, cfg map[string]any) error

// metric builder
dc.mb = &promcom.MetricBuilder{
Prefix: newCfg.MetricPrefix,
AppendSubscriptionName: newCfg.AppendSubscriptionName,
StringsAsLabels: newCfg.StringsAsLabels,
Prefix: newCfg.MetricPrefix,
AppendSubscriptionName: newCfg.AppendSubscriptionName,
StringsAsLabels: newCfg.StringsAsLabels,
StringsAsSingleMetricLabels: newCfg.StringsAsSingleMetricLabels,
}

// rebuild processors ?
Expand Down