Skip to content

Commit

Permalink
Added span for startup and auth
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinPower747 committed Aug 25, 2024
1 parent 2c12e90 commit b760d77
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-bookworm as builder
FROM golang:1.23-bookworm as builder
WORKDIR /app

COPY go.* ./
Expand Down
8 changes: 8 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/gorilla/mux"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/otel"

"gavs.at/shortener/internal/handlers"
"gavs.at/shortener/internal/storage"
Expand All @@ -26,6 +27,7 @@ func main() {
}

func run() (err error) {

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

Expand All @@ -35,6 +37,11 @@ func run() (err error) {
return
}

tracer := otel.GetTracerProvider().Tracer("gavs.at/shortener")

_, span := tracer.Start(ctx, "startup")
defer span.End()

defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
}()
Expand Down Expand Up @@ -74,6 +81,7 @@ func run() (err error) {
}

err = srv.Shutdown(context.Background())

return
}

Expand Down
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module gavs.at/shortener

go 1.21.0

toolchain go1.22.2
go 1.23.0

require (
github.com/Azure/azure-sdk-for-go/sdk/data/aztables v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.0
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/gorilla/mux v1.8.1
github.com/microsoft/ApplicationInsights-Go v0.4.4
Expand All @@ -14,14 +13,12 @@ require (
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
)

require (
code.cloudfoundry.org/clock v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -33,7 +30,6 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/Azure/azure-sdk-for-go/sdk/data/aztables v1.2.0 h1:aJG+Jxd9/rrLwf8R1K
github.com/Azure/azure-sdk-for-go/sdk/data/aztables v1.2.0/go.mod h1:41ONblJrPxDcnVr+voS+3xXWy/KnZLh+7zY5s6woAlQ=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1 h1:7CBQ+Ei8SP2c6ydQTGCCrS35bDxgTMfoP2miAwK++OU=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1/go.mod h1:c/wcGeGx5FUPbM/JltUYHZcKmigwyVLJlDq+4HdtXaw=
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.0 h1:RTTsXUJWn0jumeX62Mb153wYXykqnrzYBYDeHp0kiuk=
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.0/go.mod h1:k4MMjrPHIEK+umaMGk1GNLgjEybJZ9mHSRDZ+sDFv3Y=
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
Expand Down Expand Up @@ -71,10 +73,10 @@ github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.53.0 h1:KHTx4DmXkuhl/a4/jU5eDMrPuxulzd7m8nusORJ64Fc=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.53.0/go.mod h1:Orsflew5fQlsj8qLxP5A9Y38PGaRxXs93TGaDHDwGT0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIXefpVJtvA/8srF4V4y0akAoPHkIslgAkjixJA=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg=
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
go.opentelemetry.io/otel/exporters/jaeger v1.16.0 h1:YhxxmXZ011C0aDZKoNw+juVWAmEfv/0W2XBOv9aHTaA=
go.opentelemetry.io/otel/exporters/jaeger v1.16.0/go.mod h1:grYbBo/5afWlPpdPZYhyn78Bk04hnvxn2+hvxQhKIQM=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 h1:j9+03ymgYhPKmeXGk5Zu+cIZOlVzd9Zv7QIiyItjFBU=
Expand All @@ -83,8 +85,6 @@ go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6b
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/sdk/metric v1.28.0 h1:OkuaKgKrgAbYrrY0t92c+cC+2F6hsFNnCQArXCKlg08=
go.opentelemetry.io/otel/sdk/metric v1.28.0/go.mod h1:cWPjykihLAPvXKi4iZc1dpER3Jdq2Z0YLse3moQUCpg=
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
Expand Down
18 changes: 18 additions & 0 deletions pkg/middleware/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"gavs.at/shortener/pkg/web"
"go.opentelemetry.io/otel"
)

const (
Expand All @@ -18,10 +19,22 @@ const (

func BasicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tracer := otel.GetTracerProvider().Tracer("gavs.at/shortener")
_, span := tracer.Start(r.Context(), "BasicAuth")

endSpan := func() {
if span.IsRecording() {
span.End()
}
}

defer endSpan()

authHeader := strings.TrimSpace(r.Header.Get("Authorization"))

if authHeader == "" {
web.NotAuthorized(w, "Missing Authorization Header")
span.RecordError(fmt.Errorf("Missing Authorization Header"))

return
}
Expand All @@ -30,6 +43,7 @@ func BasicAuth(next http.Handler) http.Handler {

if !strings.HasPrefix(authHeader, "Basic ") {
web.NotAuthorized(w, fmt.Sprintf("Invalid Authorization Header, %s authentication scheme is not supported", headerSections[0]))
span.RecordError(fmt.Errorf("Invalid Authorization Header, %s authentication scheme is not supported", headerSections[0]))

return
}
Expand All @@ -38,6 +52,7 @@ func BasicAuth(next http.Handler) http.Handler {

if err != nil {
web.NotAuthorized(w, "Invalid Authorization Header")
span.RecordError(fmt.Errorf("Invalid Authorization Header"))

return
}
Expand All @@ -53,10 +68,13 @@ func BasicAuth(next http.Handler) http.Handler {

if username != expectedUsername || passwordHash != expectedPasswordHash {
web.NotAuthorized(w, "Invalid Credentials")
span.RecordError(fmt.Errorf("Invalid Credentials"))

return
}

endSpan()

next.ServeHTTP(w, r)
})
}
Expand Down

0 comments on commit b760d77

Please sign in to comment.