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

[exporter] Remove jaeger dbmodel dependency #36972

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions exporter/logzioexporter/dbmodel.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 logzioexporter

// 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 {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
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"`
}
126 changes: 126 additions & 0 deletions exporter/logzioexporter/from_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package logzioexporter

import (
"strings"

"github.com/jaegertracing/jaeger/model"
)

// NewFromDomain creates FromDomain used to convert model span to db span
func NewFromDomain(allTagsAsObject bool, tagKeysAsFields []string, tagDotReplacement string) FromDomain {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
tags := map[string]bool{}
for _, k := range tagKeysAsFields {
tags[k] = true
}
return FromDomain{allTagsAsFields: allTagsAsObject, tagKeysAsFields: tags, tagDotReplacement: tagDotReplacement}
}

// FromDomain is used to convert model span to db span
type FromDomain struct {
allTagsAsFields bool
tagKeysAsFields map[string]bool
tagDotReplacement string
}

// FromDomainEmbedProcess converts model.Span into json.Span format.
// This format includes a ParentSpanID and an embedded Process.
func (fd FromDomain) FromDomainEmbedProcess(span *model.Span) *Span {
return fd.convertSpanEmbedProcess(span)
}

func (fd FromDomain) convertSpanInternal(span *model.Span) Span {
tags, tagsMap := fd.convertKeyValuesString(span.Tags)
return Span{
TraceID: TraceID(span.TraceID.String()),
SpanID: SpanID(span.SpanID.String()),
Flags: uint32(span.Flags),
OperationName: span.OperationName,
StartTime: model.TimeAsEpochMicroseconds(span.StartTime),
StartTimeMillis: model.TimeAsEpochMicroseconds(span.StartTime) / 1000,
Duration: model.DurationAsMicroseconds(span.Duration),
Tags: tags,
Tag: tagsMap,
Logs: fd.convertLogs(span.Logs),
}
}

func (fd FromDomain) convertSpanEmbedProcess(span *model.Span) *Span {
s := fd.convertSpanInternal(span)
s.Process = fd.convertProcess(span.Process)
s.References = fd.convertReferences(span)
return &s
}

func (fd FromDomain) convertReferences(span *model.Span) []Reference {
out := make([]Reference, 0, len(span.References))
for _, ref := range span.References {
out = append(out, Reference{
RefType: fd.convertRefType(ref.RefType),
TraceID: TraceID(ref.TraceID.String()),
SpanID: SpanID(ref.SpanID.String()),
})
}
return out
}

func (FromDomain) convertRefType(refType model.SpanRefType) ReferenceType {
if refType == model.FollowsFrom {
return FollowsFrom
}
return ChildOf
}

func (fd FromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]KeyValue, map[string]any) {
var tagsMap map[string]any
var kvs []KeyValue
for _, kv := range keyValues {
if kv.GetVType() != model.BinaryType && (fd.allTagsAsFields || fd.tagKeysAsFields[kv.Key]) {
if tagsMap == nil {
tagsMap = map[string]any{}
}
tagsMap[strings.ReplaceAll(kv.Key, ".", fd.tagDotReplacement)] = kv.Value()
} else {
kvs = append(kvs, convertKeyValue(kv))
}
}
if kvs == nil {
kvs = make([]KeyValue, 0)
}
return kvs, tagsMap
}

func (FromDomain) convertLogs(logs []model.Log) []Log {
out := make([]Log, len(logs))
for i, log := range logs {
var kvs []KeyValue
for _, kv := range log.Fields {
kvs = append(kvs, convertKeyValue(kv))
}
out[i] = Log{
Timestamp: model.TimeAsEpochMicroseconds(log.Timestamp),
Fields: kvs,
}
}
return out
}

func (fd FromDomain) convertProcess(process *model.Process) Process {
tags, tagsMap := fd.convertKeyValuesString(process.Tags)
return Process{
ServiceName: process.ServiceName,
Tags: tags,
Tag: tagsMap,
}
}

func convertKeyValue(kv model.KeyValue) KeyValue {
return KeyValue{
Key: kv.Key,
Type: ValueType(strings.ToLower(kv.VType.String())),
Value: kv.AsString(),
}
}
35 changes: 17 additions & 18 deletions exporter/logzioexporter/logziospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this file to internal/dbmodel as well


"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/plugin/storage/es/spanstore/dbmodel"
)

const (
Expand All @@ -18,20 +17,20 @@ const (

// logzioSpan is same as esSpan with a few different json field names and an addition on type field.
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
type logzioSpan struct {
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"`
TraceID TraceID `json:"traceID"`
OperationName string `json:"operationName,omitempty"`
SpanID SpanID `json:"spanID"`
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 []Log `json:"logs"`
Process Process `json:"process,omitempty"`
Type string `json:"type"`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still unnecessary changes to the type - why change the field order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, since you need to reference moved types like TraceID, Process, etc., you can define them in internal/dbmodel package, where they can remain public but not usable outside of this exporter, and the code referring to them will not need to change, just the import.


func getTagsValues(tags []model.KeyValue) []string {
Expand All @@ -45,7 +44,7 @@ func getTagsValues(tags []model.KeyValue) []string {
// transformToLogzioSpanBytes receives a Jaeger span, converts it to logzio span and returns it as a byte array.
// The main differences between Jaeger span and logzio span are arrays which are represented as maps
func transformToLogzioSpanBytes(span *model.Span) ([]byte, error) {
spanConverter := dbmodel.NewFromDomain(true, getTagsValues(span.Tags), tagDotReplacementCharacter)
spanConverter := NewFromDomain(true, getTagsValues(span.Tags), tagDotReplacementCharacter)
jsonSpan := spanConverter.FromDomainEmbedProcess(span)
newSpan := logzioSpan{
TraceID: jsonSpan.TraceID,
Expand All @@ -67,8 +66,8 @@ func transformToLogzioSpanBytes(span *model.Span) ([]byte, error) {
}

// transformToDbModelSpan coverts logz.io span to ElasticSearch span
func (span *logzioSpan) transformToDbModelSpan() *dbmodel.Span {
return &dbmodel.Span{
func (span *logzioSpan) transformToDbModelSpan() *Span {
return &Span{
OperationName: span.OperationName,
Process: span.Process,
Tags: span.Tags,
Expand Down
Loading