Skip to content

Commit

Permalink
feat(events): add route to context (#602)
Browse files Browse the repository at this point in the history
<!--
  !!!! README !!!! Please fill this out.

  Please follow conventional commit naming conventions:

  https://www.conventionalcommits.org/en/v1.0.0/#summary
-->

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for additional
information on contributing to this repository!

<!-- A short description of what your PR does and what it solves. -->
## What this PR does / why we need it

- feat: request route to be able to describe HTTP path without
extrapolated path variables. This will be used in common service heath
dashboard.
- feat: apiv2 client will be updated to leverage this feature 

<!-- <<Stencil::Block(jiraPrefix)>> -->

## Jira ID

[XX-XX]

<!-- <</Stencil::Block>> -->

<!-- Notes that may be helpful for anyone reviewing this PR -->
## Notes for your reviewers

<!-- <<Stencil::Block(custom)>> -->

<!-- <</Stencil::Block>> -->

---------

Co-authored-by: Mark Lee <[email protected]>
  • Loading branch information
pavelsmejkal and malept authored Nov 14, 2024
1 parent 07b206d commit 2a6e9d6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package events

import (
"context"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -81,6 +82,9 @@ type HTTPRequest struct {
Path string `log:"http.url_details.path"`
URI string `log:"http.url_details.uri"`
Endpoint string `log:"http.url_details.endpoint"`

// Route is the URL path without interpolating the path variables.
Route string `log:"http.route"`
}

// FillFieldsFromRequest fills in the standard request fields
Expand All @@ -95,6 +99,7 @@ func (h *HTTPRequest) FillFieldsFromRequest(r *http.Request) {
h.RemoteAddr = h.getRemoteAddr(r)
h.Times.Scheduled = h.getXRequestStart(r)
h.Times.Started = time.Now()
h.Route = RequestRoute(r.Context())
}

// FillResponseInfo fills in the response data as well as ends
Expand Down Expand Up @@ -125,3 +130,20 @@ func (h *HTTPRequest) getRemoteAddr(r *http.Request) string {
// See: ETC-182. Use a remote address library
return strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]
}

// requestRouteCtxKey represents the context key for the request route.
type requestRouteCtxKey struct{}

// WithRequestRoute adds a given request route to a context.
func WithRequestRoute(ctx context.Context, route string) context.Context {
return context.WithValue(ctx, requestRouteCtxKey{}, route)
}

// RequestRoute retrieves the request route present in the context, if available.
func RequestRoute(ctx context.Context) string {
t := ctx.Value(requestRouteCtxKey{})
if t == nil {
return ""
}
return t.(string)
}
7 changes: 5 additions & 2 deletions pkg/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
type eventsSuite struct{}

func (eventsSuite) TestHTTPRequest(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost/myendpoint", http.NoBody)
req, err := http.NewRequest("GET", "http://localhost/myendpoint/1", http.NoBody)
if err != nil {
t.Fatal("Unexpected err", err)
}

req = req.WithContext(events.WithRequestRoute(req.Context(), "/myendpoint/:id"))

req.Header.Add("X-Forwarded-For", "1.1.1.1, 2.2.2.2")
xrs := time.Now().Add(-time.Minute)
seconds := xrs.Unix()
Expand Down Expand Up @@ -50,7 +52,8 @@ func (eventsSuite) TestHTTPRequest(t *testing.T) {
Duration: 0,
Method: "GET",
StatusCode: 202,
Path: "/myendpoint",
Path: "/myendpoint/1",
Route: "/myendpoint/:id",
}
if diff := cmp.Diff(info, expected, cmp.Comparer(approxTime), cmp.Comparer(approxFloat)); diff != "" {
t.Fatal("unexpected", diff)
Expand Down
3 changes: 3 additions & 0 deletions pkg/events/marshalers.go

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

0 comments on commit 2a6e9d6

Please sign in to comment.