agones.dev/agones@v1.54.0/pkg/metrics/util.go (about)

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metrics
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  	"strings"
    22  	"unicode/utf8"
    23  
    24  	"agones.dev/agones/pkg/util/runtime"
    25  	"contrib.go.opencensus.io/exporter/stackdriver"
    26  	"go.opencensus.io/stats"
    27  	"go.opencensus.io/tag"
    28  )
    29  
    30  var (
    31  	logger = runtime.NewLoggerWithSource("metrics")
    32  
    33  	keyName       = MustTagKey("name")
    34  	keyNamespace  = MustTagKey("namespace")
    35  	keyFleetName  = MustTagKey("fleet_name")
    36  	keyType       = MustTagKey("type")
    37  	keyStatusCode = MustTagKey("status_code")
    38  	keyVerb       = MustTagKey("verb")
    39  	keyEndpoint   = MustTagKey("endpoint")
    40  	keyEmpty      = MustTagKey("empty")
    41  	keyCounter    = MustTagKey("counter")
    42  	keyList       = MustTagKey("list")
    43  )
    44  
    45  // RecordWithTags records a metric value and tags
    46  func RecordWithTags(ctx context.Context, mutators []tag.Mutator, ms ...stats.Measurement) {
    47  	if err := stats.RecordWithTags(ctx, mutators, ms...); err != nil {
    48  		logger.WithError(err).Warn("error while recoding stats")
    49  	}
    50  }
    51  
    52  // MustTagKey creates a new `tag.Key` from a string, panic if the key is not a valid.
    53  func MustTagKey(key string) tag.Key {
    54  	t, err := tag.NewKey(key)
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  	return t
    59  }
    60  
    61  func parseLabels(s string) (*stackdriver.Labels, error) {
    62  	res := &stackdriver.Labels{}
    63  	if s == "" {
    64  		return res, nil
    65  	}
    66  	pairs := strings.Split(s, ",")
    67  	for _, p := range pairs {
    68  		keyValue := strings.Split(p, "=")
    69  		if len(keyValue) != 2 {
    70  			return nil, fmt.Errorf("invalid labels: %s, expect key=value,key2=value2", s)
    71  		}
    72  		key := strings.TrimSpace(keyValue[0])
    73  		value := strings.TrimSpace(keyValue[1])
    74  
    75  		if key == "" {
    76  			return nil, errors.New("invalid key: can not be empty")
    77  		}
    78  
    79  		if value == "" {
    80  			return nil, fmt.Errorf("invalid value for key %s: can not be empty", key)
    81  		}
    82  
    83  		if !utf8.ValidString(key) {
    84  			return nil, fmt.Errorf("invalid key: %s, must be a valid utf-8 string", key)
    85  		}
    86  
    87  		if !utf8.ValidString(value) {
    88  			return nil, fmt.Errorf("invalid value: %s, must be a valid utf-8 string", value)
    89  		}
    90  
    91  		res.Set(key, value, "")
    92  	}
    93  	return res, nil
    94  }