Skip to content

add _created support: add createdTime() method to counter #1044

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewCounter(opts CounterOpts) Counter {
opts.ConstLabels,
)
result := &counter{desc: desc, labelPairs: desc.constLabelPairs, now: time.Now}
result.initCreated()
result.init(result) // Init self-collection.
return result
}
Expand All @@ -88,8 +89,9 @@ type counter struct {
// valInt stores values that are exact integers. Both have to go first
// in the struct to guarantee alignment for atomic operations.
// http://golang.org/pkg/sync/atomic/#pkg-note-BUG
valBits uint64
valInt uint64
valBits uint64
valInt uint64
createdTime uint64
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this field is ever used, no?

Choose a reason for hiding this comment

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

It's initialized on L166 in initCreated() call.

Presumably, a forthcoming change would use it for serving in a Prometheus counter metric.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, but we would need this to be either in this PR ideally so we see how end solution would look like 🤗

Definitely it's good direction!


selfCollector
desc *Desc
Expand Down Expand Up @@ -161,6 +163,10 @@ func (c *counter) updateExemplar(v float64, l Labels) {
c.exemplar.Store(e)
}

func (c *counter) initCreated() {
c.createdTime = uint64(c.now().Unix())
}

// CounterVec is a Collector that bundles a set of Counters that all share the
// same Desc, but have different values for their variable labels. This is used
// if you want to count the same thing partitioned by various dimensions
Expand All @@ -185,6 +191,7 @@ func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec {
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, lvs))
}
result := &counter{desc: desc, labelPairs: MakeLabelPairs(desc, lvs), now: time.Now}
result.initCreated()
result.init(result) // Init self-collection.
return result
}),
Expand Down
14 changes: 14 additions & 0 deletions prometheus/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,17 @@ func TestCounterExemplar(t *testing.T) {
t.Error("adding exemplar with oversized labels succeeded")
}
}

func TestCounterCreatedTime(t *testing.T) {
now := time.Now()

counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
}).(*counter)
counter.now = func() time.Time { return now }
counter.initCreated()
if float64(now.Unix()) != float64(counter.createdTime) {
t.Error("counter has different created timestamp as initialized")
}
}