Skip to content

Commit

Permalink
remove to dbmodel package
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzk1 committed Dec 30, 2024
1 parent c575410 commit 9a2c51b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 148 deletions.
139 changes: 28 additions & 111 deletions exporter/logzioexporter/from_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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(),
}
}
18 changes: 10 additions & 8 deletions exporter/logzioexporter/from_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"},
},
}

Expand Down
90 changes: 90 additions & 0 deletions exporter/logzioexporter/internal/dbmodel/model.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Loading

0 comments on commit 9a2c51b

Please sign in to comment.