Skip to content

Commit

Permalink
Fix duplicate naming for prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Oct 8, 2024
1 parent 42937e7 commit 5697728
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion example/promProvider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func main() {

provider := promprovider.NewPromProvider(
promprovider.WithRegistry(registry),
promprovider.WithHttpServer(),
promprovider.WithHttpServer(), //Activate Kitex monitoring

Check failure on line 41 in example/promProvider/main.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
promprovider.WithRPCServer(), //Activate Hertz monitoring
)

provider.Serve(":9090", "/metrics-demo")
Expand Down
18 changes: 9 additions & 9 deletions telemetry/provider/promprovider/promprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewPromProvider(opts ...Option) *promProvider {
if cfg.enableRPC {
RPCCounterVec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: buildName(cfg.name, semantic.Counter),
Name: buildName(cfg.name, "rpc", semantic.Counter),
Help: fmt.Sprintf("Total number of requires completed by the %s, regardless of success or failure.", semantic.Counter),
},
[]string{semantic.LabelRPCCallerKey, semantic.LabelRPCCalleeKey, semantic.LabelRPCMethodKey, semantic.LabelKeyStatus},
Expand All @@ -67,7 +67,7 @@ func NewPromProvider(opts ...Option) *promProvider {

clientHandledHistogramRPC := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: buildName(cfg.name, semantic.Latency),
Name: buildName(cfg.name, "rpc", semantic.Latency),
Help: fmt.Sprintf("Latency (microseconds) of the %s until it is finished.", semantic.Latency),
Buckets: cfg.buckets,
},
Expand All @@ -78,13 +78,13 @@ func NewPromProvider(opts ...Option) *promProvider {
// create retry recorder
retryHandledHistogramRPC := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: buildName(cfg.name, semantic.Retry),
Name: buildName(cfg.name, "rpc", semantic.Retry),
Help: fmt.Sprintf("Distribution of retry attempts for %s until it is finished.", semantic.Retry),
Buckets: retryBuckets,
},
[]string{semantic.LabelRPCCallerKey, semantic.LabelRPCCalleeKey, semantic.LabelRPCMethodKey},
)
registry.MustRegister(clientHandledHistogramRPC)
registry.MustRegister(retryHandledHistogramRPC)
retryRecorder := metric.NewPromRecorder(retryHandledHistogramRPC)

metrics = append(metrics,
Expand All @@ -96,7 +96,7 @@ func NewPromProvider(opts ...Option) *promProvider {
if cfg.enableHTTP {
HttpCounterVec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: buildName(cfg.name, semantic.Counter),
Name: buildName(cfg.name, "http", semantic.Counter),
Help: "Total number of HTTPs completed by the server, regardless of success or failure.",
},
[]string{semantic.LabelHttpMethodKey, semantic.LabelStatusCode, semantic.LabelPath},
Expand All @@ -106,7 +106,7 @@ func NewPromProvider(opts ...Option) *promProvider {

HttpHandledHistogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: buildName(cfg.name, semantic.Latency),
Name: buildName(cfg.name, "http", semantic.Latency),
Help: "Latency (microseconds) of HTTP that had been application-level handled by the server.",
Buckets: cfg.buckets,
},
Expand Down Expand Up @@ -142,9 +142,9 @@ func (p *promProvider) Serve(addr, path string) {
}()
}

func buildName(name, service string) string {
func buildName(name, protocol, service string) string {
if name != "" {
return fmt.Sprintf("%s_%s", name, service)
return fmt.Sprintf("%s_%s_%s", name, protocol, service)
}
return service
return fmt.Sprintf("%s_%s", protocol, service)
}

0 comments on commit 5697728

Please sign in to comment.