Skip to content

Commit

Permalink
Add support for opentelemetry tracing.
Browse files Browse the repository at this point in the history
Trace our render and record some useful events during the rendering.
  • Loading branch information
paddycarver committed May 11, 2024
1 parent 4a458c8 commit 4b3ba40
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module impractical.co/temple

go 1.21

require (
go.opentelemetry.io/otel v1.26.0
go.opentelemetry.io/otel/trace v1.26.0
)

require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30=
go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21 changes: 21 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"html/template"
"io"
"io/fs"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

var (
Expand Down Expand Up @@ -111,6 +115,10 @@ func Render[SiteType Site, PageType Renderable](ctx context.Context, out io.Writ
}
}()

tracer := otel.GetTracerProvider().Tracer("impractical.co/temple")
var span trace.Span
ctx, span = tracer.Start(ctx, "render")
defer span.End()
// try to render the page
err := basicRender(ctx, out, site, page)

Expand All @@ -126,6 +134,11 @@ func Render[SiteType Site, PageType Renderable](ctx context.Context, out io.Writ
logger(ctx).
ErrorContext(ctx, "error rendering page", "error", err)

span.AddEvent("error rendering span",
trace.WithStackTrace(true),
trace.WithAttributes(attribute.String("error", err.Error())),
)

// now let's render the server error page
if pager, ok := Site(site).(ServerErrorPager); ok {
err = basicRender(ctx, out, site, pager.ServerErrorPage(ctx))
Expand Down Expand Up @@ -170,10 +183,14 @@ func basicRender[SiteType Site, PageType Renderable](ctx context.Context, output
}

func getTemplate(ctx context.Context, site Site, page Renderable) (*template.Template, error) {
span := trace.SpanFromContext(ctx)
key := page.Key(ctx)
if cache, ok := site.(TemplateCacher); ok {
cached := cache.GetCachedTemplate(ctx, key)
if cached != nil {
span.AddEvent("got cached template",
trace.WithAttributes(attribute.String("key", key)),
)
return cached, nil
}
}
Expand All @@ -189,6 +206,10 @@ func getTemplate(ctx context.Context, site Site, page Renderable) (*template.Tem
if cache, ok := site.(TemplateCacher); ok {
cache.SetCachedTemplate(ctx, key, parsed)
}
span.AddEvent("parsed templates",
trace.WithAttributes(attribute.String("key", key)),
trace.WithAttributes(attribute.StringSlice("templates", tmplPaths)),
)
return parsed, nil
}

Expand Down

0 comments on commit 4b3ba40

Please sign in to comment.