github.com/openshift-online/ocm-sdk-go@v0.1.473/metrics/labels.go (about)

     1  /*
     2  Copyright (c) 2021 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This file contains functions that calculate the labels included in metrics.
    18  
    19  package metrics
    20  
    21  import (
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  // serviceLabel calculates the `service` for the given URL path.
    27  func serviceLabel(path string) string {
    28  	if !strings.HasPrefix(path, "/api/") {
    29  		return ""
    30  	}
    31  	if strings.HasPrefix(path, "/api/accounts_mgmt") {
    32  		return "ocm-accounts-service"
    33  	} else if strings.HasPrefix(path, "/api/clusters_mgmt") {
    34  		return "ocm-clusters-service"
    35  	} else if strings.HasPrefix(path, "/api/authorizations") {
    36  		return "ocm-authorizations-service"
    37  	} else if strings.HasPrefix(path, "/api/service_logs") {
    38  		return "ocm-logs-service"
    39  	} else {
    40  		parts := strings.Split(path, "/")
    41  		if len(parts) > 3 {
    42  			return "ocm-" + parts[2]
    43  		}
    44  		return ""
    45  	}
    46  }
    47  
    48  // methodLabel calculates the `method` label from the given HTTP method.
    49  func methodLabel(method string) string {
    50  	return strings.ToUpper(method)
    51  }
    52  
    53  // pathLabel calculates the `path` label from the URL path.
    54  func pathLabel(paths pathTree, path string) string {
    55  	// Remove leading and trailing slashes:
    56  	for len(path) > 0 && strings.HasPrefix(path, "/") {
    57  		path = path[1:]
    58  	}
    59  	for len(path) > 0 && strings.HasSuffix(path, "/") {
    60  		path = path[0 : len(path)-1]
    61  	}
    62  
    63  	// Clear segments that correspond to path variables:
    64  	segments := strings.Split(path, "/")
    65  	current := paths
    66  	for i, segment := range segments {
    67  		next, ok := current[segment]
    68  		if ok {
    69  			current = next
    70  			continue
    71  		}
    72  		next, ok = current["-"]
    73  		if ok {
    74  			segments[i] = "-"
    75  			current = next
    76  			continue
    77  		}
    78  		return "/-"
    79  	}
    80  
    81  	// Reconstruct the path joining the modified segments:
    82  	return "/" + strings.Join(segments, "/")
    83  }
    84  
    85  // codeLabel calculates the `code` label from the given HTTP response.
    86  func codeLabel(code int) string {
    87  	return strconv.Itoa(code)
    88  }
    89  
    90  // Names of the labels added to metrics:
    91  const (
    92  	serviceLabelName = "apiservice"
    93  	codeLabelName    = "code"
    94  	methodLabelName  = "method"
    95  	pathLabelName    = "path"
    96  )
    97  
    98  // Array of labels added to call metrics:
    99  var requestLabelNames = []string{
   100  	serviceLabelName,
   101  	codeLabelName,
   102  	methodLabelName,
   103  	pathLabelName,
   104  }