github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/models/factory.go (about)

     1  // Copyright 2022 iLogtail Authors
     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 models
    16  
    17  import "github.com/alibaba/ilogtail/pkg/constraints"
    18  
    19  func NewTagsWithMap(tags map[string]string) Tags {
    20  	return &keyValuesImpl[string]{
    21  		keyValues: tags,
    22  	}
    23  }
    24  
    25  func NewTagsWithKeyValues(keyValues ...string) Tags {
    26  	if len(keyValues)%2 != 0 {
    27  		keyValues = keyValues[:len(keyValues)-1]
    28  	}
    29  	tags := make(map[string]string)
    30  	for i := 0; i < len(keyValues); i += 2 {
    31  		tags[keyValues[i]] = keyValues[i+1]
    32  	}
    33  	return &keyValuesImpl[string]{
    34  		keyValues: tags,
    35  	}
    36  }
    37  
    38  func NewTags() Tags {
    39  	return NewKeyValues[string]()
    40  }
    41  
    42  func NewMetadataWithMap(md map[string]string) Metadata {
    43  	return &keyValuesImpl[string]{
    44  		keyValues: md,
    45  	}
    46  }
    47  
    48  func NewMetadataWithKeyValues(keyValues ...string) Metadata {
    49  	if len(keyValues)%2 != 0 {
    50  		keyValues = keyValues[:len(keyValues)-1]
    51  	}
    52  	md := make(map[string]string)
    53  	for i := 0; i < len(keyValues); i += 2 {
    54  		md[keyValues[i]] = keyValues[i+1]
    55  	}
    56  	return &keyValuesImpl[string]{
    57  		keyValues: md,
    58  	}
    59  }
    60  
    61  func NewMetadata() Metadata {
    62  	return NewKeyValues[string]()
    63  }
    64  
    65  func NewGroup(meta Metadata, tags Tags) *GroupInfo {
    66  	return &GroupInfo{
    67  		Metadata: meta,
    68  		Tags:     tags,
    69  	}
    70  }
    71  func NewMetric(name string, metricType MetricType, tags Tags, timestamp int64, value MetricValue, typedValues MetricTypedValues) *Metric {
    72  	return &Metric{
    73  		Name:       name,
    74  		MetricType: metricType,
    75  		Timestamp:  uint64(timestamp),
    76  		Tags:       tags,
    77  		Value:      value,
    78  		TypedValue: typedValues,
    79  	}
    80  }
    81  
    82  func NewSingleValueMetric[T constraints.IntUintFloat](name string, metricType MetricType, tags Tags, timestamp int64, value T) *Metric {
    83  	return &Metric{
    84  		Name:       name,
    85  		MetricType: metricType,
    86  		Timestamp:  uint64(timestamp),
    87  		Tags:       tags,
    88  		Value:      &MetricSingleValue{Value: float64(value)},
    89  		TypedValue: NilTypedValues,
    90  	}
    91  }
    92  
    93  func NewMultiValuesMetric(name string, metricType MetricType, tags Tags, timestamp int64, values MetricFloatValues) *Metric {
    94  	return &Metric{
    95  		Name:       name,
    96  		MetricType: metricType,
    97  		Timestamp:  uint64(timestamp),
    98  		Tags:       tags,
    99  		Value:      &MetricMultiValue{Values: values},
   100  		TypedValue: NilTypedValues,
   101  	}
   102  }
   103  
   104  func NewMetricMultiValue() *MetricMultiValue {
   105  	return &MetricMultiValue{
   106  		Values: &keyValuesImpl[float64]{
   107  			keyValues: make(map[string]float64),
   108  		},
   109  	}
   110  }
   111  
   112  func NewMetricMultiValueWithMap(keyValues map[string]float64) *MetricMultiValue {
   113  	return &MetricMultiValue{
   114  		Values: &keyValuesImpl[float64]{
   115  			keyValues: keyValues,
   116  		},
   117  	}
   118  }
   119  
   120  func NewMetricTypedValues() MetricTypedValues {
   121  	return &keyValuesImpl[*TypedValue]{
   122  		keyValues: make(map[string]*TypedValue),
   123  	}
   124  }
   125  
   126  func NewMetricTypedValueWithMap(keyValues map[string]*TypedValue) MetricTypedValues {
   127  	return &keyValuesImpl[*TypedValue]{
   128  		keyValues: keyValues,
   129  	}
   130  }
   131  
   132  func NewSpan(name, traceID, spanID string, kind SpanKind, startTime, endTime uint64, tags Tags, events []*SpanEvent, links []*SpanLink) *Span {
   133  	return &Span{
   134  		Name:      name,
   135  		TraceID:   traceID,
   136  		SpanID:    spanID,
   137  		Kind:      kind,
   138  		StartTime: startTime,
   139  		EndTime:   endTime,
   140  		Tags:      tags,
   141  		Events:    events,
   142  		Links:     links,
   143  	}
   144  }
   145  
   146  func NewByteArray(bytes []byte) ByteArray {
   147  	return ByteArray(bytes)
   148  }
   149  
   150  func NewLog(name string, body []byte, level, spanID, traceID string, tags Tags, timestamp uint64) *Log {
   151  	log := &Log{
   152  		Name:      name,
   153  		Level:     level,
   154  		Tags:      tags,
   155  		Timestamp: timestamp,
   156  		SpanID:    spanID,
   157  		TraceID:   traceID,
   158  		Contents:  NewLogContents(),
   159  	}
   160  	log.SetBody(body)
   161  	return log
   162  }
   163  
   164  func NewSimpleLog(body []byte, tags Tags, timestamp uint64) *Log {
   165  	log := &Log{
   166  		Tags:      tags,
   167  		Timestamp: timestamp,
   168  		Contents:  NewLogContents(),
   169  	}
   170  	log.SetBody(body)
   171  	return log
   172  }
   173  
   174  func NewSimpleLevelLog(level string, body []byte, tags Tags, timestamp uint64) *Log {
   175  	log := &Log{
   176  		Level:     level,
   177  		Tags:      tags,
   178  		Timestamp: timestamp,
   179  		Contents:  NewLogContents(),
   180  	}
   181  	log.SetBody(body)
   182  	return log
   183  }
   184  
   185  func NewLogContents() LogContents {
   186  	return NewKeyValues[interface{}]()
   187  }