Skip to content

Commit

Permalink
Add example of NewConstMetricWithCreatedTimestamp (#1375)
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
ArthurSens committed Nov 20, 2023
1 parent e160b50 commit 3f80cd1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions prometheus/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,27 @@ func ExampleNewMetricWithTimestamp() {
// Output:
// {"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"}}
}

0 comments on commit 3f80cd1

Please sign in to comment.