MCPcopy
hub / github.com/prometheus/client_golang / ExampleNewMetricWithTimestamp

Function ExampleNewMetricWithTimestamp

prometheus/examples_test.go:688–716  ·  prometheus/examples_test.go::ExampleNewMetricWithTimestamp
()

Source from the content-addressed store, hash-verified

686}
687
688func ExampleNewMetricWithTimestamp() {
689 desc := prometheus.NewDesc(
690 "temperature_kelvin",
691 "Current temperature in Kelvin.",
692 nil, nil,
693 )
694
695 // Create a constant gauge from values we got from an external
696 // temperature reporting system. Those values are reported with a slight
697 // delay, so we want to add the timestamp of the actual measurement.
698 temperatureReportedByExternalSystem := 298.15
699 timeReportedByExternalSystem := time.Date(2009, time.November, 10, 23, 0, 0, 12345678, time.UTC)
700 s := prometheus.NewMetricWithTimestamp(
701 timeReportedByExternalSystem,
702 prometheus.MustNewConstMetric(
703 desc, prometheus.GaugeValue, temperatureReportedByExternalSystem,
704 ),
705 )
706
707 // Just for demonstration, let's check the state of the gauge by
708 // (ab)using its Write method (which is usually only used by Prometheus
709 // internally).
710 metric := &dto.Metric{}
711 s.Write(metric)
712 fmt.Println(toNormalizedJSON(metric))
713
714 // Output:
715 // {"gauge":{"value":298.15},"timestampMs":"1257894000012"}
716}
717
718func ExampleNewConstMetricWithCreatedTimestamp() {
719 // Here we have a metric that is reported by an external system.

Callers

nothing calls this directly

Calls 6

NewDescFunction · 0.92
NewMetricWithTimestampFunction · 0.92
MustNewConstMetricFunction · 0.92
toNormalizedJSONFunction · 0.85
WriteMethod · 0.65
PrintlnMethod · 0.65

Tested by

no test coverage detected