Skip to content

Commit

Permalink
[chore] Move resource test to service/internal/resource (#9730)
Browse files Browse the repository at this point in the history
**Description:**  This test was out of place!

Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
mx-psi and codeboten authored Mar 12, 2024
1 parent 0839af5 commit ef9caab
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 58 deletions.
67 changes: 67 additions & 0 deletions service/internal/resource/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
sdkresource "go.opentelemetry.io/otel/sdk/resource"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
)

const (
Expand Down Expand Up @@ -101,3 +104,67 @@ func TestNew(t *testing.T) {
}

}

func pdataFromSdk(res *sdkresource.Resource) pcommon.Resource {
// pcommon.NewResource is the best way to generate a new resource currently and is safe to use outside of tests.
// Because the resource is signal agnostic, and we need a net new resource, not an existing one, this is the only
// method of creating it without exposing internal packages.
pcommonRes := pcommon.NewResource()
for _, keyValue := range res.Attributes() {
pcommonRes.Attributes().PutStr(string(keyValue.Key), keyValue.Value.AsString())
}
return pcommonRes
}

func TestBuildResource(t *testing.T) {
buildInfo := component.NewDefaultBuildInfo()

// Check default config
var resMap map[string]*string
otelRes := New(buildInfo, resMap)
res := pdataFromSdk(otelRes)

assert.Equal(t, res.Attributes().Len(), 3)
value, ok := res.Attributes().Get(semconv.AttributeServiceName)
assert.True(t, ok)
assert.Equal(t, buildInfo.Command, value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceVersion)
assert.True(t, ok)
assert.Equal(t, buildInfo.Version, value.AsString())

_, ok = res.Attributes().Get(semconv.AttributeServiceInstanceID)
assert.True(t, ok)

// Check override by nil
resMap = map[string]*string{
semconv.AttributeServiceName: nil,
semconv.AttributeServiceVersion: nil,
semconv.AttributeServiceInstanceID: nil,
}
otelRes = New(buildInfo, resMap)
res = pdataFromSdk(otelRes)

// Attributes should not exist since we nil-ified all.
assert.Equal(t, res.Attributes().Len(), 0)

// Check override values
strPtr := func(v string) *string { return &v }
resMap = map[string]*string{
semconv.AttributeServiceName: strPtr("a"),
semconv.AttributeServiceVersion: strPtr("b"),
semconv.AttributeServiceInstanceID: strPtr("c"),
}
otelRes = New(buildInfo, resMap)
res = pdataFromSdk(otelRes)

assert.Equal(t, res.Attributes().Len(), 3)
value, ok = res.Attributes().Get(semconv.AttributeServiceName)
assert.True(t, ok)
assert.Equal(t, "a", value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceVersion)
assert.True(t, ok)
assert.Equal(t, "b", value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceInstanceID)
assert.True(t, ok)
assert.Equal(t, "c", value.AsString())
}
58 changes: 0 additions & 58 deletions service/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
Expand All @@ -37,63 +36,6 @@ const (
counterName = "test_counter"
)

func TestBuildResource(t *testing.T) {
buildInfo := component.NewDefaultBuildInfo()

// Check default config
cfg := telemetry.Config{}
otelRes := resource.New(buildInfo, cfg.Resource)
res := pdataFromSdk(otelRes)

assert.Equal(t, res.Attributes().Len(), 3)
value, ok := res.Attributes().Get(semconv.AttributeServiceName)
assert.True(t, ok)
assert.Equal(t, buildInfo.Command, value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceVersion)
assert.True(t, ok)
assert.Equal(t, buildInfo.Version, value.AsString())

_, ok = res.Attributes().Get(semconv.AttributeServiceInstanceID)
assert.True(t, ok)

// Check override by nil
cfg = telemetry.Config{
Resource: map[string]*string{
semconv.AttributeServiceName: nil,
semconv.AttributeServiceVersion: nil,
semconv.AttributeServiceInstanceID: nil,
},
}
otelRes = resource.New(buildInfo, cfg.Resource)
res = pdataFromSdk(otelRes)

// Attributes should not exist since we nil-ified all.
assert.Equal(t, res.Attributes().Len(), 0)

// Check override values
strPtr := func(v string) *string { return &v }
cfg = telemetry.Config{
Resource: map[string]*string{
semconv.AttributeServiceName: strPtr("a"),
semconv.AttributeServiceVersion: strPtr("b"),
semconv.AttributeServiceInstanceID: strPtr("c"),
},
}
otelRes = resource.New(buildInfo, cfg.Resource)
res = pdataFromSdk(otelRes)

assert.Equal(t, res.Attributes().Len(), 3)
value, ok = res.Attributes().Get(semconv.AttributeServiceName)
assert.True(t, ok)
assert.Equal(t, "a", value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceVersion)
assert.True(t, ok)
assert.Equal(t, "b", value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceInstanceID)
assert.True(t, ok)
assert.Equal(t, "c", value.AsString())
}

func TestTelemetryInit(t *testing.T) {
type metricValue struct {
value float64
Expand Down

0 comments on commit ef9caab

Please sign in to comment.