github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/statsd/formatter.go (about)

     1  // Copyright 2016 Google Inc. 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 statsd
    16  
    17  import (
    18  	"fmt"
    19  	"k8s.io/heapster/metrics/core"
    20  	"strings"
    21  	"unicode"
    22  )
    23  
    24  type Formatter interface {
    25  	Format(prefix string, name string, labels map[string]string, customizeLabel CustomizeLabel, metricValue core.MetricValue) (res string, err error)
    26  }
    27  
    28  type LabelStyle func(string) string
    29  type CustomizeLabel func(string) string
    30  
    31  type LabelCustomizer struct {
    32  	renameLabels map[string]string
    33  	labelStyle   LabelStyle
    34  }
    35  
    36  func (customizer LabelCustomizer) Customize(label string) string {
    37  	if val, ok := customizer.renameLabels[label]; ok {
    38  		label = val
    39  	}
    40  	return (customizer.labelStyle(label))
    41  }
    42  
    43  func NewFormatter(protocolType string) (formatter Formatter, err error) {
    44  	switch protocolType {
    45  	case "etsystatsd":
    46  		return NewEtsystatsdFormatter(), nil
    47  	case "influxstatsd":
    48  		return NewInfluxstatsdFormatter(), nil
    49  	default:
    50  		return nil, fmt.Errorf("Unknown statd formatter %s", protocolType)
    51  	}
    52  }
    53  
    54  func DefaultLabelStyle(str string) string {
    55  	return str
    56  }
    57  
    58  func SnakeToLowerCamel(str string) string {
    59  	res := SnakeToUpperCamel(str)
    60  
    61  	ustr := []rune(res)
    62  	ustr[0] = unicode.ToLower(ustr[0])
    63  	res = string(ustr)
    64  
    65  	return res
    66  }
    67  
    68  func SnakeToUpperCamel(str string) string {
    69  	res := ""
    70  	words := strings.Split(str, "_")
    71  	for _, word := range words {
    72  		res += strings.Title(word)
    73  	}
    74  	return res
    75  }