github.com/m3db/m3@v1.5.0/src/cmd/services/m3coordinator/ingest/metrics.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package ingest
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/uber-go/tally"
    27  )
    28  
    29  // LatencyBuckets are a set of latency buckets useful for measuring things.
    30  type LatencyBuckets struct {
    31  	WriteLatencyBuckets  tally.DurationBuckets
    32  	IngestLatencyBuckets tally.DurationBuckets
    33  }
    34  
    35  // NewLatencyBuckets returns write and ingest latency buckets useful for
    36  // measuring ingest latency (i.e. time from datapoint/sample created to time
    37  // ingested) and write latency (i.e. time from received a sample from remote
    38  // source to completion of that write locally).
    39  func NewLatencyBuckets() (LatencyBuckets, error) {
    40  	upTo1sBuckets, err := tally.LinearDurationBuckets(0, 100*time.Millisecond, 10)
    41  	if err != nil {
    42  		return LatencyBuckets{}, err
    43  	}
    44  
    45  	upTo10sBuckets, err := tally.LinearDurationBuckets(time.Second, 500*time.Millisecond, 18)
    46  	if err != nil {
    47  		return LatencyBuckets{}, err
    48  	}
    49  
    50  	upTo60sBuckets, err := tally.LinearDurationBuckets(10*time.Second, 5*time.Second, 11)
    51  	if err != nil {
    52  		return LatencyBuckets{}, err
    53  	}
    54  
    55  	upTo60mBuckets, err := tally.LinearDurationBuckets(0, 5*time.Minute, 12)
    56  	if err != nil {
    57  		return LatencyBuckets{}, err
    58  	}
    59  	upTo60mBuckets = upTo60mBuckets[1:] // Remove the first 0s to get 5 min aligned buckets
    60  
    61  	upTo6hBuckets, err := tally.LinearDurationBuckets(time.Hour, 30*time.Minute, 12)
    62  	if err != nil {
    63  		return LatencyBuckets{}, err
    64  	}
    65  
    66  	upTo24hBuckets, err := tally.LinearDurationBuckets(6*time.Hour, time.Hour, 19)
    67  	if err != nil {
    68  		return LatencyBuckets{}, err
    69  	}
    70  	upTo24hBuckets = upTo24hBuckets[1:] // Remove the first 6h to get 1 hour aligned buckets
    71  
    72  	var writeLatencyBuckets tally.DurationBuckets
    73  	writeLatencyBuckets = append(writeLatencyBuckets, upTo1sBuckets...)
    74  	writeLatencyBuckets = append(writeLatencyBuckets, upTo10sBuckets...)
    75  	writeLatencyBuckets = append(writeLatencyBuckets, upTo60sBuckets...)
    76  	writeLatencyBuckets = append(writeLatencyBuckets, upTo60mBuckets...)
    77  
    78  	var ingestLatencyBuckets tally.DurationBuckets
    79  	ingestLatencyBuckets = append(ingestLatencyBuckets, upTo1sBuckets...)
    80  	ingestLatencyBuckets = append(ingestLatencyBuckets, upTo10sBuckets...)
    81  	ingestLatencyBuckets = append(ingestLatencyBuckets, upTo60sBuckets...)
    82  	ingestLatencyBuckets = append(ingestLatencyBuckets, upTo60mBuckets...)
    83  	ingestLatencyBuckets = append(ingestLatencyBuckets, upTo6hBuckets...)
    84  	ingestLatencyBuckets = append(ingestLatencyBuckets, upTo24hBuckets...)
    85  
    86  	return LatencyBuckets{
    87  		WriteLatencyBuckets:  writeLatencyBuckets,
    88  		IngestLatencyBuckets: ingestLatencyBuckets,
    89  	}, nil
    90  }