Skip to content
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

Converge to typed value in prometheus output #3104

Merged
merged 1 commit into from
Aug 10, 2017
Merged
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
12 changes: 11 additions & 1 deletion plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,17 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
}
p.fam[mname] = fam
} else {
if fam.ValueType != vt {
// Metrics can be untyped even though the corresponding plugin
// creates them with a type. This happens when the metric was
// transferred over the network in a format that does not
// preserve value type and received using an input such as a
// queue consumer. To avoid issues we automatically upgrade
// value type from untyped to a typed metric.
if fam.ValueType == prometheus.UntypedValue {
fam.ValueType = vt
}

if vt != prometheus.UntypedValue && fam.ValueType != vt {
// Don't return an error since this would be a permanent error
log.Printf("Mixed ValueType for measurement %q; dropping point", point.Name())
break
Expand Down
50 changes: 50 additions & 0 deletions plugins/outputs/prometheus_client/prometheus_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,56 @@ func TestWrite_MixedValueType(t *testing.T) {
require.Equal(t, 1, len(fam.Samples))
}

func TestWrite_MixedValueTypeUpgrade(t *testing.T) {
now := time.Now()
p1, err := metric.New(
"foo",
map[string]string{"a": "x"},
map[string]interface{}{"value": 1.0},
now,
telegraf.Untyped)
p2, err := metric.New(
"foo",
map[string]string{"a": "y"},
map[string]interface{}{"value": 2.0},
now,
telegraf.Gauge)
var metrics = []telegraf.Metric{p1, p2}

client := NewClient()
err = client.Write(metrics)
require.NoError(t, err)

fam, ok := client.fam["foo"]
require.True(t, ok)
require.Equal(t, 2, len(fam.Samples))
}

func TestWrite_MixedValueTypeDowngrade(t *testing.T) {
now := time.Now()
p1, err := metric.New(
"foo",
map[string]string{"a": "x"},
map[string]interface{}{"value": 1.0},
now,
telegraf.Gauge)
p2, err := metric.New(
"foo",
map[string]string{"a": "y"},
map[string]interface{}{"value": 2.0},
now,
telegraf.Untyped)
var metrics = []telegraf.Metric{p1, p2}

client := NewClient()
err = client.Write(metrics)
require.NoError(t, err)

fam, ok := client.fam["foo"]
require.True(t, ok)
require.Equal(t, 2, len(fam.Samples))
}

func TestWrite_Tags(t *testing.T) {
now := time.Now()
p1, err := metric.New(
Expand Down