Skip to content

Commit

Permalink
remove to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzk1 committed Jan 1, 2025
1 parent b71bb84 commit fede1cc
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 207 deletions.
4 changes: 3 additions & 1 deletion exporter/logzioexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"strconv"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter/internal/dbmodel"

"github.com/hashicorp/go-hclog"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/pkg/cache"
Expand Down Expand Up @@ -189,7 +191,7 @@ func (exporter *logzioExporter) pushTraceData(ctx context.Context, traces ptrace
span.Process = batch.Process
span.Tags = exporter.dropEmptyTags(span.Tags)
span.Process.Tags = exporter.dropEmptyTags(span.Process.Tags)
logzioSpan, transformErr := transformToLogzioSpanBytes(span)
logzioSpan, transformErr := dbmodel.TransformToLogzioSpanBytes(span)
if transformErr != nil {
return transformErr
}
Expand Down
4 changes: 3 additions & 1 deletion exporter/logzioexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"testing"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter/internal/dbmodel"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -255,7 +257,7 @@ func TestPushTraceData(tester *testing.T) {
res.Attributes().PutStr(conventions.AttributeHostName, testHost)
err := testTracesExporter(tester, td, &cfg)
require.NoError(tester, err)
var newSpan logzioSpan
var newSpan dbmodel.LogzioSpan
decoded, _ := gUnzipData(recordedRequests)
requests := strings.Split(string(decoded), "\n")
assert.NoError(tester, json.Unmarshal([]byte(requests[0]), &newSpan))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package logzioexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter"
package dbmodel // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter"

import (
"strings"

"github.com/jaegertracing/jaeger/model"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter/internal/dbmodel"
)

// newFromDomain creates fromDomain used to convert model span to db span
Expand All @@ -30,15 +28,15 @@ type fromDomain struct {

// fromDomainEmbedProcess converts model.span into json.span format.
// This format includes a ParentSpanID and an embedded process.
func (fd fromDomain) fromDomainEmbedProcess(span *model.Span) *logzioSpan {
func (fd fromDomain) fromDomainEmbedProcess(span *model.Span) *LogzioSpan {
return fd.convertSpanEmbedProcess(span)
}

func (fd fromDomain) convertSpanInternal(span *model.Span) logzioSpan {
func (fd fromDomain) convertSpanInternal(span *model.Span) LogzioSpan {
tags, tagsMap := fd.convertKeyValuesString(span.Tags)
return logzioSpan{
TraceID: dbmodel.TraceID(span.TraceID.String()),
SpanID: dbmodel.SpanID(span.SpanID.String()),
return LogzioSpan{
TraceID: TraceID(span.TraceID.String()),
SpanID: SpanID(span.SpanID.String()),
Flags: uint32(span.Flags),
OperationName: span.OperationName,
StartTime: model.TimeAsEpochMicroseconds(span.StartTime),
Expand All @@ -50,35 +48,35 @@ func (fd fromDomain) convertSpanInternal(span *model.Span) logzioSpan {
}
}

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

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

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

func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]dbmodel.KeyValue, map[string]any) {
func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]KeyValue, map[string]any) {
var tagsMap map[string]any
var kvs []dbmodel.KeyValue
var kvs []KeyValue
for _, kv := range keyValues {
if kv.GetVType() != model.BinaryType && (fd.allTagsAsFields || fd.tagKeysAsFields[kv.Key]) {
if tagsMap == nil {
Expand All @@ -90,39 +88,39 @@ func (fd fromDomain) convertKeyValuesString(keyValues model.KeyValues) ([]dbmode
}
}
if kvs == nil {
kvs = make([]dbmodel.KeyValue, 0)
kvs = make([]KeyValue, 0)
}
return kvs, tagsMap
}

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

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

func convertKeyValue(kv model.KeyValue) dbmodel.KeyValue {
return dbmodel.KeyValue{
func convertKeyValue(kv model.KeyValue) KeyValue {
return KeyValue{
Key: kv.Key,
Type: dbmodel.ValueType(strings.ToLower(kv.VType.String())),
Type: ValueType(strings.ToLower(kv.VType.String())),
Value: kv.AsString(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package logzioexporter
package dbmodel

import (
"bytes"
Expand All @@ -17,8 +17,6 @@ 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 All @@ -29,18 +27,18 @@ func TestFromDomainEmbedProcess(t *testing.T) {
converter := newFromDomain(false, nil, ":")
embeddedSpan := converter.fromDomainEmbedProcess(&span)

var expectedSpan logzioSpan
var expectedSpan LogzioSpan
require.NoError(t, json.Unmarshal(jsonStr, &expectedSpan))

testJSONEncoding(t, jsonStr, embeddedSpan.transformToDbModelSpan())
testJSONEncoding(t, jsonStr, embeddedSpan)
}

// Loads and returns domain model and JSON model.
func loadModel(t *testing.T) ([]byte, []byte) {
in := fmt.Sprintf("./testdata/span.json")
in := fmt.Sprintf("../../testdata/span.json")
inStr, err := os.ReadFile(in)
require.NoError(t, err)
out := fmt.Sprintf("./testdata/es.json")
out := fmt.Sprintf("../../testdata/logziospan.json")
outStr, err := os.ReadFile(out)
require.NoError(t, err)
return inStr, outStr
Expand Down Expand Up @@ -97,35 +95,35 @@ func TestConvertKeyValueValue(t *testing.T) {
key := "key"
tests := []struct {
kv model.KeyValue
expected dbmodel.KeyValue
expected KeyValue
}{
{
kv: model.Bool(key, true),
expected: dbmodel.KeyValue{Key: key, Value: "true", Type: "bool"},
expected: KeyValue{Key: key, Value: "true", Type: "bool"},
},
{
kv: model.Bool(key, false),
expected: dbmodel.KeyValue{Key: key, Value: "false", Type: "bool"},
expected: KeyValue{Key: key, Value: "false", Type: "bool"},
},
{
kv: model.Int64(key, int64(1499)),
expected: dbmodel.KeyValue{Key: key, Value: "1499", Type: "int64"},
expected: KeyValue{Key: key, Value: "1499", Type: "int64"},
},
{
kv: model.Float64(key, float64(15.66)),
expected: dbmodel.KeyValue{Key: key, Value: "15.66", Type: "float64"},
expected: KeyValue{Key: key, Value: "15.66", Type: "float64"},
},
{
kv: model.String(key, longString),
expected: dbmodel.KeyValue{Key: key, Value: longString, Type: "string"},
expected: KeyValue{Key: key, Value: longString, Type: "string"},
},
{
kv: model.Binary(key, []byte(longString)),
expected: dbmodel.KeyValue{Key: key, Value: hex.EncodeToString([]byte(longString)), Type: "binary"},
expected: KeyValue{Key: key, Value: hex.EncodeToString([]byte(longString)), Type: "binary"},
},
{
kv: model.KeyValue{VType: 1500, Key: key},
expected: dbmodel.KeyValue{Key: key, Value: "unknown type 1500", Type: "1500"},
expected: KeyValue{Key: key, Value: "unknown type 1500", Type: "1500"},
},
}

Expand Down
66 changes: 66 additions & 0 deletions exporter/logzioexporter/internal/dbmodel/logziospan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package dbmodel // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter"

import (
"encoding/json"

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

const (
spanLogType = "jaegerSpan"
// TagDotReplacementCharacter state which character should replace the dot in es
tagDotReplacementCharacter = "@"
)

// 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"`
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"`
}

func getTagsValues(tags []model.KeyValue) []string {
values := make([]string, len(tags))
for i := range tags {
values[i] = tags[i].VStr
}
return values
}

// 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 := newFromDomain(true, getTagsValues(span.Tags), tagDotReplacementCharacter)
jsonSpan := spanConverter.fromDomainEmbedProcess(span)
newSpan := LogzioSpan{
TraceID: jsonSpan.TraceID,
OperationName: jsonSpan.OperationName,
SpanID: jsonSpan.SpanID,
References: jsonSpan.References,
Flags: jsonSpan.Flags,
StartTime: jsonSpan.StartTime,
StartTimeMillis: jsonSpan.StartTimeMillis,
Timestamp: jsonSpan.StartTimeMillis,
Duration: jsonSpan.Duration,
Tags: jsonSpan.Tags,
Tag: jsonSpan.Tag,
Process: jsonSpan.Process,
Logs: jsonSpan.Logs,
Type: spanLogType,
}
return json.Marshal(newSpan)
}
33 changes: 33 additions & 0 deletions exporter/logzioexporter/internal/dbmodel/logziospan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package dbmodel

import (
"encoding/json"
"fmt"
"os"
"testing"

"github.com/jaegertracing/jaeger/model"
"github.com/stretchr/testify/require"
)

func TestTransformToLogzioSpanBytes(tester *testing.T) {
inStr, err := os.ReadFile("../../testdata/span.json")
require.NoError(tester, err, "error opening sample span file")

var span model.Span
err = json.Unmarshal(inStr, &span)
if err != nil {
fmt.Println("json.Unmarshal")
}
newSpan, err := TransformToLogzioSpanBytes(&span)
require.NoError(tester, err)
m := make(map[string]any)
err = json.Unmarshal(newSpan, &m)
require.NoError(tester, err)
if _, ok := m["JaegerTag"]; !ok {
tester.Error("error converting span to logzioSpan, JaegerTag is not found")
}
}
21 changes: 0 additions & 21 deletions exporter/logzioexporter/internal/dbmodel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,6 @@ const (
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"`
Expand Down
Loading

0 comments on commit fede1cc

Please sign in to comment.