So NewMetricWithTimestamp() returns a Metric interface object that you can
then emit from a Collector's Collect() method. See this example from
cadvisor:
https://github.com/google/cadvisor/blob/19df107fd64fa31efc90e186af91b97f38d205e9/metrics/prometheus.go#L1931-L1934

You can see more usage example here:
https://sourcegraph.com/search?q=context:global+prometheus.NewMetricWithTimestamp&patternType=literal

In general, it seems like you are building an exporter (a process
that proxies/translates existing values into the Prometheus format, in your
case those existing values are coming from a file), so you are not
instrumenting the exporting process itself, and thus you probably don't
want to use the "NewGauge()" / "mygauge.WithLabelValues().Set()" functions
that are for direct instrumentation of a process. Instead, you'll want to
implement a Collector interface that just returns a set of proxied metrics,
as outlined here:

*
https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#hdr-Custom_Collectors_and_constant_Metrics
* https://prometheus.io/docs/instrumenting/writing_exporters/#collectors

On Tue, Aug 24, 2021 at 7:43 PM Prince <[email protected]> wrote:

> Thank you, As I understood the  NewMetricWithTimestamp() takes two
> parameters one the time and the other one is metric. So as my metric name
> is go_duration, so I did this :
>
> *1st way:*
> *go func(){*
> go_duration.WithLabelValues("type").Set(12345.678)
> prometheus.NewMetricWithTimestamp(time_var, go_duration )
> *}()*
>
> *2nd way: *
> *go func(){*
>
> prometheus.NewMetricWithTimestamp(time_var,go_duration.WithLabelValues("type").Set(12345.678))
> *}()*
>
>
> Using 1st way not getting the timestamp only values are getting scarped.
> Using 2nd way getting error as: 
> "go_duration.WithLabelValues("type").Set(12345.678)
> used as a value"
> On Tuesday, August 24, 2021 at 10:15:57 PM UTC+5:30 [email protected]
> wrote:
>
>> Hi,
>>
>> You should be able to use the NewMetricWithTimestamp() function for this:
>> https://pkg.go.dev/github.com/prometheus/client_golang/prometheus?utm_source=godoc#NewMetricWithTimestamp
>>
>> Note that client-side timestamps should only be used in exceptional
>> circumstances, and if you still expect those timestamps to be regularly
>> updated (because otherwise Prometheus will just collect a dot here and
>> there and mostly show empty graphs). If that is not the case, consider
>> omitting the client-side timestamp and instead sending a metric that
>> includes the last-update timestamp in its sample value (like the
>> node_exporter does for the mtime metric in its "textfile" collector module:
>> https://github.com/prometheus/node_exporter/blob/b6215e649cdfc0398ca98df8e63f3773f1725840/collector/textfile.go#L38
>> )
>>
>> Regards,
>> Julius
>>
>> On Mon, Aug 23, 2021 at 2:03 PM Prince <[email protected]> wrote:
>>
>>> Hi everyone, I am new to prometheus. I am using type Gauge. I wanted to
>>> get the timestamp along with the value. It will be great if anyone can help
>>> on this.
>>> example:
>>> go_duration.WithLabelValues("type").Set(12345.678)
>>>
>>> so here collector is getting only 12345.678, I am reading this data from
>>> a file where along with the vale there is a corresponding timestamp is
>>> there. I want that too.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Prometheus Developers" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/prometheus-developers/f365ad0c-75b0-4bad-a75d-3980f0f61669n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/prometheus-developers/f365ad0c-75b0-4bad-a75d-3980f0f61669n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Julius Volz
>> PromLabs - promlabs.com
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Prometheus Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/prometheus-developers/a69144c4-cfe1-42d8-9a46-a9fd1230f421n%40googlegroups.com
> <https://groups.google.com/d/msgid/prometheus-developers/a69144c4-cfe1-42d8-9a46-a9fd1230f421n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Julius Volz
PromLabs - promlabs.com

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-developers/CAObpH5y%3DoStT5kBJ%2BKKWnTFB3M4zbnu635XfT3f7moDxiLm-Jg%40mail.gmail.com.

Reply via email to