Skip to content

Commit

Permalink
generator: Start span before auth
Browse files Browse the repository at this point in the history
  • Loading branch information
monstermunchkin committed Dec 2, 2024
1 parent e335da7 commit 3b14fbc
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 287 deletions.
23 changes: 5 additions & 18 deletions http/jsonapi/generator/generate_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,31 +595,19 @@ func (g *Generator) buildHandler(method string, op *openapi3.Operation, pattern
// recover panics
g.Defer().Qual(pkgMaintErrors, "HandleRequest").Call(jen.Lit(handler), jen.Id("w"), jen.Id("r"))

g.Add(auth)
// set tracing context

ctxStmt := jen.Id("r").Dot("Context").Call()

if auth != nil {
ctxStmt = jen.Id("ctx")
}

g.Line().Comment("Trace the service function handler execution")
g.Id("span").Op(":=").Qual(pkgSentry, "StartSpan").Call(
ctxStmt, jen.Lit("http.server"), jen.Qual(pkgSentry, "WithDescription").Call(jen.Lit(handler)))
jen.Id("r").Dot("Context").Call(), jen.Lit("http.server"), jen.Qual(pkgSentry, "WithDescription").Call(jen.Lit(handler)))
g.Defer().Id("span").Dot("Finish").Call()
g.Line().Empty()

operator := ":="

if auth != nil {
operator = "="
}

g.Id("ctx").Op(operator).Id("span").Dot("Context").Call()
// set tracing context
g.Id("ctx").Op(":=").Id("span").Dot("Context").Call()

g.Id("r").Op("=").Id("r.WithContext").Call(jen.Id("ctx"))

g.Add(auth)

g.Line().Comment("Setup context, response writer and request type")

// response writer
Expand Down Expand Up @@ -830,7 +818,6 @@ func generateAuthorizationForMultipleSecSchemas(op *openapi3.Operation, secSchem

caser := cases.Title(language.Und, cases.NoLower)

r.Line().Var().Id("ctx").Id("context.Context")
r.Line().Var().Id("ok").Id("bool")
for _, val := range orderedSec {
name := val[0]
Expand Down
30 changes: 14 additions & 16 deletions http/jsonapi/generator/internal/pay/open-api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3b14fbc

Please sign in to comment.