Skip to content

Commit

Permalink
remove back 0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzk1 committed Jan 4, 2025
1 parent 43e10a5 commit 930e342
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 72 deletions.
4 changes: 1 addition & 3 deletions exporter/logzioexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ 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 @@ -191,7 +189,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 := dbmodel.TransformToLogzioSpanBytes(span)
logzioSpan, transformErr := transformToLogzioSpanBytes(span)
if transformErr != nil {
return transformErr
}
Expand Down
4 changes: 1 addition & 3 deletions exporter/logzioexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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 @@ -257,7 +255,7 @@ func TestPushTraceData(tester *testing.T) {
res.Attributes().PutStr(conventions.AttributeHostName, testHost)
err := testTracesExporter(tester, td, &cfg)
require.NoError(tester, err)
var newSpan dbmodel.LogzioSpan
var newSpan 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
@@ -1,13 +1,16 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package dbmodel // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzioexporter"
package logzioexporter // 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 @@ -28,15 +31,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: TraceID(span.TraceID.String()),
SpanID: SpanID(span.SpanID.String()),
return logzioSpan{
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 @@ -48,35 +51,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) []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 @@ -88,39 +91,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) []Log {
out := make([]Log, len(logs))
func (fromDomain) convertLogs(logs []model.Log) []dbmodel.Log {
out := make([]dbmodel.Log, len(logs))
for i, log := range logs {
var kvs []KeyValue
var kvs []dbmodel.KeyValue
for _, kv := range log.Fields {
kvs = append(kvs, convertKeyValue(kv))
}
out[i] = Log{
out[i] = dbmodel.Log{
Timestamp: model.TimeAsEpochMicroseconds(log.Timestamp),
Fields: kvs,
}
}
return out
}

func (fd fromDomain) convertProcess(process *model.Process) Process {
func (fd fromDomain) convertProcess(process *model.Process) dbmodel.Process {
tags, tagsMap := fd.convertKeyValuesString(process.Tags)
return Process{
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(),
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package dbmodel
package logzioexporter

import (
"bytes"
Expand All @@ -13,10 +14,11 @@ import (
"testing"

"github.com/gogo/protobuf/jsonpb"
"github.com/jaegertracing/jaeger/model"
"github.com/stretchr/testify/assert"
"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 @@ -27,19 +29,17 @@ 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)
}

// Loads and returns domain model and JSON model.
func loadModel(t *testing.T) ([]byte, []byte) {
in := fmt.Sprintf("../../testdata/span.json")
inStr, err := os.ReadFile(in)
inStr, err := os.ReadFile("./testdata/span.json")
require.NoError(t, err)
out := fmt.Sprintf("../../testdata/logziospan.json")
outStr, err := os.ReadFile(out)
outStr, err := os.ReadFile("./testdata/logziospan.json")
require.NoError(t, err)
return inStr, outStr
}
Expand All @@ -50,7 +50,7 @@ func testJSONEncoding(t *testing.T, expectedStr []byte, object any) {
enc.SetIndent("", " ")
require.NoError(t, enc.Encode(object))
if !assert.Equal(t, string(expectedStr), buf.String()) {
err := os.WriteFile("model-actual.json", buf.Bytes(), 0o644)
err := os.WriteFile("model-actual.json", buf.Bytes(), 0o600)
require.NoError(t, err)
}
}
Expand Down Expand Up @@ -95,35 +95,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
3 changes: 2 additions & 1 deletion exporter/logzioexporter/internal/dbmodel/model.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

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

// ReferenceType is the reference type of one span to another
type ReferenceType string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"encoding/json"

"github.com/jaegertracing/jaeger/model"

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

const (
Expand All @@ -15,22 +17,22 @@ const (
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"`
// logzioSpan is same as esSpan with a few different json field names and an addition on type field.
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"`
}

func getTagsValues(tags []model.KeyValue) []string {
Expand All @@ -43,10 +45,10 @@ 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) {
func transformToLogzioSpanBytes(span *model.Span) ([]byte, error) {
spanConverter := newFromDomain(true, getTagsValues(span.Tags), tagDotReplacementCharacter)
jsonSpan := spanConverter.fromDomainEmbedProcess(span)
newSpan := LogzioSpan{
newSpan := logzioSpan{
TraceID: jsonSpan.TraceID,
OperationName: jsonSpan.OperationName,
SpanID: jsonSpan.SpanID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package dbmodel
package logzioexporter

import (
"encoding/json"
Expand All @@ -14,15 +14,15 @@ import (
)

func TestTransformToLogzioSpanBytes(tester *testing.T) {
inStr, err := os.ReadFile("../../testdata/span.json")
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)
newSpan, err := transformToLogzioSpanBytes(&span)
require.NoError(tester, err)
m := make(map[string]any)
err = json.Unmarshal(newSpan, &m)
Expand Down

0 comments on commit 930e342

Please sign in to comment.