Skip to content

Commit

Permalink
Merge pull request #1067 from Scalingo/fix/1065/otel-add-unit-tests
Browse files Browse the repository at this point in the history
test(otel): Add unit test for otel package
  • Loading branch information
brandon-welsch authored Feb 10, 2025
2 parents 85441f8 + 00dc388 commit 2e7e56c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions otel/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.4
require (
github.com/Scalingo/go-utils/errors/v2 v2.4.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/otel v1.34.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.34.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.34.0
Expand All @@ -15,11 +16,13 @@ require (

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
Expand All @@ -32,4 +35,5 @@ require (
google.golang.org/grpc v1.69.4 // indirect
google.golang.org/protobuf v1.36.3 // indirect
gopkg.in/errgo.v1 v1.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 3 additions & 0 deletions otel/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ google.golang.org/grpc v1.69.4 h1:MF5TftSMkd8GLw/m0KM6V8CMOCY6NZ1NQDPGFgbTt4A=
google.golang.org/grpc v1.69.4/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v1 v1.0.1 h1:oQFRXzZ7CkBGdm1XZm/EbQYaYNNEElNBOd09M6cqNso=
gopkg.in/errgo.v1 v1.0.1/go.mod h1:3NjfXwocQRYAPTq4/fzX+CwUhPRcR/azYRhj8G+LqMo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
77 changes: 77 additions & 0 deletions otel/otel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package otel

import (
"context"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
tests := []struct {
name string
reinit bool
env map[string]string
expectError string
}{
{
name: "initialization without service_name defined should result in error",
expectError: "required key OTEL_SERVICE_NAME missing value",
},
{
name: "minimal initialization",
env: map[string]string{
"OTEL_SERVICE_NAME": "test",
},
},
{
name: "re-initialization to check singleton usage",
reinit: true,
env: map[string]string{
"OTEL_SERVICE_NAME": "test",
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Cleanup(func() {
globalProviders = nil
globalOnce = sync.Once{}
})

ctx := context.Background()

if test.env != nil {
for k, v := range test.env {
t.Setenv(k, v)
}
}

err := New(ctx)
if test.expectError != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expectError)

require.Nil(t, globalProviders)
} else {
require.NoError(t, err)

require.NotNil(t, globalProviders)
assert.NotNil(t, globalProviders.meterProvider)

// Check when reinitializing the SDK
if test.reinit {
previousGlobalProviders := *globalProviders

err = New(ctx)
require.NoError(t, err)
// Check that pointer are the same
assert.Equal(t, previousGlobalProviders, *globalProviders)
}
}
})
}
}

0 comments on commit 2e7e56c

Please sign in to comment.