Skip to content

Commit

Permalink
feat: created util to print out trace from endpoint call
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal committed Dec 18, 2024
1 parent b6bf153 commit 7bbd148
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
7 changes: 4 additions & 3 deletions routes/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/handlers"
"github.com/stakwork/sphinx-tribes/utils"
)

func PeopleRoutes() chi.Router {
Expand All @@ -14,9 +15,9 @@ func PeopleRoutes() chi.Router {

peopleHandler := handlers.NewPeopleHandler(db.DB)
r.Group(func(r chi.Router) {
r.Get("/", peopleHandler.GetListedPeople)
r.Get("/search", peopleHandler.GetPeopleBySearch)
r.Get("/posts", handlers.GetListedPosts)
r.Get("/", utils.TraceWithLogging(peopleHandler.GetListedPeople))
r.Get("/search", utils.TraceWithLogging(peopleHandler.GetPeopleBySearch))
r.Get("/posts", utils.TraceWithLogging(handlers.GetListedPosts))
r.Get("/wanteds/assigned/{uuid}", bountyHandler.GetPersonAssignedBounties)
r.Get("/wanteds/created/{uuid}", bountyHandler.GetPersonCreatedBounties)
r.Get("/wanteds/header", handlers.GetWantedsHeader)
Expand Down
3 changes: 2 additions & 1 deletion routes/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/handlers"
"github.com/stakwork/sphinx-tribes/utils"
)

func PersonRoutes() chi.Router {
Expand All @@ -13,7 +14,7 @@ func PersonRoutes() chi.Router {
r.Group(func(r chi.Router) {
r.Get("/{pubkey}", peopleHandler.GetPersonByPubkey)
r.Get("/id/{id}", peopleHandler.GetPersonById)
r.Get("/uuid/{uuid}", peopleHandler.GetPersonByUuid)
r.Get("/uuid/{uuid}", utils.TraceWithLogging(peopleHandler.GetPersonByUuid))
r.Get("/uuid/{uuid}/assets", handlers.GetPersonAssetsByUuid)
r.Get("/githubname/{github}", handlers.GetPersonByGithubName)
})
Expand Down
22 changes: 21 additions & 1 deletion routes/test_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package routes

import (
"net/http"
"runtime/trace"
"log"
// "os"
"bytes"

"github.com/go-chi/chi"
)
Expand All @@ -10,7 +14,23 @@ func TestRoutes() chi.Router {
r := chi.NewRouter()

r.Get("/internal-server-error", func(w http.ResponseWriter, r *http.Request) {
panic("Forced internal server error")
// Enable tracing
//f, err := os.Create("trace.out")
var buf bytes.Buffer
//if err != nil {
// log.Fatalf("Failed to create trace output file: %v", err)
//}
//defer f.Close()

if err := trace.Start(&buf); err != nil {
log.Fatalf("Failed to start trace: %v", err)
}
defer func() {
trace.Stop()
log.Println("Trace Data:")
log.Println(buf.String())
}()
//panic("Forced internal server error")
})

return r
Expand Down
81 changes: 81 additions & 0 deletions utils/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package utils

import (
"bytes"
"log"
"net/http"
"regexp"
"runtime/trace"
"strings"
)

func TraceWithLogging(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer

// Start trace capturing
if err := trace.Start(&buf); err != nil {
http.Error(w, "Failed to start trace", http.StatusInternalServerError)
log.Fatalf("Failed to start trace: %v", err)
return
}
defer func() {
trace.Stop()

// Extract and process trace output
traceOutput := buf.String()
//log.Println("Raw Trace Output (for debugging):", traceOutput)
// Split the trace output into lines
traceLines := strings.Split(traceOutput, "\n")
startedFormatting := false

// Log each trace line with a line number and format
for _, line := range traceLines {
if !startedFormatting && strings.Contains(line, "start trace") {
startedFormatting = true
} else {
continue
}

if line != "" {
traceFormatted := formatTrace(line)
for j, formattedLine := range traceFormatted {
log.Printf("%03d %s", j+1, formattedLine)
}
}
}
}()

next(w, r)
}
}

func splitTrace(traceString string) []string {
// Define the regex pattern for detecting file paths ending with .go
re := regexp.MustCompile(`/([^/\s]+\.go)`)

// Replace each match with the file path followed by a newline
formattedTrace := re.ReplaceAllString(traceString, "$0\n")

cleanRegex := regexp.MustCompile(`[^a-zA-Z0-9\s\./:_\-@()]`)
cleanedTrace := cleanRegex.ReplaceAllString(formattedTrace, "")


// Split the formatted trace into lines and return as a string array
return strings.Split(strings.TrimSpace(cleanedTrace), "\n")
}
func formatTrace(input string) []string {
// Find the position where "start trace" appears
startIdx := strings.Index(input, "start trace")
if startIdx == -1 {
return nil // "start trace" not found, return nil slice
}

// Slice input string from "start trace" onwards
traceContent := input[startIdx:]

// Use splitTrace to split and format the trace content
return splitTrace(traceContent)
}


0 comments on commit 7bbd148

Please sign in to comment.