github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/components/metrics/labels.go (about)

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  
     8  	"github.com/wfusion/gofusion/common/infra/watermill/message"
     9  )
    10  
    11  const (
    12  	labelKeyHandlerName    = "handler_name"
    13  	labelKeyPublisherName  = "publisher_name"
    14  	labelKeySubscriberName = "subscriber_name"
    15  	labelSuccess           = "success"
    16  	labelAcked             = "acked"
    17  
    18  	labelValueNoHandler = "<no handler>"
    19  )
    20  
    21  var (
    22  	labelGetters = map[string]func(context.Context) string{
    23  		labelKeyHandlerName:    message.HandlerNameFromCtx,
    24  		labelKeyPublisherName:  message.PublisherNameFromCtx,
    25  		labelKeySubscriberName: message.SubscriberNameFromCtx,
    26  	}
    27  )
    28  
    29  func labelsFromCtx(ctx context.Context, labels ...string) prometheus.Labels {
    30  	ctxLabels := map[string]string{}
    31  
    32  	for _, l := range labels {
    33  		k := l
    34  		ctxLabels[l] = ""
    35  
    36  		getter, ok := labelGetters[k]
    37  		if !ok {
    38  			continue
    39  		}
    40  
    41  		v := getter(ctx)
    42  		if v != "" {
    43  			ctxLabels[l] = v
    44  		}
    45  	}
    46  
    47  	return ctxLabels
    48  }