Skip to content

Commit

Permalink
update examples on promauto adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
amberpixels committed Nov 24, 2024
1 parent 30f9c92 commit 5efce43
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 60 deletions.
56 changes: 0 additions & 56 deletions prometheus/promsafe/promauto/auto_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package promauto
package promauto_adapter

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promsafe"
)

// NewCounterVec behaves as promauto.NewCounterVec but with type-safe labels
// NewCounterVec behaves as adapter-promauto.NewCounterVec but with type-safe labels
func NewCounterVec[T promsafe.LabelsProviderMarker](opts prometheus.CounterOpts) *promsafe.CounterVec[T] {
//_ = promauto.NewCounterVec // keeping for reference

Expand All @@ -22,12 +22,12 @@ type Factory[T promsafe.LabelsProviderMarker] struct {
r prometheus.Registerer
}

// With behaves same as promauto.With but with type-safe labels
// With behaves same as adapter-promauto.With but with type-safe labels
func With[T promsafe.LabelsProviderMarker](r prometheus.Registerer) Factory[T] {
return Factory[T]{r: r}
}

// NewCounterVec behaves like promauto.NewCounterVec but with type-safe labels
// NewCounterVec behaves like adapter-promauto.NewCounterVec but with type-safe labels
func (f Factory[T]) NewCounterVec(opts prometheus.CounterOpts) *promsafe.CounterVec[T] {
c := NewCounterVec[T](opts)
if f.r != nil {
Expand Down
44 changes: 44 additions & 0 deletions prometheus/promsafe/promauto_adapter/promauto_adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package promauto_adapter_test

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promsafe"
promauto "github.com/prometheus/client_golang/prometheus/promsafe/promauto_adapter"
)

func ExampleNewCounterVec_promauto_adapted() {
// Examples on how to migrate from promauto to promsafe gently
//
// Before:
// import "github.com/prometheus/client_golang/prometheus/promauto"
// func main() {
// myReg := prometheus.NewRegistry()
// counterOpts := prometheus.CounterOpts{Name:"..."}
// promauto.With(myReg).NewCounterVec(counterOpts, []string{"event_type", "source"})
// }
//
// After:
//
// import (
// promauto "github.com/prometheus/client_golang/prometheus/promsafe/promauto_adapter"
// )
// ...

myReg := prometheus.NewRegistry()
counterOpts := prometheus.CounterOpts{
Name: "items_counted_detailed_auto",
}

type MyLabels struct {
promsafe.StructLabelProvider
EventType string
Source string
}
c := promauto.With[MyLabels](myReg).NewCounterVec(counterOpts)

c.With(MyLabels{
EventType: "reservation", Source: "source1",
}).Inc()

// Output:
}

0 comments on commit 5efce43

Please sign in to comment.