Skip to content

Commit

Permalink
Add nr segment for more middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang committed Jan 20, 2025
1 parent aec1923 commit 09da6fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/middleware/metric/metric_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
"github.com/labstack/echo/v4"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand All @@ -36,6 +37,10 @@ func init() {
func MetricsMiddleware(client *monitoring.MetricClient, config *config.Config) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if txn := newrelic.FromContext(c.Request().Context()); txn != nil {
segment := txn.StartSegment("MetricsMiddleware")
defer segment.End()
}
startTime := time.Now()
err := next(c)
endTime := time.Now()
Expand Down
5 changes: 5 additions & 0 deletions server/middleware/request_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/labstack/echo/v4"
echo_middleware "github.com/labstack/echo/v4/middleware"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -40,6 +41,10 @@ func RequestLoggerMiddleware() echo.MiddlewareFunc {
LogURI: true,
LogStatus: true,
LogValuesFunc: func(c echo.Context, v echo_middleware.RequestLoggerValues) error {
if txn := newrelic.FromContext(c.Request().Context()); txn != nil {
segment := txn.StartSegment("RequestLoggerMiddleware")
defer segment.End()
}
// Get the teeReader
reader, ok := c.Request().Context().Value(ctxKey).(*teeReader)
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions server/middleware/response_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rs/zerolog/log"
)

Expand All @@ -29,6 +30,10 @@ func (rw *responseWriter) Write(p []byte) (n int, err error) {
func ResponseLoggerMiddleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if txn := newrelic.FromContext(c.Request().Context()); txn != nil {
segment := txn.StartSegment("ResponseLoggerMiddleware")
defer segment.End()
}
// Create a custom response writer to capture the response body
rw := &responseWriter{
ResponseWriter: c.Response().Writer,
Expand Down
6 changes: 6 additions & 0 deletions server/middleware/tracing_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rs/zerolog"
)

Expand All @@ -19,6 +20,11 @@ func generateFallbackCorrelationID() string {
// TracingMiddleware is a middleware that adds a trace ID to the context
func TracingMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if txn := newrelic.FromContext(c.Request().Context()); txn != nil {
segment := txn.StartSegment("TracingMiddleware")
defer segment.End()
}

traceID := c.Request().Header.Get("X-Cloud-Trace-Context")

if traceID == "" {
Expand Down

0 comments on commit 09da6fa

Please sign in to comment.