github.com/grafana/synthetic-monitoring-agent@v0.64.2/pkg/accounting/accounting.go (about)

     1  // Package accounting provides information about the number of active
     2  // series produed by checks and other related metrics.
     3  package accounting
     4  
     5  import (
     6  	"errors"
     7  	"strings"
     8  
     9  	"github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
    10  )
    11  
    12  //go:generate ./generate-active-series-counts
    13  
    14  var ErrUnhandledCheck = errors.New("cannot compute the number of active series for check")
    15  
    16  // GetActiveSeriesForCheck returns the number of active series that the
    17  // provided check produces. The data is embedded in the program at build
    18  // time and it's obtained from the test data used to keep the generated
    19  // series in sync with the rest of the program.
    20  //
    21  // This is of course dependent on the running agent being the same
    22  // version as the code embedded in the program using this information.
    23  func GetActiveSeriesForCheck(check synthetic_monitoring.Check) (int, error) {
    24  	accountingClass, err := GetCheckAccountingClass(check)
    25  	if err != nil {
    26  		return 0, err
    27  	}
    28  
    29  	as, found := activeSeriesByCheckType[accountingClass]
    30  	if !found {
    31  		return 0, ErrUnhandledCheck
    32  	}
    33  
    34  	return as, nil
    35  }
    36  
    37  // GetCheckAccountingClass returns the accounting class corresponding to
    38  // the specified check.
    39  func GetCheckAccountingClass(check synthetic_monitoring.Check) (string, error) {
    40  	checkType := check.Type()
    41  	key := checkType.String()
    42  
    43  	switch checkType {
    44  	case synthetic_monitoring.CheckTypeDns:
    45  
    46  	case synthetic_monitoring.CheckTypeHttp:
    47  		if strings.HasPrefix(check.Target, "https://") {
    48  			key += "_ssl"
    49  		}
    50  
    51  	case synthetic_monitoring.CheckTypeScripted, synthetic_monitoring.CheckTypeBrowser:
    52  
    53  	case synthetic_monitoring.CheckTypeMultiHttp:
    54  
    55  	case synthetic_monitoring.CheckTypePing:
    56  
    57  	case synthetic_monitoring.CheckTypeTcp:
    58  		if check.Settings.Tcp.Tls {
    59  			key += "_ssl"
    60  		}
    61  
    62  	case synthetic_monitoring.CheckTypeTraceroute:
    63  
    64  	case synthetic_monitoring.CheckTypeGrpc:
    65  		if check.Settings.Grpc.Tls {
    66  			key += "_ssl"
    67  		}
    68  
    69  	default:
    70  		return "", ErrUnhandledCheck
    71  	}
    72  
    73  	if check.BasicMetricsOnly {
    74  		key += "_basic"
    75  	}
    76  
    77  	return key, nil
    78  }
    79  
    80  // ClassInfo contains information about a specific accounting class
    81  type ClassInfo struct {
    82  	CheckType  synthetic_monitoring.CheckType  // the correspodning check type for this class
    83  	CheckClass synthetic_monitoring.CheckClass // the check class for this accounting class
    84  	Series     int                             // how many series does this class of check produce
    85  }
    86  
    87  // GetAccountingClassInfo returns all the known accounting classes and
    88  // their corresponding information
    89  func GetAccountingClassInfo() map[string]ClassInfo {
    90  	info := make(map[string]ClassInfo, len(activeSeriesByCheckType))
    91  
    92  	for class, as := range activeSeriesByCheckType {
    93  		checkType := getTypeFromClass(class)
    94  		info[class] = ClassInfo{
    95  			CheckType:  getTypeFromClass(class),
    96  			CheckClass: checkType.Class(),
    97  			Series:     as,
    98  		}
    99  	}
   100  
   101  	return info
   102  }
   103  
   104  // getTypeFromClass is a helper that returns the corresponding check
   105  // type for a given accounting class.
   106  func getTypeFromClass(class string) synthetic_monitoring.CheckType {
   107  	// We know that the accounting class is built by appending
   108  	// variations to the base check type so strip that to get back
   109  	// the check type.
   110  	str := strings.SplitN(class, "_", 2)[0]
   111  
   112  	checkType, found := synthetic_monitoring.CheckTypeFromString(str)
   113  	if !found {
   114  		panic("unhandled check type string")
   115  	}
   116  
   117  	return checkType
   118  }