github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/transformation/unary_multi.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 transformation
    22  
    23  import (
    24  	"math"
    25  	"time"
    26  )
    27  
    28  // transformReset returns the provided datapoint and a zero datapoint one second later.
    29  //
    30  // This transform is useful for force resetting a counter value in Prometheus. When running the M3Aggregator in HA, both
    31  // the follower and leader are computing aggregate counters, but they started counting at different times. If these
    32  // counters are emitted as monotonic cumulative counters, during failover the counter decreases if the new leader
    33  // started counting later. Prometheus assumes any decrease in a counter is due a counter reset, which leads to strange
    34  // display results since the counter did not actually reset.
    35  //
    36  // This transform gets around this issue by explicitly not accumulating results, like Add, and force resets the counter
    37  // with a zero value so PromQL properly graphs the delta as the rate value.
    38  //
    39  // This does have the downside of an extra 0 datapoint per resolution period. The storage cost is more than just the
    40  // extra 0 value since the value is stored 1 second after the actual datapoint. This degrades the timestamp encoding
    41  // since the timestamps are no longer at a fixed interval. In practice we see a 3x increase in storage for these
    42  // aggregated counters.
    43  //
    44  // Currently only a single extra datapoint per aggregation is supported. If multiple transforms in an aggregation emit
    45  // an additional datapoint, only the last one is used.
    46  func transformReset() UnaryMultiOutputTransform {
    47  	return UnaryMultiOutputTransformFn(func(dp Datapoint, resolution time.Duration) (Datapoint, Datapoint) {
    48  		// Add the reset datapoint to be half the resolution period to ensure equal spacing between datapoints.
    49  		// We take the max with 1 to ensure there's at least a 1 nanosecond gap.
    50  		resetWindow := int64(math.Max(float64(resolution.Nanoseconds()/2), 1))
    51  
    52  		return dp, Datapoint{Value: 0, TimeNanos: dp.TimeNanos + resetWindow*int64(time.Nanosecond)}
    53  	})
    54  }