generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Request / Response matching (#70)
## Which problem is this PR solving? - Closes #66 ## Short description of the changes - Make sure there is only one event being created for every request ## How to verify that this has the expected result - Run the agent and look at the events in Honeycomb ## Known issues - Some requests don't have a response pair, we'll be addressing this in a future PR - Request / response body byte size doesn't currently work --------- Co-authored-by: JamieDanielson <jamieedanielson@gmail.com> Co-authored-by: Mike Goldsmth <goldsmith.mike@gmail.com> Co-authored-by: Jamie Danielson <JamieDanielson@users.noreply.github.com> Co-authored-by: Robb Kidd <robb@thekidds.org> Co-authored-by: Vera Reynolds <verareynolds@honeycomb.io> Co-authored-by: Mike Goldsmith <MikeGoldsmith@users.noreply.github.com>
- Loading branch information
1 parent
da8c2b3
commit 0793c6e
Showing
7 changed files
with
185 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package assemblers | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type httpEvent struct { | ||
requestId string | ||
request *http.Request | ||
response *http.Response | ||
duration time.Duration | ||
srcIp string | ||
dstIp string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package assemblers | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type httpMatcher struct { | ||
messages *sync.Map | ||
} | ||
|
||
type entry struct { | ||
request *http.Request | ||
requestTimestamp time.Time | ||
|
||
response *http.Response | ||
responseTimestamp time.Time | ||
} | ||
|
||
func newRequestResponseMatcher() httpMatcher { | ||
return httpMatcher{ | ||
messages: &sync.Map{}, | ||
} | ||
} | ||
|
||
func (m *httpMatcher) LoadOrStoreRequest(requestID string, request *http.Request) *entry { | ||
|
||
// check if we already have a response for this request, if yes, return it | ||
if e, ok := m.messages.LoadAndDelete(requestID); ok { | ||
e.(*entry).request = request | ||
e.(*entry).requestTimestamp = time.Now() | ||
return e.(*entry) | ||
} | ||
|
||
// we don't have a response for this request, so store it for later | ||
entry := entry{ | ||
request: request, | ||
requestTimestamp: time.Now(), | ||
} | ||
m.messages.Store(requestID, &entry) | ||
return nil | ||
} | ||
|
||
func (m *httpMatcher) LoadOrStoreResponse(requestID string, response *http.Response) *entry { | ||
|
||
// check if we already have a request for this response, if yes, return it | ||
if e, ok := m.messages.LoadAndDelete(requestID); ok { | ||
e.(*entry).response = response | ||
e.(*entry).responseTimestamp = time.Now() | ||
return e.(*entry) | ||
} | ||
|
||
// we don't have a request for this response, so store it for later | ||
entry := entry{ | ||
response: response, | ||
responseTimestamp: time.Now(), | ||
} | ||
m.messages.Store(requestID, &entry) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.