Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzk1 committed Dec 28, 2024
1 parent 4421218 commit 2a9709a
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 359 deletions.
90 changes: 0 additions & 90 deletions exporter/logzioexporter/dbmodel.go

This file was deleted.

2 changes: 1 addition & 1 deletion exporter/logzioexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,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 logzioSpan
var newSpan LogzioSpan
decoded, _ := gUnzipData(recordedRequests)
requests := strings.Split(string(decoded), "\n")
assert.NoError(tester, json.Unmarshal([]byte(requests[0]), &newSpan))
Expand Down
8 changes: 4 additions & 4 deletions exporter/logzioexporter/from_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ 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) *Span {
func (fd FromDomain) FromDomainEmbedProcess(span *model.Span) *LogzioSpan {
return fd.convertSpanEmbedProcess(span)
}

func (fd FromDomain) convertSpanInternal(span *model.Span) Span {
func (fd FromDomain) convertSpanInternal(span *model.Span) LogzioSpan {
tags, tagsMap := fd.convertKeyValuesString(span.Tags)
return Span{
return LogzioSpan{
TraceID: TraceID(span.TraceID.String()),
SpanID: SpanID(span.SpanID.String()),
Flags: uint32(span.Flags),
Expand All @@ -48,7 +48,7 @@ func (fd FromDomain) convertSpanInternal(span *model.Span) Span {
}
}

func (fd FromDomain) convertSpanEmbedProcess(span *model.Span) *Span {
func (fd FromDomain) convertSpanEmbedProcess(span *model.Span) *LogzioSpan {
s := fd.convertSpanInternal(span)
s.Process = fd.convertProcess(span.Process)
s.References = fd.convertReferences(span)
Expand Down
139 changes: 139 additions & 0 deletions exporter/logzioexporter/from_domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package logzioexporter

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

"github.com/gogo/protobuf/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestFromDomainEmbedProcess(t *testing.T) {
domainStr, jsonStr := loadFixtures(t)

var span model.Span
require.NoError(t, jsonpb.Unmarshal(bytes.NewReader(domainStr), &span))
converter := NewFromDomain(false, nil, ":")
embeddedSpan := converter.FromDomainEmbedProcess(&span)

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

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

// Loads and returns domain model and JSON model fixtures with given number i.
func loadFixtures(t *testing.T) ([]byte, []byte) {
in := fmt.Sprintf("./testdata/span.json")
inStr, err := os.ReadFile(in)
require.NoError(t, err)
out := fmt.Sprintf("./testdata/es.json")
outStr, err := os.ReadFile(out)
require.NoError(t, err)
return inStr, outStr
}

func testJSONEncoding(t *testing.T, expectedStr []byte, object any) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")

outFile := fmt.Sprintf("./testdata/es.json")
require.NoError(t, enc.Encode(object))

if !assert.Equal(t, string(expectedStr), buf.String()) {
err := os.WriteFile(outFile+"-actual.json", buf.Bytes(), 0o644)
require.NoError(t, err)
}
}

func TestEmptyTags(t *testing.T) {
tags := make([]model.KeyValue, 0)
span := model.Span{Tags: tags, Process: &model.Process{Tags: tags}}
converter := NewFromDomain(false, nil, ":")
dbSpan := converter.FromDomainEmbedProcess(&span)
assert.Empty(t, dbSpan.Tags)
assert.Empty(t, dbSpan.Tag)
}

func TestTagMap(t *testing.T) {
tags := []model.KeyValue{
model.String("foo", "foo"),
model.Bool("a", true),
model.Int64("b.b", 1),
}
span := model.Span{Tags: tags, Process: &model.Process{Tags: tags}}
converter := NewFromDomain(false, []string{"a", "b.b", "b*"}, ":")
dbSpan := converter.FromDomainEmbedProcess(&span)

assert.Len(t, dbSpan.Tags, 1)
assert.Equal(t, "foo", dbSpan.Tags[0].Key)
assert.Len(t, dbSpan.Process.Tags, 1)
assert.Equal(t, "foo", dbSpan.Process.Tags[0].Key)

tagsMap := map[string]any{}
tagsMap["a"] = true
tagsMap["b:b"] = int64(1)
assert.Equal(t, tagsMap, dbSpan.Tag)
assert.Equal(t, tagsMap, dbSpan.Process.Tag)
}

func TestConvertKeyValueValue(t *testing.T) {
longString := `Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues
Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues Bender Bending Rodrigues `
key := "key"
tests := []struct {
kv model.KeyValue
expected KeyValue
}{
{
kv: model.Bool(key, true),
expected: KeyValue{Key: key, Value: "true", Type: "bool"},
},
{
kv: model.Bool(key, false),
expected: KeyValue{Key: key, Value: "false", Type: "bool"},
},
{
kv: model.Int64(key, int64(1499)),
expected: KeyValue{Key: key, Value: "1499", Type: "int64"},
},
{
kv: model.Float64(key, float64(15.66)),
expected: KeyValue{Key: key, Value: "15.66", Type: "float64"},
},
{
kv: model.String(key, longString),
expected: KeyValue{Key: key, Value: longString, Type: "string"},
},
{
kv: model.Binary(key, []byte(longString)),
expected: 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"},
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%s:%s", test.expected.Type, test.expected.Key), func(t *testing.T) {
actual := convertKeyValue(test.kv)
assert.Equal(t, test.expected, actual)
})
}
}
5 changes: 4 additions & 1 deletion exporter/logzioexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logzio
go 1.22.0

require (
github.com/gogo/protobuf v1.3.2
github.com/hashicorp/go-hclog v1.6.3
github.com/jaegertracing/jaeger v1.62.0
github.com/kr/pretty v0.3.1
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger v0.116.0
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/component v0.116.1-0.20241220212031-7c2639723f67
Expand Down Expand Up @@ -36,7 +38,6 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand All @@ -45,6 +46,7 @@ require (
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand All @@ -54,6 +56,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.116.0 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.11.1 // indirect
go.opentelemetry.io/collector/client v1.22.1-0.20241220212031-7c2639723f67 // indirect
go.opentelemetry.io/collector/config/configauth v0.116.1-0.20241220212031-7c2639723f67 // indirect
Expand Down
3 changes: 3 additions & 0 deletions exporter/logzioexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2a9709a

Please sign in to comment.