github.com/newrelic/go-agent@v3.26.0+incompatible/internal/apdex.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package internal 5 6 import "time" 7 8 // ApdexZone is a transaction classification. 9 type ApdexZone int 10 11 // https://en.wikipedia.org/wiki/Apdex 12 const ( 13 ApdexNone ApdexZone = iota 14 ApdexSatisfying 15 ApdexTolerating 16 ApdexFailing 17 ) 18 19 // ApdexFailingThreshold calculates the threshold at which the transaction is 20 // considered a failure. 21 func ApdexFailingThreshold(threshold time.Duration) time.Duration { 22 return 4 * threshold 23 } 24 25 // CalculateApdexZone calculates the apdex based on the transaction duration and 26 // threshold. 27 // 28 // Note that this does not take into account whether or not the transaction 29 // had an error. That is expected to be done by the caller. 30 func CalculateApdexZone(threshold, duration time.Duration) ApdexZone { 31 if duration <= threshold { 32 return ApdexSatisfying 33 } 34 if duration <= ApdexFailingThreshold(threshold) { 35 return ApdexTolerating 36 } 37 return ApdexFailing 38 } 39 40 func (zone ApdexZone) label() string { 41 switch zone { 42 case ApdexSatisfying: 43 return "S" 44 case ApdexTolerating: 45 return "T" 46 case ApdexFailing: 47 return "F" 48 default: 49 return "" 50 } 51 }