-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add splunkhttprouter instrumentation
- Loading branch information
Showing
11 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Splunk instrumentation for `github.com/julienschmidt/httprouter` | ||
|
||
This package provides OpenTelemetry instrumentation for the | ||
[github.com/julienschmidt/httprouter](https://github.com/julienschmidt/httprouter) | ||
module. | ||
|
||
## Getting Started | ||
|
||
This package is designed to be used as a drop-in replacement for the use of the | ||
`github.com/julienschmidt/httprouter` package. See | ||
[example_test.go](./example_test.go) for more information. |
42 changes: 42 additions & 0 deletions
42
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/example_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Splunk Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package splunkhttprouter_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
|
||
"github.com/signalfx/splunk-otel-go/instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter" | ||
) | ||
|
||
func Index(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { | ||
fmt.Fprint(w, "Welcome!\n") | ||
} | ||
|
||
func Hello(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) { | ||
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) | ||
} | ||
|
||
func Example() { | ||
router := splunkhttprouter.New() | ||
router.GET("/", Index) | ||
router.GET("/hello/:name", Hello) | ||
|
||
if err := http.ListenAndServe(":8080", router); err != nil { | ||
panic(err) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/signalfx/splunk-otel-go/instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/julienschmidt/httprouter v1.3.0 | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0 | ||
) | ||
|
||
replace github.com/signalfx/splunk-otel-go => ../../../../.. |
32 changes: 32 additions & 0 deletions
32
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/go.sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/felixge/httpsnoop v1.0.2 h1:+nS9g82KMXccJ/wp0zyRW9ZBHFETmMGtkk+2CTTrW4o= | ||
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= | ||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/logr v1.2.1 h1:DX7uPQ4WgAWfoh+NGGlbJQswnYIVvz0SRlLS3rPZQDA= | ||
github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE= | ||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI= | ||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= | ||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= | ||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0 h1:hpEoMBvKLC6CqFZogJypr9IHwwSNF3ayEkNzD502QAM= | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0/go.mod h1:Ihno+mNBfZlT0Qot3XyRTdZ/9U/Cg2Pfgj75DTdIfq4= | ||
go.opentelemetry.io/otel v1.3.0 h1:APxLf0eiBwLl+SOXiJJCVYzA1OOJNyAoV8C5RNRyy7Y= | ||
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs= | ||
go.opentelemetry.io/otel/internal/metric v0.26.0 h1:dlrvawyd/A+X8Jp0EBT4wWEe4k5avYaXsXrBr4dbfnY= | ||
go.opentelemetry.io/otel/internal/metric v0.26.0/go.mod h1:CbBP6AxKynRs3QCbhklyLUtpfzbqCLiafV9oY2Zj1Jk= | ||
go.opentelemetry.io/otel/metric v0.26.0 h1:VaPYBTvA13h/FsiWfxa3yZnZEm15BhStD8JZQSA773M= | ||
go.opentelemetry.io/otel/metric v0.26.0/go.mod h1:c6YL0fhRo4YVoNs6GoByzUgBp36hBL523rECoZA5UWg= | ||
go.opentelemetry.io/otel/trace v1.3.0 h1:doy8Hzb1RJ+I3yFhtDmwNc7tIyw1tNMOIsyPzp1NOGY= | ||
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
67 changes: 67 additions & 0 deletions
67
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/httprouter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright Splunk Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package splunkhttprouter provides OpenTelemetry instrumentation for the | ||
// github.com/julienschmidt/httprouter module. | ||
package splunkhttprouter | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
) | ||
|
||
// Router is a traced version of httprouter.Router. | ||
type Router struct { | ||
*httprouter.Router | ||
|
||
handler http.Handler | ||
} | ||
|
||
// New returns a new router augmented with tracing. | ||
func New(opts ...otelhttp.Option) *Router { | ||
r := &Router{Router: httprouter.New()} | ||
|
||
const nInternalOpts = 1 | ||
o := make([]otelhttp.Option, len(opts)+nInternalOpts) | ||
// Put this first so it can be overridden by the user. | ||
o[0] = otelhttp.WithSpanNameFormatter(r.name) | ||
if len(opts) > 0 { | ||
copy(o[1:], opts) | ||
} | ||
r.handler = otelhttp.NewHandler(r.Router, "", o...) | ||
|
||
return r | ||
} | ||
|
||
// ServeHTTP serves req writing a response to w and tracing the process. | ||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
r.handler.ServeHTTP(w, req) | ||
} | ||
|
||
// name is a SpanFormatter for the otelhttp instrumentation. | ||
func (r *Router) name(_ string, req *http.Request) string { | ||
path := req.URL.Path | ||
_, params, trailing := r.Router.Lookup(req.Method, path) | ||
for _, param := range params { | ||
path = strings.Replace(path, param.Value, ":"+param.Key, 1) | ||
} | ||
if trailing { | ||
path = strings.TrimSuffix(path, "/") | ||
} | ||
|
||
return "HTTP " + req.Method + " " + path | ||
} |
17 changes: 17 additions & 0 deletions
17
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/test/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module github.com/signalfx/splunk-otel-go/instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/test | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/julienschmidt/httprouter v1.3.0 | ||
github.com/signalfx/splunk-otel-go/instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter v0.0.0-00010101000000-000000000000 | ||
github.com/stretchr/testify v1.7.0 | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0 | ||
go.opentelemetry.io/otel v1.3.0 | ||
go.opentelemetry.io/otel/sdk v1.3.0 | ||
) | ||
|
||
replace ( | ||
github.com/signalfx/splunk-otel-go => ../../../../../../ | ||
github.com/signalfx/splunk-otel-go/instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter => ../ | ||
) |
37 changes: 37 additions & 0 deletions
37
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/test/go.sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/felixge/httpsnoop v1.0.2 h1:+nS9g82KMXccJ/wp0zyRW9ZBHFETmMGtkk+2CTTrW4o= | ||
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= | ||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/logr v1.2.1 h1:DX7uPQ4WgAWfoh+NGGlbJQswnYIVvz0SRlLS3rPZQDA= | ||
github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE= | ||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI= | ||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= | ||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= | ||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0 h1:hpEoMBvKLC6CqFZogJypr9IHwwSNF3ayEkNzD502QAM= | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0/go.mod h1:Ihno+mNBfZlT0Qot3XyRTdZ/9U/Cg2Pfgj75DTdIfq4= | ||
go.opentelemetry.io/otel v1.3.0 h1:APxLf0eiBwLl+SOXiJJCVYzA1OOJNyAoV8C5RNRyy7Y= | ||
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs= | ||
go.opentelemetry.io/otel/internal/metric v0.26.0 h1:dlrvawyd/A+X8Jp0EBT4wWEe4k5avYaXsXrBr4dbfnY= | ||
go.opentelemetry.io/otel/internal/metric v0.26.0/go.mod h1:CbBP6AxKynRs3QCbhklyLUtpfzbqCLiafV9oY2Zj1Jk= | ||
go.opentelemetry.io/otel/metric v0.26.0 h1:VaPYBTvA13h/FsiWfxa3yZnZEm15BhStD8JZQSA773M= | ||
go.opentelemetry.io/otel/metric v0.26.0/go.mod h1:c6YL0fhRo4YVoNs6GoByzUgBp36hBL523rECoZA5UWg= | ||
go.opentelemetry.io/otel/sdk v1.3.0 h1:3278edCoH89MEJ0Ky8WQXVmDQv3FX4ZJ3Pp+9fJreAI= | ||
go.opentelemetry.io/otel/sdk v1.3.0/go.mod h1:rIo4suHNhQwBIPg9axF8V9CA72Wz2mKF1teNrup8yzs= | ||
go.opentelemetry.io/otel/trace v1.3.0 h1:doy8Hzb1RJ+I3yFhtDmwNc7tIyw1tNMOIsyPzp1NOGY= | ||
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk= | ||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw= | ||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
112 changes: 112 additions & 0 deletions
112
instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter/test/httprouter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright Splunk Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/* | ||
Package test provides end-to-end testing of the splunkhttprouter | ||
instrumentation with the default SDK. | ||
This package is in a separate module from the instrumentation it tests to | ||
isolate the dependency of the default SDK and not impose this as a transitive | ||
dependency for users. | ||
*/ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/signalfx/splunk-otel-go/instrumentation/github.com/julienschmidt/httprouter/splunkhttprouter" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
) | ||
|
||
func Error(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
|
||
func Hello(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) { | ||
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) | ||
} | ||
|
||
func newTestServer(tp *trace.TracerProvider) http.Handler { | ||
router := splunkhttprouter.New(otelhttp.WithTracerProvider(tp)) | ||
router.GET("/error", Error) | ||
router.GET("/hello/:name", Hello) | ||
|
||
return router | ||
} | ||
|
||
func newFixtures(t *testing.T) (*tracetest.SpanRecorder, http.Handler) { | ||
sr := tracetest.NewSpanRecorder() | ||
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) | ||
t.Cleanup(func() { require.NoError(t, tp.Shutdown(context.Background())) }) | ||
return sr, newTestServer(tp) | ||
} | ||
|
||
func TestRouter(t *testing.T) { | ||
tests := []struct { | ||
method string | ||
target string | ||
assertion func(*testing.T, string, string, trace.ReadOnlySpan) | ||
}{ | ||
{ | ||
method: http.MethodGet, | ||
target: "/hello/bob", | ||
assertion: assertSpan("/hello/:name", codes.Unset), | ||
}, | ||
{ | ||
method: http.MethodGet, | ||
target: "/hello/bob/", | ||
assertion: assertSpan("/hello/:name", codes.Unset), | ||
}, | ||
{ | ||
method: http.MethodGet, | ||
target: "/error", | ||
assertion: assertSpan("/error", codes.Error), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
sr, r := newFixtures(t) | ||
req := httptest.NewRequest(test.method, test.target, http.NoBody) | ||
r.ServeHTTP(httptest.NewRecorder(), req) | ||
t.Run(test.method+" "+test.target, func(t *testing.T) { | ||
require.Len(t, sr.Ended(), 1) | ||
test.assertion(t, test.method, test.target, sr.Ended()[0]) | ||
}) | ||
} | ||
} | ||
|
||
func assertSpan(path string, otelCode codes.Code) func(*testing.T, string, string, trace.ReadOnlySpan) { | ||
return func(t *testing.T, method, target string, span trace.ReadOnlySpan) { | ||
name := "HTTP " + method | ||
if path != "" { | ||
name += " " + path | ||
} | ||
assert.Equal(t, name, span.Name()) | ||
|
||
status := span.Status() | ||
assert.Equal(t, otelCode, status.Code) | ||
assert.Equal(t, "", status.Description) | ||
} | ||
} |