github.com/matrixorigin/matrixone@v0.7.0/pkg/util/export/observability/logs/log.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 logs
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"sync"
    21  	"time"
    22  	"unsafe"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/util/export/observability"
    25  	"github.com/matrixorigin/matrixone/pkg/util/export/table"
    26  )
    27  
    28  type LogRecord struct {
    29  	TraceId     string
    30  	SpanId      string
    31  	Timestamp   time.Time
    32  	CollectTime time.Time
    33  	LoggerName  string
    34  	Level       string
    35  	Caller      string
    36  	Message     string
    37  	Stack       string
    38  	// Labels store other elems and kubernetes elems
    39  	Labels map[string]any // json
    40  }
    41  
    42  var logPool = &sync.Pool{New: func() any {
    43  	return &LogRecord{
    44  		Labels: make(map[string]any),
    45  	}
    46  }}
    47  
    48  func NewLogRecord() *LogRecord {
    49  	return logPool.Get().(*LogRecord)
    50  }
    51  
    52  func (*LogRecord) GetName() string {
    53  	return observability.LogsTable.GetIdentify()
    54  }
    55  
    56  func (*LogRecord) GetTable() *table.Table {
    57  	return observability.LogsTable
    58  }
    59  
    60  func (l *LogRecord) FillRow(ctx context.Context, row *table.Row) {
    61  	row.Reset()
    62  	row.SetColumnVal(observability.LogsTraceIDCol, l.TraceId)
    63  	row.SetColumnVal(observability.LogsSpanIDCol, l.SpanId)
    64  	row.SetColumnVal(observability.LogsTimestampCol, l.Timestamp)
    65  	row.SetColumnVal(observability.LogsCollectTimeCol, l.CollectTime)
    66  	row.SetColumnVal(observability.LogsLoggerNameCol, l.LoggerName)
    67  	row.SetColumnVal(observability.LogsLevelCol, l.Level)
    68  	row.SetColumnVal(observability.LogsCallerCol, l.Caller)
    69  	row.SetColumnVal(observability.LogsMessageCol, l.Message)
    70  	row.SetColumnVal(observability.LogsStackCol, l.Stack)
    71  
    72  	labels, err := json.Marshal(&l.Labels)
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  	row.SetColumnVal(observability.LogsLabelsCol, string(labels))
    77  }
    78  
    79  func (l *LogRecord) Size() int64 {
    80  	return int64(unsafe.Sizeof(l)) + int64(
    81  		len(l.TraceId)+len(l.SpanId)+len(l.LoggerName)+len(l.Level)+
    82  			len(l.Caller)+len(l.Message)+len(l.Stack),
    83  	)
    84  }
    85  
    86  func (l *LogRecord) Free() {
    87  	l.TraceId = ""
    88  	l.SpanId = ""
    89  	l.Timestamp = time.Time{}
    90  	l.CollectTime = time.Time{}
    91  	l.LoggerName = ""
    92  	l.Level = ""
    93  	l.Message = ""
    94  	l.Stack = ""
    95  	l.Labels = nil
    96  	logPool.Put(l)
    97  }