From 9a2c51bcefa9c53fd0ebab4537afccaee372138f Mon Sep 17 00:00:00 2001 From: zzzk1 Date: Mon, 30 Dec 2024 21:09:35 +0800 Subject: [PATCH] remove to dbmodel package --- exporter/logzioexporter/from_domain.go | 139 ++++-------------- exporter/logzioexporter/from_domain_test.go | 18 ++- .../logzioexporter/internal/dbmodel/model.go | 90 ++++++++++++ exporter/logzioexporter/logziospan.go | 60 ++++---- 4 files changed, 159 insertions(+), 148 deletions(-) create mode 100644 exporter/logzioexporter/internal/dbmodel/model.go diff --git a/exporter/logzioexporter/from_domain.go b/exporter/logzioexporter/from_domain.go index c5f5bae125d1..8f5955cb049c 100644 --- a/exporter/logzioexporter/from_domain.go +++ b/exporter/logzioexporter/from_domain.go @@ -8,93 +8,10 @@ import ( "strings" "github.com/jaegertracing/jaeger/model" -) - -// ReferenceType is the reference type of one span to another -type ReferenceType string - -// TraceID is the shared trace ID of all spans in the trace. -type TraceID string - -// SpanID is the id of a span -type SpanID string - -// ValueType is the type of a value stored in keyValue struct. -type ValueType string -const ( - // ChildOf means a span is the child of another span - ChildOf ReferenceType = "CHILD_OF" - // FollowsFrom means a span follows from another span - FollowsFrom ReferenceType = "FOLLOWS_FROM" - - // StringType indicates a string value stored in keyValue - StringType ValueType = "string" - // BoolType indicates a Boolean value stored in keyValue - BoolType ValueType = "bool" - // Int64Type indicates a 64bit signed integer value stored in keyValue - Int64Type ValueType = "int64" - // Float64Type indicates a 64bit float value stored in keyValue - Float64Type ValueType = "float64" - // BinaryType indicates an arbitrary byte array stored in keyValue - BinaryType ValueType = "binary" + "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter/internal/dbmodel" ) -// reference is a reference from one span to another -type reference struct { - RefType ReferenceType `json:"refType"` - TraceID TraceID `json:"traceID"` - SpanID SpanID `json:"spanID"` -} - -// process is the process emitting a set of spans -type process struct { - ServiceName string `json:"serviceName"` - Tags []keyValue `json:"tags"` - // Alternative representation of tags for better kibana support - Tag map[string]any `json:"tag,omitempty"` -} - -// spanLog is a log emitted in a span -type spanLog struct { - Timestamp uint64 `json:"timestamp"` - Fields []keyValue `json:"fields"` -} - -// keyValue is a key-value pair with typed value. -type keyValue struct { - Key string `json:"key"` - Type ValueType `json:"type,omitempty"` - Value any `json:"value"` -} - -// service is the JSON struct for service:operation documents in ElasticSearch -type service struct { - ServiceName string `json:"serviceName"` - OperationName string `json:"operationName"` -} - -// only for testing span is ES database representation of the domain span. -type span struct { - TraceID TraceID `json:"traceID"` - SpanID SpanID `json:"spanID"` - ParentSpanID SpanID `json:"parentSpanID,omitempty"` // deprecated - Flags uint32 `json:"flags,omitempty"` - OperationName string `json:"operationName"` - References []reference `json:"references"` - StartTime uint64 `json:"startTime"` // microseconds since Unix epoch - // ElasticSearch does not support a UNIX Epoch timestamp in microseconds, - // so Jaeger maps StartTime to a 'long' type. This extra StartTimeMillis field - // works around this issue, enabling timerange queries. - StartTimeMillis uint64 `json:"startTimeMillis"` - Duration uint64 `json:"duration"` // microseconds - Tags []keyValue `json:"tags"` - // Alternative representation of tags for better kibana support - Tag map[string]any `json:"tag,omitempty"` - Logs []spanLog `json:"logs"` - Process process `json:"process,omitempty"` -} - // newFromDomain creates fromDomain used to convert model span to db span func newFromDomain(allTagsAsObject bool, tagKeysAsFields []string, tagDotReplacement string) fromDomain { tags := map[string]bool{} @@ -120,8 +37,8 @@ func (fd fromDomain) fromDomainEmbedProcess(span *model.Span) *logzioSpan { func (fd fromDomain) convertSpanInternal(span *model.Span) logzioSpan { tags, tagsMap := fd.convertKeyValuesString(span.Tags) return logzioSpan{ - TraceID: TraceID(span.TraceID.String()), - SpanID: SpanID(span.SpanID.String()), + TraceID: dbmodel.TraceID(span.TraceID.String()), + SpanID: dbmodel.SpanID(span.SpanID.String()), Flags: uint32(span.Flags), OperationName: span.OperationName, StartTime: model.TimeAsEpochMicroseconds(span.StartTime), @@ -140,28 +57,28 @@ func (fd fromDomain) convertSpanEmbedProcess(span *model.Span) *logzioSpan { return &s } -func (fd fromDomain) convertReferences(span *model.Span) []reference { - out := make([]reference, 0, len(span.References)) +func (fd fromDomain) convertReferences(span *model.Span) []dbmodel.Reference { + out := make([]dbmodel.Reference, 0, len(span.References)) for _, ref := range span.References { - out = append(out, reference{ + out = append(out, dbmodel.Reference{ RefType: fd.convertRefType(ref.RefType), - TraceID: TraceID(ref.TraceID.String()), - SpanID: SpanID(ref.SpanID.String()), + TraceID: dbmodel.TraceID(ref.TraceID.String()), + SpanID: dbmodel.SpanID(ref.SpanID.String()), }) } return out } -func (fromDomain) convertRefType(refType model.SpanRefType) ReferenceType { +func (fromDomain) convertRefType(refType model.SpanRefType) dbmodel.ReferenceType { if refType == model.FollowsFrom { - return FollowsFrom + return dbmodel.FollowsFrom } - return ChildOf + return dbmodel.ChildOf } -func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]keyValue, map[string]any) { +func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]dbmodel.KeyValue, map[string]any) { var tagsMap map[string]any - var kvs []keyValue + var kvs []dbmodel.KeyValue for _, kv := range keyValues { if kv.GetVType() != model.BinaryType && (fd.allTagsAsFields || fd.tagKeysAsFields[kv.Key]) { if tagsMap == nil { @@ -173,39 +90,39 @@ func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]keyVal } } if kvs == nil { - kvs = make([]keyValue, 0) + kvs = make([]dbmodel.KeyValue, 0) } return kvs, tagsMap } -func (fromDomain) convertLogs(logs []model.Log) []spanLog { - out := make([]spanLog, len(logs)) - for i, l := range logs { - var kvs []keyValue - for _, kv := range l.Fields { +func (fromDomain) convertLogs(logs []model.Log) []dbmodel.Log { + out := make([]dbmodel.Log, len(logs)) + for i, log := range logs { + var kvs []dbmodel.KeyValue + for _, kv := range log.Fields { kvs = append(kvs, convertKeyValue(kv)) } - out[i] = spanLog{ - Timestamp: model.TimeAsEpochMicroseconds(l.Timestamp), + out[i] = dbmodel.Log{ + Timestamp: model.TimeAsEpochMicroseconds(log.Timestamp), Fields: kvs, } } return out } -func (fd fromDomain) convertProcess(p *model.Process) process { - tags, tagsMap := fd.convertKeyValuesString(p.Tags) - return process{ - ServiceName: p.ServiceName, +func (fd fromDomain) convertProcess(process *model.Process) dbmodel.Process { + tags, tagsMap := fd.convertKeyValuesString(process.Tags) + return dbmodel.Process{ + ServiceName: process.ServiceName, Tags: tags, Tag: tagsMap, } } -func convertKeyValue(kv model.KeyValue) keyValue { - return keyValue{ +func convertKeyValue(kv model.KeyValue) dbmodel.KeyValue { + return dbmodel.KeyValue{ Key: kv.Key, - Type: ValueType(strings.ToLower(kv.VType.String())), + Type: dbmodel.ValueType(strings.ToLower(kv.VType.String())), Value: kv.AsString(), } } diff --git a/exporter/logzioexporter/from_domain_test.go b/exporter/logzioexporter/from_domain_test.go index 40510fb565fb..d0b41e358471 100644 --- a/exporter/logzioexporter/from_domain_test.go +++ b/exporter/logzioexporter/from_domain_test.go @@ -17,6 +17,8 @@ import ( "github.com/stretchr/testify/require" "github.com/jaegertracing/jaeger/model" + + "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter/internal/dbmodel" ) func TestFromDomainEmbedProcess(t *testing.T) { @@ -98,35 +100,35 @@ func TestConvertKeyValueValue(t *testing.T) { key := "key" tests := []struct { kv model.KeyValue - expected keyValue + expected dbmodel.KeyValue }{ { kv: model.Bool(key, true), - expected: keyValue{Key: key, Value: "true", Type: "bool"}, + expected: dbmodel.KeyValue{Key: key, Value: "true", Type: "bool"}, }, { kv: model.Bool(key, false), - expected: keyValue{Key: key, Value: "false", Type: "bool"}, + expected: dbmodel.KeyValue{Key: key, Value: "false", Type: "bool"}, }, { kv: model.Int64(key, int64(1499)), - expected: keyValue{Key: key, Value: "1499", Type: "int64"}, + expected: dbmodel.KeyValue{Key: key, Value: "1499", Type: "int64"}, }, { kv: model.Float64(key, float64(15.66)), - expected: keyValue{Key: key, Value: "15.66", Type: "float64"}, + expected: dbmodel.KeyValue{Key: key, Value: "15.66", Type: "float64"}, }, { kv: model.String(key, longString), - expected: keyValue{Key: key, Value: longString, Type: "string"}, + expected: dbmodel.KeyValue{Key: key, Value: longString, Type: "string"}, }, { kv: model.Binary(key, []byte(longString)), - expected: keyValue{Key: key, Value: hex.EncodeToString([]byte(longString)), Type: "binary"}, + expected: dbmodel.KeyValue{Key: key, Value: hex.EncodeToString([]byte(longString)), Type: "binary"}, }, { kv: model.KeyValue{VType: 1500, Key: key}, - expected: keyValue{Key: key, Value: "unknown type 1500", Type: "1500"}, + expected: dbmodel.KeyValue{Key: key, Value: "unknown type 1500", Type: "1500"}, }, } diff --git a/exporter/logzioexporter/internal/dbmodel/model.go b/exporter/logzioexporter/internal/dbmodel/model.go new file mode 100644 index 000000000000..d849c4885733 --- /dev/null +++ b/exporter/logzioexporter/internal/dbmodel/model.go @@ -0,0 +1,90 @@ +// Copyright (c) 2019 The Jaeger Authors. +// Copyright (c) 2018 Uber Technologies, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package dbmodel + +// ReferenceType is the reference type of one span to another +type ReferenceType string + +// TraceID is the shared trace ID of all spans in the trace. +type TraceID string + +// SpanID is the id of a span +type SpanID string + +// ValueType is the type of a value stored in KeyValue struct. +type ValueType string + +const ( + // ChildOf means a span is the child of another span + ChildOf ReferenceType = "CHILD_OF" + // FollowsFrom means a span follows from another span + FollowsFrom ReferenceType = "FOLLOWS_FROM" + + // StringType indicates a string value stored in KeyValue + StringType ValueType = "string" + // BoolType indicates a Boolean value stored in KeyValue + BoolType ValueType = "bool" + // Int64Type indicates a 64bit signed integer value stored in KeyValue + Int64Type ValueType = "int64" + // Float64Type indicates a 64bit float value stored in KeyValue + Float64Type ValueType = "float64" + // BinaryType indicates an arbitrary byte array stored in KeyValue + BinaryType ValueType = "binary" +) + +// Span is ES database representation of the domain span. +type Span struct { + TraceID TraceID `json:"traceID"` + SpanID SpanID `json:"spanID"` + ParentSpanID SpanID `json:"parentSpanID,omitempty"` // deprecated + Flags uint32 `json:"flags,omitempty"` + OperationName string `json:"operationName"` + References []Reference `json:"references"` + StartTime uint64 `json:"startTime"` // microseconds since Unix epoch + // ElasticSearch does not support a UNIX Epoch timestamp in microseconds, + // so Jaeger maps StartTime to a 'long' type. This extra StartTimeMillis field + // works around this issue, enabling timerange queries. + StartTimeMillis uint64 `json:"startTimeMillis"` + Duration uint64 `json:"duration"` // microseconds + Tags []KeyValue `json:"tags"` + // Alternative representation of tags for better kibana support + Tag map[string]any `json:"tag,omitempty"` + Logs []Log `json:"logs"` + Process Process `json:"process,omitempty"` +} + +// Reference is a reference from one span to another +type Reference struct { + RefType ReferenceType `json:"refType"` + TraceID TraceID `json:"traceID"` + SpanID SpanID `json:"spanID"` +} + +// Process is the process emitting a set of spans +type Process struct { + ServiceName string `json:"serviceName"` + Tags []KeyValue `json:"tags"` + // Alternative representation of tags for better kibana support + Tag map[string]any `json:"tag,omitempty"` +} + +// Log is a log emitted in a span +type Log struct { + Timestamp uint64 `json:"timestamp"` + Fields []KeyValue `json:"fields"` +} + +// KeyValue is a key-value pair with typed value. +type KeyValue struct { + Key string `json:"key"` + Type ValueType `json:"type,omitempty"` + Value any `json:"value"` +} + +// Service is the JSON struct for service:operation documents in ElasticSearch +type Service struct { + ServiceName string `json:"serviceName"` + OperationName string `json:"operationName"` +} diff --git a/exporter/logzioexporter/logziospan.go b/exporter/logzioexporter/logziospan.go index 78c87d290505..2c2512c2811e 100644 --- a/exporter/logzioexporter/logziospan.go +++ b/exporter/logzioexporter/logziospan.go @@ -7,6 +7,8 @@ import ( "encoding/json" "github.com/jaegertracing/jaeger/model" + + "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter/internal/dbmodel" ) const ( @@ -17,20 +19,20 @@ const ( // logzioSpan is same as esSpan with a few different json field names and an addition on type field. type logzioSpan struct { - TraceID TraceID `json:"traceID"` - SpanID SpanID `json:"spanID"` - OperationName string `json:"operationName,omitempty"` - References []reference `json:"references"` - Flags uint32 `json:"flags,omitempty"` - StartTime uint64 `json:"startTime"` - StartTimeMillis uint64 `json:"startTimeMillis"` - Timestamp uint64 `json:"@timestamp"` - Duration uint64 `json:"duration"` - Tags []keyValue `json:"JaegerTags,omitempty"` - Tag map[string]any `json:"JaegerTag,omitempty"` - Logs []spanLog `json:"logs"` - Process process `json:"process,omitempty"` - Type string `json:"type"` + TraceID dbmodel.TraceID `json:"traceID"` + OperationName string `json:"operationName,omitempty"` + SpanID dbmodel.SpanID `json:"spanID"` + References []dbmodel.Reference `json:"references"` + Flags uint32 `json:"flags,omitempty"` + StartTime uint64 `json:"startTime"` + StartTimeMillis uint64 `json:"startTimeMillis"` + Timestamp uint64 `json:"@timestamp"` + Duration uint64 `json:"duration"` + Tags []dbmodel.KeyValue `json:"JaegerTags,omitempty"` + Tag map[string]any `json:"JaegerTag,omitempty"` + Logs []dbmodel.Log `json:"logs"` + Process dbmodel.Process `json:"process,omitempty"` + Type string `json:"type"` } func getTagsValues(tags []model.KeyValue) []string { @@ -65,20 +67,20 @@ func transformToLogzioSpanBytes(span *model.Span) ([]byte, error) { return json.Marshal(newSpan) } -// only for testing transformToDbModelSpan coverts logz.io span to ElasticSearch span -func (logziospan *logzioSpan) transformToDbModelSpan() *span { - return &span{ - OperationName: logziospan.OperationName, - Process: logziospan.Process, - Tags: logziospan.Tags, - Tag: logziospan.Tag, - References: logziospan.References, - Logs: logziospan.Logs, - Duration: logziospan.Duration, - StartTimeMillis: logziospan.StartTimeMillis, - StartTime: logziospan.StartTime, - Flags: logziospan.Flags, - SpanID: logziospan.SpanID, - TraceID: logziospan.TraceID, +// transformToDbModelSpan coverts logz.io span to ElasticSearch span +func (span *logzioSpan) transformToDbModelSpan() *dbmodel.Span { + return &dbmodel.Span{ + OperationName: span.OperationName, + Process: span.Process, + Tags: span.Tags, + Tag: span.Tag, + References: span.References, + Logs: span.Logs, + Duration: span.Duration, + StartTimeMillis: span.StartTimeMillis, + StartTime: span.StartTime, + Flags: span.Flags, + SpanID: span.SpanID, + TraceID: span.TraceID, } }