Skip to content

Commit d714f96

Browse files
kir4hVincent Orlowski
authored and
Vincent Orlowski
committed
Fix export timestamp not working for prometheus on v2 (influxdata#7289)
1 parent 5c27e9b commit d714f96

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

plugins/outputs/prometheus_client/prometheus_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (p *PrometheusClient) Init() error {
139139
return err
140140
}
141141
case 2:
142-
p.collector = v2.NewCollector(p.ExpirationInterval.Duration, p.StringAsLabel)
142+
p.collector = v2.NewCollector(p.ExpirationInterval.Duration, p.StringAsLabel, p.ExportTimestamp)
143143
err := registry.Register(p.collector)
144144
if err != nil {
145145
return err

plugins/outputs/prometheus_client/prometheus_client_v2_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ func TestMetricVersion2(t *testing.T) {
4848
# HELP cpu_time_idle Telegraf collected metric
4949
# TYPE cpu_time_idle untyped
5050
cpu_time_idle{host="example.org"} 42
51+
`),
52+
},
53+
{
54+
name: "when export timestamp is true timestamp is present in the metric",
55+
output: &PrometheusClient{
56+
Listen: ":0",
57+
MetricVersion: 2,
58+
CollectorsExclude: []string{"gocollector", "process"},
59+
Path: "/metrics",
60+
ExportTimestamp: true,
61+
Log: Logger,
62+
},
63+
metrics: []telegraf.Metric{
64+
testutil.MustMetric(
65+
"cpu",
66+
map[string]string{
67+
"host": "example.org",
68+
},
69+
map[string]interface{}{
70+
"time_idle": 42.0,
71+
},
72+
time.Unix(0, 0),
73+
),
74+
},
75+
expected: []byte(`
76+
# HELP cpu_time_idle Telegraf collected metric
77+
# TYPE cpu_time_idle untyped
78+
cpu_time_idle{host="example.org"} 42 0
5179
`),
5280
},
5381
{

plugins/outputs/prometheus_client/v2/collector.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ type Collector struct {
4343
coll *serializer.Collection
4444
}
4545

46-
func NewCollector(expire time.Duration, stringsAsLabel bool) *Collector {
46+
func NewCollector(expire time.Duration, stringsAsLabel bool, exportTimestamp bool) *Collector {
4747
config := serializer.FormatConfig{}
4848
if stringsAsLabel {
4949
config.StringHandling = serializer.StringAsLabel
5050
}
51+
52+
if exportTimestamp {
53+
config.TimestampExport = serializer.ExportTimestamp
54+
}
55+
5156
return &Collector{
5257
expireDuration: expire,
5358
coll: serializer.NewCollection(config),

0 commit comments

Comments
 (0)