Skip to content

Commit ab59244

Browse files
authored
Merge pull request gofr-dev#768 from gofr-dev/EN/docs-custom-metrics
Enhance custom metrics doc
2 parents 261b5dc + c5154ce commit ab59244

File tree

1 file changed

+71
-1
lines changed
  • docs/advanced-guide/publishing-custom-metrics

1 file changed

+71
-1
lines changed

docs/advanced-guide/publishing-custom-metrics/page.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464
app.Metrics().NewUpDownCounter("total_credit_day_sale", "used to track the total credit sales in a day")
6565

6666
app.POST("/sale", func(ctx *gofr.Context) (interface{}, error) {
67-
ctx.Metrics().IncrementCounter(ctx, "total_credit_day_sale")
67+
ctx.Metrics().DeltaUpDownCounter(ctx, "total_credit_day_sale", 1000)
6868

6969
return "Sale Completed", nil
7070
})
@@ -139,6 +139,76 @@ func main() {
139139
}
140140
```
141141

142+
## Adding Labels to Custom Metrics
143+
144+
GoFr leverages metrics support by enabling labels. Labels are a key feature in metrics that allow you to categorize and filter metrics based on relevant information.
145+
146+
### Understanding Labels
147+
148+
Labels are key-value pairs attached to metrics. They provide additional context about the metric data.
149+
150+
Common examples of labels include:
151+
- environment: (e.g., "production", "staging")
152+
- service: (e.g., "api-gateway", "database")
153+
- status: (e.g., "success", "failure")
154+
155+
By adding labels, you can create different time series for the same metric based on the label values.
156+
This allows for more granular analysis and visualization in Grafana (or any other) dashboards.
157+
158+
### Additional Considerations
159+
160+
- Prefer to keep the number of labels manageable to avoid overwhelming complexity.
161+
- Choose meaningful label names that clearly describe the data point.
162+
- Ensure consistency in label naming conventions across your application.
163+
164+
By effectively using labels in GoFr, you can enrich your custom metrics and gain deeper insights into your application's performance and behavior.
165+
166+
### Usage:
167+
168+
Labels are added while populating the data for metrics, by passing them as arguments (comma separated key-value pairs)
169+
in the GoFr's methods (namely: `IncreamentCounter`, `DeltaUpDownCounter`, `RecordHistogram`, `SetGauge`).
170+
171+
Example: `c.Metrics().IncrementCounter(c, "metric-name", "metric-value", "label-1", "value-1", "label-2", "value-2")`
172+
173+
```go
174+
package main
175+
176+
import (
177+
"gofr.dev/pkg/gofr"
178+
)
179+
180+
func main() {
181+
// Initialise gofr object
182+
a := gofr.New()
183+
184+
// Add custom metrics
185+
a.Metrics().NewUpDownCounter("total_credit_day_sale", "used to track the total credit sales in a day")
186+
187+
// Add all the routes
188+
a.POST("/sale", SaleHandler)
189+
a.POST("/return", ReturnHandler)
190+
191+
// Run the application
192+
a.Run()
193+
}
194+
195+
func SaleHandler(c *gofr.Context) (interface{}, error) {
196+
// logic to create sales
197+
198+
c.Metrics().DeltaUpDownCounter(c, "total_credit_day_sale", 10, "sale_type", "credit", "product_type", "beverage") // Here "sale_type" & "product_type" are the labels and "credit" & "beverage" are the values
199+
200+
return "Sale Successful", nil
201+
}
202+
203+
func ReturnHandler(c *gofr.Context) (interface{}, error) {
204+
// logic to create a sales return
205+
206+
c.Metrics().DeltaUpDownCounter(c, "total_credit_day_sale", -5, "sale_type", "credit_return", "product_type", "dairy")
207+
208+
return "Return Successful", nil
209+
}
210+
```
211+
142212
**Good To Know**
143213

144214
```doc

0 commit comments

Comments
 (0)