Skip to content

Commit

Permalink
test: add unit tests for sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
karstenkoehler committed Nov 15, 2023
1 parent f24a12e commit fea832c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
Expand All @@ -43,13 +44,16 @@ require (
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
github.com/openzipkin/zipkin-go v0.4.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rbcervilla/redisstore/v9 v9.0.0 // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/zemirco/memorystore v0.0.0-20160308183530-ecd57e5134f6 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
Expand All @@ -63,4 +67,5 @@ require (
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/zemirco/memorystore v0.0.0-20160308183530-ecd57e5134f6 h1:j+ZgVPhfLkC3WDIqNCSpU2/Y67d2FNohAjrxR3HV+KQ=
github.com/zemirco/memorystore v0.0.0-20160308183530-ecd57e5134f6/go.mod h1:PLhuixMlky6sB4/LEnpp1//u2BcRF2pKUYXLMVyOrIc=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
Expand Down
115 changes: 115 additions & 0 deletions sampler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package opentelemetry

import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestURLPrefixSampler_SampleAll(t *testing.T) {
tests := []struct {
path string
want bool
}{
{
path: "/",
want: true,
},
{
path: "/my-path",
want: true,
},
{
path: "/nested/path",
want: true,
},
{
path: "/static/assets/app.css",
want: true,
},
}

shouldSample := URLPrefixSampler(nil, nil, true)
for _, tt := range tests {
t.Run("checking path "+tt.path, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, tt.path, nil)
assert.NoError(t, err)

if got := shouldSample(request); got != tt.want {
t.Errorf("URLPrefixSampler.shouldSample() = %v, want %v", got, tt.want)
}
})
}
}

func TestURLPrefixSampler_SampleAllowed(t *testing.T) {
tests := []struct {
path string
want bool
}{
{
path: "/",
want: false,
},
{
path: "/my-path",
want: true,
},
{
path: "/nested/path",
want: true,
},
{
path: "/static/assets/app.css",
want: false,
},
}

shouldSample := URLPrefixSampler([]string{"/my-path", "/nested"}, nil, true)
for _, tt := range tests {
t.Run("checking path "+tt.path, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, tt.path, nil)
assert.NoError(t, err)

if got := shouldSample(request); got != tt.want {
t.Errorf("URLPrefixSampler.shouldSample() = %v, want %v", got, tt.want)
}
})
}
}

func TestURLPrefixSampler_SampleBlocked(t *testing.T) {
tests := []struct {
path string
want bool
}{
{
path: "/",
want: true,
},
{
path: "/my-path",
want: true,
},
{
path: "/nested/path",
want: true,
},
{
path: "/static/assets/app.css",
want: false,
},
}

shouldSample := URLPrefixSampler(nil, []string{"/static"}, true)
for _, tt := range tests {
t.Run("checking path "+tt.path, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, tt.path, nil)
assert.NoError(t, err)

if got := shouldSample(request); got != tt.want {
t.Errorf("URLPrefixSampler.shouldSample() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit fea832c

Please sign in to comment.