Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: suppprt log event model #647

Merged
merged 13 commits into from
Mar 8, 2023
6 changes: 5 additions & 1 deletion pkg/models/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (ByteArray) SetName(name string) {
}

func (ByteArray) GetTags() Tags {
return noopStringValues
return NilStringValues
}

func (ByteArray) GetType() EventType {
Expand All @@ -45,3 +45,7 @@ func (ByteArray) GetObservedTimestamp() uint64 {

func (ByteArray) SetObservedTimestamp(timestamp uint64) {
}

func (b ByteArray) Clone() PipelineEvent {
return b
}
48 changes: 30 additions & 18 deletions pkg/models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const (
)

var (
noopStringValues = &keyValuesNoop[string]{}
noopTypedValues = &keyValuesNoop[*TypedValue]{}
noopFloatValues = &keyValuesNoop[float64]{}
NilStringValues = &keyValuesNil[string]{}
NilTypedValues = &keyValuesNil[*TypedValue]{}
NilFloatValues = &keyValuesNil[float64]{}
)

type TypedValue struct {
Expand All @@ -61,15 +61,13 @@ type KeyValues[TValue string | float64 | *TypedValue] interface {
Iterator() map[string]TValue

Len() int
}

type Tags interface {
KeyValues[string]
IsNil() bool
}

type Metadata interface {
KeyValues[string]
}
type Tags KeyValues[string]

type Metadata KeyValues[string]

type keyValuesImpl[TValue string | float64 | *TypedValue] struct {
keyValues map[string]TValue
Expand Down Expand Up @@ -140,34 +138,48 @@ func (kv *keyValuesImpl[TValue]) Len() int {
return 0
}

type keyValuesNoop[TValue string | float64 | *TypedValue] struct {
func (kv *keyValuesImpl[TValue]) IsNil() bool {
return false
}

type keyValuesNil[TValue string | float64 | *TypedValue] struct {
}

func (kv *keyValuesNoop[TValue]) Add(key string, value TValue) {
func (kv *keyValuesNil[TValue]) Add(key string, value TValue) {
}

func (kv *keyValuesNoop[TValue]) AddAll(items map[string]TValue) {
func (kv *keyValuesNil[TValue]) AddAll(items map[string]TValue) {
}

func (kv *keyValuesNoop[TValue]) Get(key string) TValue {
func (kv *keyValuesNil[TValue]) Get(key string) TValue {
var null TValue
return null
}

func (kv *keyValuesNoop[TValue]) Contains(key string) bool {
func (kv *keyValuesNil[TValue]) Contains(key string) bool {
return false
}

func (kv *keyValuesNoop[TValue]) Delete(key string) {
func (kv *keyValuesNil[TValue]) Delete(key string) {
}

func (kv *keyValuesNoop[TValue]) Merge(other KeyValues[TValue]) {
func (kv *keyValuesNil[TValue]) Merge(other KeyValues[TValue]) {
}

func (kv *keyValuesNoop[TValue]) Iterator() map[string]TValue {
func (kv *keyValuesNil[TValue]) Iterator() map[string]TValue {
return make(map[string]TValue)
}

func (kv *keyValuesNoop[TValue]) Len() int {
func (kv *keyValuesNil[TValue]) Len() int {
return 0
}

func (kv *keyValuesNil[TValue]) IsNil() bool {
return true
}

func NewKeyValues[TValue string | float64 | *TypedValue]() KeyValues[TValue] {
return &keyValuesImpl[TValue]{
keyValues: make(map[string]TValue),
}
}
55 changes: 42 additions & 13 deletions pkg/models/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,30 @@ func NewTagsWithKeyValues(keyValues ...string) Tags {
}

func NewTags() Tags {
return &keyValuesImpl[string]{
keyValues: make(map[string]string),
}
return NewKeyValues[string]()
}

func NewMetadataWithMap(tags map[string]string) Metadata {
func NewMetadataWithMap(md map[string]string) Metadata {
return &keyValuesImpl[string]{
keyValues: tags,
keyValues: md,
}
}

func NewMetadataWithKeyValues(keyValues ...string) Metadata {
if len(keyValues)%2 != 0 {
keyValues = keyValues[:len(keyValues)-1]
}
tags := make(map[string]string)
md := make(map[string]string)
for i := 0; i < len(keyValues); i += 2 {
tags[keyValues[i]] = keyValues[i+1]
md[keyValues[i]] = keyValues[i+1]
}
return &keyValuesImpl[string]{
keyValues: tags,
keyValues: md,
}
}

func NewMetadata() Metadata {
return &keyValuesImpl[string]{
keyValues: make(map[string]string),
}
return NewKeyValues[string]()
}

func NewGroup(meta Metadata, tags Tags) *GroupInfo {
Expand Down Expand Up @@ -91,7 +87,7 @@ func NewSingleValueMetric[T constraints.IntUintFloat](name string, metricType Me
Timestamp: uint64(timestamp),
Tags: tags,
Value: &MetricSingleValue{Value: float64(value)},
TypedValue: noopTypedValues,
TypedValue: NilTypedValues,
}
}

Expand All @@ -102,7 +98,7 @@ func NewMultiValuesMetric(name string, metricType MetricType, tags Tags, timesta
Timestamp: uint64(timestamp),
Tags: tags,
Value: &MetricMultiValue{Values: values},
TypedValue: noopTypedValues,
TypedValue: NilTypedValues,
}
}

Expand Down Expand Up @@ -145,3 +141,36 @@ func NewSpan(name, traceID, spanID string, kind SpanKind, startTime, endTime uin
func NewByteArray(bytes []byte) ByteArray {
return ByteArray(bytes)
}

func NewLog(name, body, level, spanID, traceID string, tags Tags, timestamp uint64) *Log {
return &Log{
Name: name,
Level: level,
Body: body,
Tags: tags,
Timestamp: timestamp,
SpanID: spanID,
TraceID: traceID,
}
}

func NewSimpleLog(body string, tags Tags, timestamp uint64) *Log {
return &Log{
Body: body,
Tags: tags,
Timestamp: timestamp,
}
}

func NewSimpleLevelLog(level, body string, tags Tags, timestamp uint64) *Log {
return &Log{
Level: level,
Body: body,
Tags: tags,
Timestamp: timestamp,
}
}

func NewLogIndices() Indices {
return NewKeyValues[string]()
}
151 changes: 151 additions & 0 deletions pkg/models/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package models

import "github.com/alibaba/ilogtail/pkg/util"

type Indices KeyValues[string]

type Log struct {
liuhaoyang marked this conversation as resolved.
Show resolved Hide resolved
Name string
Level string
SpanID string
TraceID string
Tags Tags
liuhaoyang marked this conversation as resolved.
Show resolved Hide resolved
Timestamp uint64
ObservedTimestamp uint64
Body string
Indices Indices
}

func (m *Log) GetName() string {
if m != nil {
return m.Name
}
return ""
}

func (m *Log) SetName(name string) {
if m != nil {
m.Name = name
}
}

func (m *Log) GetTags() Tags {
if m != nil {
return m.Tags
}
return NilStringValues
}

func (m *Log) GetType() EventType {
return EventTypeLogging
}

func (m *Log) GetTimestamp() uint64 {
if m != nil {
return m.Timestamp
}
return 0
}

func (m *Log) GetObservedTimestamp() uint64 {
if m != nil {
return m.ObservedTimestamp
}
return 0
}

func (m *Log) SetObservedTimestamp(observedTimestamp uint64) {
if m != nil {
m.ObservedTimestamp = observedTimestamp
}
}

func (m *Log) GetLevel() string {
if m != nil {
return m.Level
}
return ""
}

func (m *Log) SetLevel(level string) {
if m != nil {
m.Level = level
}
}

func (m *Log) GetSpanID() string {
if m != nil {
return m.SpanID
}
return ""
}

func (m *Log) SetSpanID(spanID string) {
if m != nil {
m.SpanID = spanID
}
}

func (m *Log) GetTraceID() string {
if m != nil {
return m.TraceID
}
return ""
}

func (m *Log) SetTraceID(traceID string) {
if m != nil {
m.TraceID = traceID
}
}

func (m *Log) GetBody() string {
if m != nil {
return m.Body
}
return ""
}

func (m *Log) SetBody(body string) {
if m != nil {
m.Body = body
}
}

func (m *Log) GetBytesBody() []byte {
return util.ZeroCopyStringToBytes(m.GetBody())
}

func (m *Log) SetBytesBody(body []byte) {
m.SetBody(util.ZeroCopyBytesToString(body))
}

func (m *Log) SetIndices(indices Indices) {
if m != nil {
m.Indices = indices
}
}

func (m *Log) GetIndices() Indices {
if m != nil {
return m.Indices
}
return NilStringValues
}

func (m *Log) Clone() PipelineEvent {
if m != nil {
return &Log{
Name: m.Name,
Level: m.Level,
SpanID: m.SpanID,
TraceID: m.TraceID,
Tags: m.Tags,
Timestamp: m.Timestamp,
ObservedTimestamp: m.ObservedTimestamp,
Body: m.Body,
Indices: m.Indices,
}
}
return nil
}
Loading