golang.org/x/tools@v0.21.0/internal/event/export/ocagent/wire/metrics.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wire
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  )
    11  
    12  type ExportMetricsServiceRequest struct {
    13  	Node     *Node     `json:"node,omitempty"`
    14  	Metrics  []*Metric `json:"metrics,omitempty"`
    15  	Resource *Resource `json:"resource,omitempty"`
    16  }
    17  
    18  type Metric struct {
    19  	MetricDescriptor *MetricDescriptor `json:"metric_descriptor,omitempty"`
    20  	Timeseries       []*TimeSeries     `json:"timeseries,omitempty"`
    21  	Resource         *Resource         `json:"resource,omitempty"`
    22  }
    23  
    24  type MetricDescriptor struct {
    25  	Name        string                `json:"name,omitempty"`
    26  	Description string                `json:"description,omitempty"`
    27  	Unit        string                `json:"unit,omitempty"`
    28  	Type        MetricDescriptor_Type `json:"type,omitempty"`
    29  	LabelKeys   []*LabelKey           `json:"label_keys,omitempty"`
    30  }
    31  
    32  type MetricDescriptor_Type int32
    33  
    34  const (
    35  	MetricDescriptor_UNSPECIFIED             MetricDescriptor_Type = 0
    36  	MetricDescriptor_GAUGE_INT64             MetricDescriptor_Type = 1
    37  	MetricDescriptor_GAUGE_DOUBLE            MetricDescriptor_Type = 2
    38  	MetricDescriptor_GAUGE_DISTRIBUTION      MetricDescriptor_Type = 3
    39  	MetricDescriptor_CUMULATIVE_INT64        MetricDescriptor_Type = 4
    40  	MetricDescriptor_CUMULATIVE_DOUBLE       MetricDescriptor_Type = 5
    41  	MetricDescriptor_CUMULATIVE_DISTRIBUTION MetricDescriptor_Type = 6
    42  	MetricDescriptor_SUMMARY                 MetricDescriptor_Type = 7
    43  )
    44  
    45  type LabelKey struct {
    46  	Key         string `json:"key,omitempty"`
    47  	Description string `json:"description,omitempty"`
    48  }
    49  
    50  type TimeSeries struct {
    51  	StartTimestamp *Timestamp    `json:"start_timestamp,omitempty"`
    52  	LabelValues    []*LabelValue `json:"label_values,omitempty"`
    53  	Points         []*Point      `json:"points,omitempty"`
    54  }
    55  
    56  type LabelValue struct {
    57  	Value    string `json:"value,omitempty"`
    58  	HasValue bool   `json:"has_value,omitempty"`
    59  }
    60  
    61  type Point struct {
    62  	Timestamp *Timestamp `json:"timestamp,omitempty"`
    63  	Value     PointValue `json:"value,omitempty"`
    64  }
    65  
    66  type PointInt64Value struct {
    67  	Int64Value int64 `json:"int64Value,omitempty"`
    68  }
    69  
    70  // MarshalJSON creates JSON formatted the same way as jsonpb so that the
    71  // OpenCensus service can correctly determine the underlying value type.
    72  // This custom MarshalJSON exists because,
    73  // by default *Point is JSON marshalled as:
    74  //
    75  //	{"value": {"int64Value": 1}}
    76  //
    77  // but it should be marshalled as:
    78  //
    79  //	{"int64Value": 1}
    80  func (p *Point) MarshalJSON() ([]byte, error) {
    81  	if p == nil {
    82  		return []byte("null"), nil
    83  	}
    84  
    85  	switch d := p.Value.(type) {
    86  	case PointInt64Value:
    87  		return json.Marshal(&struct {
    88  			Timestamp *Timestamp `json:"timestamp,omitempty"`
    89  			Value     int64      `json:"int64Value,omitempty"`
    90  		}{
    91  			Timestamp: p.Timestamp,
    92  			Value:     d.Int64Value,
    93  		})
    94  	case PointDoubleValue:
    95  		return json.Marshal(&struct {
    96  			Timestamp *Timestamp `json:"timestamp,omitempty"`
    97  			Value     float64    `json:"doubleValue,omitempty"`
    98  		}{
    99  			Timestamp: p.Timestamp,
   100  			Value:     d.DoubleValue,
   101  		})
   102  	case PointDistributionValue:
   103  		return json.Marshal(&struct {
   104  			Timestamp *Timestamp         `json:"timestamp,omitempty"`
   105  			Value     *DistributionValue `json:"distributionValue,omitempty"`
   106  		}{
   107  			Timestamp: p.Timestamp,
   108  			Value:     d.DistributionValue,
   109  		})
   110  	default:
   111  		return nil, fmt.Errorf("unknown point type %T", p.Value)
   112  	}
   113  }
   114  
   115  type PointDoubleValue struct {
   116  	DoubleValue float64 `json:"doubleValue,omitempty"`
   117  }
   118  
   119  type PointDistributionValue struct {
   120  	DistributionValue *DistributionValue `json:"distributionValue,omitempty"`
   121  }
   122  
   123  type PointSummaryValue struct {
   124  	SummaryValue *SummaryValue `json:"summaryValue,omitempty"`
   125  }
   126  
   127  type PointValue interface {
   128  	labelPointValue()
   129  }
   130  
   131  func (PointInt64Value) labelPointValue()        {}
   132  func (PointDoubleValue) labelPointValue()       {}
   133  func (PointDistributionValue) labelPointValue() {}
   134  func (PointSummaryValue) labelPointValue()      {}
   135  
   136  type DistributionValue struct {
   137  	Count                 int64         `json:"count,omitempty"`
   138  	Sum                   float64       `json:"sum,omitempty"`
   139  	SumOfSquaredDeviation float64       `json:"sum_of_squared_deviation,omitempty"`
   140  	BucketOptions         BucketOptions `json:"bucket_options,omitempty"`
   141  	Buckets               []*Bucket     `json:"buckets,omitempty"`
   142  }
   143  
   144  type BucketOptionsExplicit struct {
   145  	Bounds []float64 `json:"bounds,omitempty"`
   146  }
   147  
   148  type BucketOptions interface {
   149  	labelBucketOptions()
   150  }
   151  
   152  func (*BucketOptionsExplicit) labelBucketOptions() {}
   153  
   154  var _ BucketOptions = (*BucketOptionsExplicit)(nil)
   155  var _ json.Marshaler = (*BucketOptionsExplicit)(nil)
   156  
   157  // Declared for the purpose of custom JSON marshaling without cycles.
   158  type bucketOptionsExplicitAlias BucketOptionsExplicit
   159  
   160  // MarshalJSON creates JSON formatted the same way as jsonpb so that the
   161  // OpenCensus service can correctly determine the underlying value type.
   162  // This custom MarshalJSON exists because,
   163  // by default BucketOptionsExplicit is JSON marshalled as:
   164  //
   165  //	{"bounds":[1,2,3]}
   166  //
   167  // but it should be marshalled as:
   168  //
   169  //	{"explicit":{"bounds":[1,2,3]}}
   170  func (be *BucketOptionsExplicit) MarshalJSON() ([]byte, error) {
   171  	return json.Marshal(&struct {
   172  		Explicit *bucketOptionsExplicitAlias `json:"explicit,omitempty"`
   173  	}{
   174  		Explicit: (*bucketOptionsExplicitAlias)(be),
   175  	})
   176  }
   177  
   178  type Bucket struct {
   179  	Count    int64     `json:"count,omitempty"`
   180  	Exemplar *Exemplar `json:"exemplar,omitempty"`
   181  }
   182  
   183  type Exemplar struct {
   184  	Value       float64           `json:"value,omitempty"`
   185  	Timestamp   *Timestamp        `json:"timestamp,omitempty"`
   186  	Attachments map[string]string `json:"attachments,omitempty"`
   187  }
   188  
   189  type SummaryValue struct {
   190  	Count    *Int64Value  `json:"count,omitempty"`
   191  	Sum      *DoubleValue `json:"sum,omitempty"`
   192  	Snapshot *Snapshot    `json:"snapshot,omitempty"`
   193  }
   194  
   195  type Snapshot struct {
   196  	Count            *Int64Value                  `json:"count,omitempty"`
   197  	Sum              *DoubleValue                 `json:"sum,omitempty"`
   198  	PercentileValues []*SnapshotValueAtPercentile `json:"percentile_values,omitempty"`
   199  }
   200  
   201  type SnapshotValueAtPercentile struct {
   202  	Percentile float64 `json:"percentile,omitempty"`
   203  	Value      float64 `json:"value,omitempty"`
   204  }