github.com/matrixorigin/matrixone@v0.7.0/pkg/util/export/observability/metrics/metric.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metrics
    16  
    17  import (
    18  	"context"
    19  	"crypto/md5"
    20  	"encoding/hex"
    21  	"encoding/json"
    22  	"github.com/matrixorigin/matrixone/pkg/util/export/observability"
    23  	"sync"
    24  	"time"
    25  	"unsafe"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/util/export/table"
    28  
    29  	"github.com/prometheus/common/model"
    30  )
    31  
    32  type Metric struct {
    33  	Name      string
    34  	Timestamp time.Time
    35  	Value     float64
    36  	Labels    model.Metric
    37  	SeriesId  string
    38  }
    39  
    40  var metricPool = &sync.Pool{New: func() any {
    41  	return &Metric{}
    42  }}
    43  
    44  func NewMetric() *Metric {
    45  	return metricPool.Get().(*Metric)
    46  }
    47  
    48  func (m *Metric) GetName() string {
    49  	return observability.MetricTable.GetIdentify()
    50  }
    51  
    52  func (m *Metric) GetRow() *table.Table {
    53  	return observability.MetricTable
    54  }
    55  
    56  func (m *Metric) FillRow(ctx context.Context, row *table.Row) {
    57  	row.Reset()
    58  	row.SetColumnVal(observability.MetricNameColumn, m.Name)
    59  	row.SetColumnVal(observability.MetricTimestampColumn, m.Timestamp)
    60  	row.SetColumnVal(observability.MetricValueColumn, m.Value)
    61  
    62  	labels, err := json.Marshal(&m.Labels)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	row.SetColumnVal(observability.MetricLabelsColumn, string(labels))
    67  	// calculate md5
    68  	hash := md5.New()
    69  	if _, err := hash.Write(table.String2Bytes(m.Name)); err != nil {
    70  		panic(err)
    71  	}
    72  	if _, err := hash.Write(labels); err != nil {
    73  		panic(err)
    74  	}
    75  	hashed := hash.Sum(nil)
    76  	m.SeriesId = hex.EncodeToString(hashed)
    77  	row.SetColumnVal(observability.MetricSeriesIDColumn, m.SeriesId)
    78  }
    79  
    80  func (m *Metric) Size() int64 {
    81  	return int64(unsafe.Sizeof(m)) + int64(
    82  		len(m.Name)+len(m.SeriesId),
    83  	)
    84  }
    85  
    86  func (m *Metric) Free() {
    87  	m.Name = ""
    88  	m.Timestamp = time.Time{}
    89  	m.Value = 0.0
    90  	m.Labels = nil
    91  	m.SeriesId = ""
    92  	metricPool.Put(m)
    93  }