Add example of NewConstMetricWithCreatedTimestamp (#1375)

Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
This commit is contained in:
Arthur Silva Sens 2023-11-20 06:56:35 -03:00 committed by GitHub
parent e160b505d3
commit 3f80cd1055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 0 deletions

View File

@ -655,3 +655,27 @@ func ExampleNewMetricWithTimestamp() {
// Output: // Output:
// {"gauge":{"value":298.15},"timestampMs":"1257894000012"} // {"gauge":{"value":298.15},"timestampMs":"1257894000012"}
} }
func ExampleNewConstMetricWithCreatedTimestamp() {
// Here we have a metric that is reported by an external system.
// Besides providing the value, the external system also provides the
// timestamp when the metric was created.
desc := prometheus.NewDesc(
"time_since_epoch_seconds",
"Current epoch time in seconds.",
nil, nil,
)
timeSinceEpochReportedByExternalSystem := time.Date(2009, time.November, 10, 23, 0, 0, 12345678, time.UTC)
epoch := time.Unix(0, 0).UTC()
s := prometheus.MustNewConstMetricWithCreatedTimestamp(
desc, prometheus.CounterValue, float64(timeSinceEpochReportedByExternalSystem.Unix()), epoch,
)
metric := &dto.Metric{}
s.Write(metric)
fmt.Println(toNormalizedJSON(metric))
// Output:
// {"counter":{"value":1257894000,"createdTimestamp":"1970-01-01T00:00:00Z"}}
}