From 83a2a655ae93d945069002f92690b3765470b7b4 Mon Sep 17 00:00:00 2001 From: Eugene Date: Fri, 20 Sep 2024 00:32:13 +0300 Subject: [PATCH] add example showing that using pointer to labels struct is fine Signed-off-by: Eugene --- prometheus/promsafe/safe_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/prometheus/promsafe/safe_test.go b/prometheus/promsafe/safe_test.go index 304055c..56b3330 100644 --- a/prometheus/promsafe/safe_test.go +++ b/prometheus/promsafe/safe_test.go @@ -118,6 +118,28 @@ func ExampleNewCounterVecT_promauto_global_migrated() { // Output: } +func ExampleNewCounterVecT_pointer_to_labels_promauto() { + // It's possible to use pointer to labels struct + myReg := prometheus.NewRegistry() + + counterOpts := prometheus.CounterOpts{ + Name: "items_counted_detailed_ptr", + } + + type MyLabels struct { + promsafe.StructLabelProvider + EventType string + Source string + } + c := promsafe.WithAuto[*MyLabels](myReg).NewCounterVecT(counterOpts) + + c.With(&MyLabels{ + EventType: "reservation", Source: "source1", + }).Inc() + + // Output: +} + func ExampleNewCounterVecT_single_label_manual() { // Manually registering with a single label // Example of usage of shorthand: no structs no generics, but one string only