github.com/Axway/agent-sdk@v1.1.101/pkg/transaction/metric/definition.go (about)

     1  package metric
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Axway/agent-sdk/pkg/transaction/models"
     7  	"github.com/sirupsen/logrus"
     8  )
     9  
    10  // use a variable for this to fake it for tests
    11  var now = time.Now
    12  
    13  const (
    14  	schemaPath              = "/api/v1/report.schema.json"
    15  	metricEvent             = "api.transaction.status.metric"
    16  	messageKey              = "message"
    17  	metricKey               = "metric"
    18  	metricFlow              = "api-central-metric"
    19  	metricRetries           = "metricRetry"
    20  	retries                 = "retries"
    21  	lighthouseTransactions  = "Transactions"
    22  	lighthouseVolume        = "Volume"
    23  	transactionCountMetric  = "transaction.count"
    24  	transactionVolumeMetric = "transaction.volume"
    25  	unknown                 = "unknown"
    26  )
    27  
    28  type TransactionContext struct {
    29  	APIDetails models.APIDetails
    30  	AppDetails models.AppDetails
    31  	StatusCode string
    32  }
    33  
    34  // Detail - holds the details for computing metrics
    35  // for API and consumer subscriptions
    36  type Detail struct {
    37  	APIDetails models.APIDetails
    38  	AppDetails models.AppDetails
    39  	StatusCode string
    40  	Duration   int64
    41  	Bytes      int64
    42  }
    43  
    44  type MetricDetail struct {
    45  	APIDetails  models.APIDetails
    46  	AppDetails  models.AppDetails
    47  	StatusCode  string
    48  	Count       int64
    49  	Response    ResponseMetrics
    50  	Observation ObservationDetails
    51  }
    52  
    53  // ResponseMetrics - Holds metrics API response
    54  type ResponseMetrics struct {
    55  	Max int64   `json:"max"`
    56  	Min int64   `json:"min"`
    57  	Avg float64 `json:"avg"`
    58  }
    59  
    60  // ObservationDetails - Holds start and end timestamp for interval
    61  type ObservationDetails struct {
    62  	Start int64 `json:"start,omitempty"`
    63  	End   int64 `json:"end,omitempty"`
    64  }
    65  
    66  // APIMetric - struct to hold metric aggregated for subscription,application,api,statuscode
    67  type APIMetric struct {
    68  	Subscription  models.Subscription  `json:"subscription"`
    69  	App           models.AppDetails    `json:"application"`
    70  	Product       models.Product       `json:"product,omitempty"`
    71  	API           models.APIDetails    `json:"api"`
    72  	AssetResource models.AssetResource `json:"assetResource,omitempty"`
    73  	ProductPlan   models.ProductPlan   `json:"productPlan,omitempty"`
    74  	Quota         models.Quota         `json:"quota,omitempty"`
    75  	StatusCode    string               `json:"statusCode"`
    76  	Status        string               `json:"status"`
    77  	Count         int64                `json:"count"`
    78  	Response      ResponseMetrics      `json:"response"`
    79  	Observation   ObservationDetails   `json:"observation"`
    80  	EventID       string               `json:"eventID"`
    81  	StartTime     time.Time            `json:"-"`
    82  }
    83  
    84  // GetStartTime - Returns the start time for subscription metric
    85  func (a *APIMetric) GetStartTime() time.Time {
    86  	return a.StartTime
    87  }
    88  
    89  // GetType - Returns APIMetric
    90  func (a *APIMetric) GetType() string {
    91  	return "APIMetric"
    92  }
    93  
    94  // GetType - Returns APIMetric
    95  func (a *APIMetric) GetEventID() string {
    96  	return a.EventID
    97  }
    98  
    99  func (a *APIMetric) GetLogFields() logrus.Fields {
   100  	fields := logrus.Fields{
   101  		"id":             a.EventID,
   102  		"count":          a.Count,
   103  		"status":         a.StatusCode,
   104  		"minResponse":    a.Response.Min,
   105  		"maxResponse":    a.Response.Max,
   106  		"avgResponse":    a.Response.Avg,
   107  		"startTimestamp": a.Observation.Start,
   108  		"endTimestamp":   a.Observation.End,
   109  	}
   110  	fields = a.Subscription.GetLogFields(fields)
   111  	fields = a.App.GetLogFields(fields)
   112  	fields = a.Product.GetLogFields(fields)
   113  	fields = a.API.GetLogFields(fields)
   114  	fields = a.AssetResource.GetLogFields(fields)
   115  	fields = a.ProductPlan.GetLogFields(fields)
   116  	fields = a.Quota.GetLogFields(fields)
   117  	return fields
   118  }
   119  
   120  // cachedMetric - struct to hold metric specific that gets cached and used for agent recovery
   121  type cachedMetric struct {
   122  	Subscription  models.Subscription  `json:"subscription,omitempty"`
   123  	App           models.AppDetails    `json:"app,omitempty"`
   124  	Product       models.Product       `json:"product,omitempty"`
   125  	API           models.APIDetails    `json:"api"`
   126  	AssetResource models.AssetResource `json:"assetResource,omitempty"`
   127  	ProductPlan   models.ProductPlan   `json:"productPlan,omitempty"`
   128  	Quota         models.Quota         `json:"quota,omitempty"`
   129  	StatusCode    string               `json:"statusCode"`
   130  	Count         int64                `json:"count"`
   131  	Values        []int64              `json:"values"`
   132  	StartTime     time.Time            `json:"startTime"`
   133  }
   134  
   135  // V4EventDistribution - represents V4 distribution
   136  type V4EventDistribution struct {
   137  	Environment string `json:"environment"`
   138  	Version     string `json:"version"`
   139  }
   140  
   141  // V4Session - represents V4 session
   142  type V4Session struct {
   143  	ID string `json:"id"`
   144  }
   145  
   146  // V4Data - Interface for representing the metric data
   147  type V4Data interface {
   148  	GetStartTime() time.Time
   149  	GetType() string
   150  	GetEventID() string
   151  	GetLogFields() logrus.Fields
   152  }
   153  
   154  // V4Event - represents V7 event
   155  type V4Event struct {
   156  	ID           string               `json:"id"`
   157  	Timestamp    int64                `json:"timestamp"`
   158  	Event        string               `json:"event"`
   159  	App          string               `json:"app,omitempty"` // ORG GUID
   160  	Version      string               `json:"version"`
   161  	Distribution *V4EventDistribution `json:"distribution"`
   162  	Data         V4Data               `json:"data"`
   163  	Session      *V4Session           `json:"session,omitempty"`
   164  }
   165  
   166  func (v V4Event) getLogFields() logrus.Fields {
   167  	return v.Data.GetLogFields()
   168  }
   169  
   170  // UsageReport -Lighthouse Usage report
   171  type UsageReport struct {
   172  	Product string                 `json:"product"`
   173  	Usage   map[string]int64       `json:"usage"`
   174  	Meta    map[string]interface{} `json:"meta"`
   175  }
   176  
   177  // UsageEvent -Lighthouse Usage Event
   178  type UsageEvent struct {
   179  	OrgGUID     string                 `json:"-"`
   180  	EnvID       string                 `json:"envId"`
   181  	Timestamp   ISO8601Time            `json:"timestamp"`
   182  	Granularity int                    `json:"granularity"`
   183  	SchemaID    string                 `json:"schemaId"`
   184  	Report      map[string]UsageReport `json:"report"`
   185  	Meta        map[string]interface{} `json:"meta"`
   186  }
   187  
   188  type UsageResponse struct {
   189  	Success     bool   `json:"success"`
   190  	Description string `json:"description"`
   191  	StatusCode  int    `json:"code"`
   192  }
   193  
   194  // Data - struct for data to report as API Metrics
   195  type Data struct {
   196  	APIDetails models.APIDetails
   197  	StatusCode string
   198  	Duration   int64
   199  	UsageBytes int64
   200  	AppDetails models.AppDetails
   201  	TeamName   string
   202  }
   203  
   204  // AppUsage - struct to hold metric specific for app usage
   205  type AppUsage struct {
   206  	App   models.AppDetails `json:"app"`
   207  	Count int64             `json:"count"`
   208  }
   209  
   210  // ISO8601 - time format
   211  const (
   212  	ISO8601 = "2006-01-02T15:04:00Z07:00"
   213  )
   214  
   215  // ISO8601Time - time
   216  type ISO8601Time time.Time
   217  
   218  // UnmarshalJSON - unmarshal json for time
   219  func (t *ISO8601Time) UnmarshalJSON(bytes []byte) error {
   220  	tt, err := time.Parse(`"`+ISO8601+`"`, string(bytes))
   221  	if err != nil {
   222  		return err
   223  	}
   224  	*t = ISO8601Time(tt)
   225  	return nil
   226  }
   227  
   228  // MarshalJSON -
   229  func (t ISO8601Time) MarshalJSON() ([]byte, error) {
   230  	tt := time.Time(t)
   231  
   232  	b := make([]byte, 0, len(ISO8601)+2)
   233  	b = append(b, '"')
   234  	b = tt.AppendFormat(b, ISO8601)
   235  	b = append(b, '"')
   236  	return b, nil
   237  }