Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a wrapper to add a timestamp to a metric #443

Merged
merged 2 commits into from
Aug 21, 2018
Merged

Conversation

beorn7
Copy link
Member

@beorn7 beorn7 commented Aug 18, 2018

Supersedes #407 .

@simonpasquier

Signed-off-by: beorn7 beorn@soundcloud.com

beorn7 added 2 commits August 18, 2018 15:56
Signed-off-by: beorn7 <beorn@soundcloud.com>
Signed-off-by: beorn7 <beorn@soundcloud.com>
return e
}

// NewMetricWithTimestamp returns a new Metric wrapping the provided Metric in a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like it works with direct instrumenation, I think we should try to limit this to MustNewConstMetric. Everyone I've seen trying to do timestamps on direct instrumentation was otherwise using the library incorrectly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usual use case is already described in the doc comment. Artificially limiting this to the output of MustNewConstMetric (and MustNewConstSummary and MustNewConstHistogram and NewConstMetric and NewConstSummary and NewConstHistogram) would be weird and cumbersome. Using this wrapper is arcane enough to not happen accidentally. Whoever elects to use such a contraption without reading the doc comments is on their own.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional point: Since the timestamp is immutable, people that incorrectly wrap metrics from direct instrumentation will quickly notice that they are exposing the same timestamp forever.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true.

Copy link

@luqasn luqasn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great - simple and effective :) thanks!


func (m timestampedMetric) Write(pb *dto.Metric) error {
e := m.Metric.Write(pb)
pb.TimestampMs = proto.Int64(m.t.Unix()*1000 + int64(m.t.Nanosecond()/1000000))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably does not make a difference, but I’d return early in case of an error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow my comment here got lost.

It probably really doesn't make a difference, but I find it a bit more robust to set the timestamp even in case of an error. We don't explicitly document to not make any use of the protomessage after an error is returned, so there might be a future use where the error conveys some message that looking at the proto message still makes sense.

Right now, of course, nobody should ever look at the proto message after an error, but then it doesn't matter if we set the timestamp or not.

@simonpasquier
Copy link
Member

👍 very nice!

@beorn7 beorn7 merged commit 29e6500 into master Aug 21, 2018
@beorn7 beorn7 deleted the beorn7/timestamp branch August 21, 2018 13:54
@atyronesmith
Copy link

Many thanks for this enhancement! This allows for fine-grain event correlation and opens Prometheus to a much wider set of applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants