Skip to content

Commit

Permalink
Add newrelic transaction attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
bethanyj28 committed Feb 8, 2022
1 parent 4a1624d commit 5573aa1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/server/end.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/bethanyj28/battlesnek/internal"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/sirupsen/logrus"
)

Expand All @@ -21,10 +22,25 @@ func (s *server) handleEnd() http.HandlerFunc {
return
}

if txn := newrelic.FromContext(r.Context()); nil != txn {
txn.AddAttribute("won", didSnakeWin(state.Board.Snakes, state.You))
txn.AddAttribute("turns", state.Turn)
txn.AddAttribute("game_id", state.Game.ID)
}

s.logger.WithFields(logrus.Fields{
"game_state": fmt.Sprintf("%+v", state),
}).Info("END")

w.WriteHeader(http.StatusOK)
}
}

func didSnakeWin(remainingSnakes []internal.Battlesnake, you internal.Battlesnake) bool {
for _, snake := range remainingSnakes {
if snake.ID == you.ID {
return true
}
}
return false
}

0 comments on commit 5573aa1

Please sign in to comment.