fix(outputs.prometheus_client): Fix export_timestamp for v1 metric type (#13169)

Co-authored-by: Roman Orlov <orlov@farpost.com>
This commit is contained in:
Orlov Roman 2023-05-02 21:46:57 +10:00 committed by GitHub
parent 80b8fe6333
commit 20921eb535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -100,7 +100,7 @@ func (p *PrometheusClient) Init() error {
default:
fallthrough
case 1:
p.collector = v1.NewCollector(time.Duration(p.ExpirationInterval), p.StringAsLabel, p.Log)
p.collector = v1.NewCollector(time.Duration(p.ExpirationInterval), p.StringAsLabel, p.ExportTimestamp, p.Log)
err := registry.Register(p.collector)
if err != nil {
return err

View File

@ -105,6 +105,35 @@ cpu_time_idle{host="example.org"} 42
# HELP cpu_time_idle Telegraf collected metric
# TYPE cpu_time_idle counter
cpu_time_idle{host="example.org"} 42
`),
},
{
name: "when export timestamp is true timestamp is present in the metric",
output: &PrometheusClient{
Listen: ":0",
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
ExportTimestamp: true,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu_time_idle",
map[string]string{
"host": "example.org",
},
map[string]interface{}{
"counter": 42.0,
},
time.Unix(1257894000, 0),
telegraf.Counter,
),
},
expected: []byte(`
# HELP cpu_time_idle Telegraf collected metric
# TYPE cpu_time_idle counter
cpu_time_idle{host="example.org"} 42 1257894000000
`),
},
{

View File

@ -61,10 +61,11 @@ type Collector struct {
expireTicker *time.Ticker
}
func NewCollector(expire time.Duration, stringsAsLabel bool, logger telegraf.Logger) *Collector {
func NewCollector(expire time.Duration, stringsAsLabel bool, exportTimestamp bool, logger telegraf.Logger) *Collector {
c := &Collector{
ExpirationInterval: expire,
StringAsLabel: stringsAsLabel,
ExportTimestamp: exportTimestamp,
Log: logger,
fam: make(map[string]*MetricFamily),
}