go.temporal.io/server@v1.23.0/common/metrics/tags.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package metrics
    26  
    27  import (
    28  	"fmt"
    29  	"strconv"
    30  	"strings"
    31  
    32  	enumspb "go.temporal.io/api/enums/v1"
    33  	enumsspb "go.temporal.io/server/api/enums/v1"
    34  
    35  	"go.temporal.io/server/common/primitives"
    36  )
    37  
    38  const (
    39  	gitRevisionTag   = "git_revision"
    40  	buildDateTag     = "build_date"
    41  	buildVersionTag  = "build_version"
    42  	buildPlatformTag = "build_platform"
    43  	goVersionTag     = "go_version"
    44  
    45  	instance       = "instance"
    46  	namespace      = "namespace"
    47  	namespaceState = "namespace_state"
    48  	targetCluster  = "target_cluster"
    49  	fromCluster    = "from_cluster"
    50  	toCluster      = "to_cluster"
    51  	taskQueue      = "taskqueue"
    52  	workflowType   = "workflowType"
    53  	activityType   = "activityType"
    54  	commandType    = "commandType"
    55  	serviceName    = "service_name"
    56  	actionType     = "action_type"
    57  	// Generic reason tag can be used anywhere a reason is needed.
    58  	reason = "reason"
    59  	// See server.api.enums.v1.ReplicationTaskType
    60  	replicationTaskType = "replicationTaskType"
    61  
    62  	namespaceAllValue = "all"
    63  	unknownValue      = "_unknown_"
    64  	totalMetricSuffix = "_total"
    65  	tagExcludedValue  = "_tag_excluded_"
    66  
    67  	getType     = "%T"
    68  	errorPrefix = "*"
    69  )
    70  
    71  // Tag is an interface to define metrics tags
    72  type Tag interface {
    73  	Key() string
    74  	Value() string
    75  }
    76  
    77  var StickyTaskQueueTag = TaskQueueTag("__sticky__")
    78  
    79  type (
    80  	tagImpl struct {
    81  		key   string
    82  		value string
    83  	}
    84  )
    85  
    86  func (v *tagImpl) Key() string {
    87  	return v.key
    88  }
    89  
    90  func (v *tagImpl) Value() string {
    91  	return v.value
    92  }
    93  
    94  // NamespaceTag returns a new namespace tag. For timers, this also ensures that we
    95  // dual emit the metric with the all tag. If a blank namespace is provided then
    96  // this converts that to an unknown namespace.
    97  func NamespaceTag(value string) Tag {
    98  	if len(value) == 0 {
    99  		value = unknownValue
   100  	}
   101  	return &tagImpl{
   102  		key:   namespace,
   103  		value: value,
   104  	}
   105  }
   106  
   107  var namespaceUnknownTag = &tagImpl{key: namespace, value: unknownValue}
   108  
   109  // NamespaceUnknownTag returns a new namespace:unknown tag-value
   110  func NamespaceUnknownTag() Tag {
   111  	return namespaceUnknownTag
   112  }
   113  
   114  // NamespaceStateTag returns a new namespace state tag.
   115  func NamespaceStateTag(value string) Tag {
   116  	if len(value) == 0 {
   117  		value = unknownValue
   118  	}
   119  	return &tagImpl{
   120  		key:   namespaceState,
   121  		value: value,
   122  	}
   123  }
   124  
   125  var taskQueueUnknownTag = &tagImpl{key: taskQueue, value: unknownValue}
   126  
   127  // TaskQueueUnknownTag returns a new taskqueue:unknown tag-value
   128  func TaskQueueUnknownTag() Tag {
   129  	return taskQueueUnknownTag
   130  }
   131  
   132  // InstanceTag returns a new instance tag
   133  func InstanceTag(value string) Tag {
   134  	return &tagImpl{key: instance, value: value}
   135  }
   136  
   137  // TargetClusterTag returns a new target cluster tag.
   138  func TargetClusterTag(value string) Tag {
   139  	if len(value) == 0 {
   140  		value = unknownValue
   141  	}
   142  	return &tagImpl{key: targetCluster, value: value}
   143  }
   144  
   145  // FromClusterIDTag returns a new from cluster tag.
   146  func FromClusterIDTag(value int32) Tag {
   147  	return &tagImpl{key: fromCluster, value: strconv.FormatInt(int64(value), 10)}
   148  }
   149  
   150  // ToClusterIDTag returns a new to cluster tag.
   151  func ToClusterIDTag(value int32) Tag {
   152  	return &tagImpl{key: toCluster, value: strconv.FormatInt(int64(value), 10)}
   153  }
   154  
   155  // TaskQueueTag returns a new task queue tag.
   156  func TaskQueueTag(value string) Tag {
   157  	if len(value) == 0 {
   158  		value = unknownValue
   159  	}
   160  	return &tagImpl{key: taskQueue, value: sanitizer.Value(value)}
   161  }
   162  
   163  func TaskQueueTypeTag(tqType enumspb.TaskQueueType) Tag {
   164  	return &tagImpl{key: TaskTypeTagName, value: tqType.String()}
   165  }
   166  
   167  // WorkflowTypeTag returns a new workflow type tag.
   168  func WorkflowTypeTag(value string) Tag {
   169  	if len(value) == 0 {
   170  		value = unknownValue
   171  	}
   172  	return &tagImpl{key: workflowType, value: value}
   173  }
   174  
   175  // ActivityTypeTag returns a new activity type tag.
   176  func ActivityTypeTag(value string) Tag {
   177  	if len(value) == 0 {
   178  		value = unknownValue
   179  	}
   180  	return &tagImpl{key: activityType, value: value}
   181  }
   182  
   183  // CommandTypeTag returns a new command type tag.
   184  func CommandTypeTag(value string) Tag {
   185  	if len(value) == 0 {
   186  		value = unknownValue
   187  	}
   188  	return &tagImpl{key: commandType, value: value}
   189  }
   190  
   191  // Returns a new service role tag.
   192  func ServiceRoleTag(value string) Tag {
   193  	if len(value) == 0 {
   194  		value = unknownValue
   195  	}
   196  	return &tagImpl{key: ServiceRoleTagName, value: value}
   197  }
   198  
   199  // Returns a new failure type tag
   200  func FailureTag(value string) Tag {
   201  	if len(value) == 0 {
   202  		value = unknownValue
   203  	}
   204  	return &tagImpl{key: FailureTagName, value: value}
   205  }
   206  
   207  func TaskCategoryTag(value string) Tag {
   208  	if len(value) == 0 {
   209  		value = unknownValue
   210  	}
   211  	return &tagImpl{key: TaskCategoryTagName, value: value}
   212  }
   213  
   214  func TaskTypeTag(value string) Tag {
   215  	if len(value) == 0 {
   216  		value = unknownValue
   217  	}
   218  	return &tagImpl{key: TaskTypeTagName, value: value}
   219  }
   220  
   221  func TaskPriorityTag(value string) Tag {
   222  	if len(value) == 0 {
   223  		value = unknownValue
   224  	}
   225  	return &tagImpl{key: TaskPriorityTagName, value: value}
   226  }
   227  
   228  func QueueReaderIDTag(readerID int64) Tag {
   229  	return &tagImpl{key: QueueReaderIDTagName, value: strconv.Itoa(int(readerID))}
   230  }
   231  
   232  func QueueActionTag(value string) Tag {
   233  	if len(value) == 0 {
   234  		value = unknownValue
   235  	}
   236  	return &tagImpl{key: QueueActionTagName, value: value}
   237  }
   238  
   239  func QueueTypeTag(value string) Tag {
   240  	if len(value) == 0 {
   241  		value = unknownValue
   242  	}
   243  	return &tagImpl{key: QueueTypeTagName, value: value}
   244  }
   245  
   246  func VisibilityPluginNameTag(value string) Tag {
   247  	if value == "" {
   248  		value = unknownValue
   249  	}
   250  	return &tagImpl{key: visibilityPluginNameTagName, value: value}
   251  }
   252  
   253  // VersionedTag represents whether a loaded task queue manager represents a specific version set.
   254  func VersionedTag(versioned bool) Tag {
   255  	return &tagImpl{key: versionedTagName, value: strconv.FormatBool(versioned)}
   256  }
   257  
   258  func ServiceErrorTypeTag(err error) Tag {
   259  	return &tagImpl{key: ErrorTypeTagName, value: strings.TrimPrefix(fmt.Sprintf(getType, err), errorPrefix)}
   260  }
   261  
   262  // HttpStatusTag returns a new httpStatusTag.
   263  func HttpStatusTag(value int) Tag {
   264  	return &tagImpl{key: httpStatusTagName, value: strconv.Itoa(value)}
   265  }
   266  
   267  func ResourceExhaustedCauseTag(cause enumspb.ResourceExhaustedCause) Tag {
   268  	return &tagImpl{key: resourceExhaustedTag, value: cause.String()}
   269  }
   270  
   271  func ServiceNameTag(value primitives.ServiceName) Tag {
   272  	return &tagImpl{key: serviceName, value: string(value)}
   273  }
   274  
   275  func ActionType(value string) Tag {
   276  	return &tagImpl{key: actionType, value: value}
   277  }
   278  
   279  func OperationTag(value string) Tag {
   280  	return &tagImpl{key: OperationTagName, value: value}
   281  }
   282  
   283  func StringTag(key string, value string) Tag {
   284  	return &tagImpl{key: key, value: value}
   285  }
   286  
   287  func CacheTypeTag(value string) Tag {
   288  	return &tagImpl{key: CacheTypeTagName, value: value}
   289  }
   290  
   291  // ReasonString is just a string but the special type is defined here to remind callers of ReasonTag to limit the
   292  // cardinality of possible reasons.
   293  type ReasonString string
   294  
   295  // ReasonTag is a generic tag can be used anywhere a reason is needed.
   296  // Make sure that the value is of limited cardinality.
   297  func ReasonTag(value ReasonString) Tag {
   298  	return &tagImpl{key: reason, value: string(value)}
   299  }
   300  
   301  // ReplicationTaskTypeTag returns a new replication task type tag.
   302  func ReplicationTaskTypeTag(value enumsspb.ReplicationTaskType) Tag {
   303  	return &tagImpl{key: replicationTaskType, value: value.String()}
   304  }