github.com/uber/kraken@v0.1.4/metrics/metrics.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package metrics
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"io"
    20  	"os"
    21  	"time"
    22  
    23  	"github.com/uber/kraken/utils/log"
    24  
    25  	"github.com/uber-go/tally"
    26  )
    27  
    28  func init() {
    29  	register("statsd", newStatsdScope)
    30  	register("disabled", newDisabledScope)
    31  	register("m3", newM3Scope)
    32  }
    33  
    34  var _scopeFactories = make(map[string]scopeFactory)
    35  
    36  type scopeFactory func(config Config, cluster string) (tally.Scope, io.Closer, error)
    37  
    38  func register(name string, f scopeFactory) {
    39  	if _, ok := _scopeFactories[name]; ok {
    40  		log.Fatalf("Metrics reporter factory %q is already registered", name)
    41  	}
    42  	_scopeFactories[name] = f
    43  }
    44  
    45  // New creates a new metrics Scope from config. If no backend is configured, metrics
    46  // are disabled.
    47  func New(config Config, cluster string) (tally.Scope, io.Closer, error) {
    48  	if config.Backend == "" {
    49  		config.Backend = "disabled"
    50  	}
    51  	f, ok := _scopeFactories[config.Backend]
    52  	if !ok || f == nil {
    53  		return nil, nil, fmt.Errorf("metrics backend %q not registered", config.Backend)
    54  	}
    55  	return f(config, cluster)
    56  }
    57  
    58  // EmitVersion periodically emits the current GIT_DESCRIBE as a metric.
    59  func EmitVersion(stats tally.Scope) {
    60  	counter, err := getVersionCounter(stats)
    61  	if err != nil {
    62  		log.Warnf("Skipping version emitting: %s", err)
    63  		return
    64  	}
    65  	for {
    66  		time.Sleep(time.Minute)
    67  		counter.Inc(1)
    68  	}
    69  }
    70  
    71  func getVersionCounter(stats tally.Scope) (tally.Counter, error) {
    72  	version := os.Getenv("GIT_DESCRIBE")
    73  	if version == "" {
    74  		return nil, errors.New("no GIT_DESCRIBE env variable found")
    75  	}
    76  	hostname, err := os.Hostname()
    77  	if err != nil {
    78  		return nil, fmt.Errorf("hostname: %s", err)
    79  	}
    80  	return stats.Tagged(map[string]string{
    81  		"host":    hostname,
    82  		"version": version,
    83  	}).Counter("version"), nil
    84  }