-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement basic request id to trace requests #303
- Loading branch information
1 parent
374a0be
commit 4defb59
Showing
8 changed files
with
166 additions
and
6 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,31 @@ | ||
import { expect } from 'chai'; | ||
import { headerToHaveDataVersion, server } from './common.js'; | ||
import { describe, it } from 'node:test'; | ||
|
||
const X_REQUEST_ID = 'x-request-id'; | ||
|
||
describe('The request id', () => { | ||
it('should be returned when explicitly specified', async () => { | ||
const requestID = 'hardcodedRequestIdInTheTest'; | ||
|
||
await server | ||
.post('/query') | ||
.set(X_REQUEST_ID, requestID) | ||
.send({ action: { type: 'Aggregated' }, filterExpression: { type: 'True' } }) | ||
.expect(200) | ||
.expect(X_REQUEST_ID, requestID); | ||
}); | ||
|
||
it('should be generated when none is specified', async () => { | ||
await server | ||
.post('/query') | ||
.send({ action: { type: 'Aggregated' }, filterExpression: { type: 'True' } }) | ||
.expect(200) | ||
.expect(response => { | ||
const headers = response.headers; | ||
expect(headers).to.have.property(X_REQUEST_ID); | ||
expect(headers[X_REQUEST_ID]).to.be.a('string'); | ||
expect(headers[X_REQUEST_ID]).length.to.be.at.least(1); | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include <Poco/Net/HTTPRequestHandler.h> | ||
#include <Poco/Net/HTTPServerRequest.h> | ||
#include <Poco/Net/HTTPServerResponse.h> | ||
|
||
namespace silo_api { | ||
|
||
constexpr auto REQUEST_ID_HEADER = "X-Request-Id"; | ||
|
||
class RequestIdHandler : public Poco::Net::HTTPRequestHandler { | ||
private: | ||
std::unique_ptr<Poco::Net::HTTPRequestHandler> wrapped_handler; | ||
|
||
public: | ||
explicit RequestIdHandler(Poco::Net::HTTPRequestHandler* wrapped_handler); | ||
|
||
void handleRequest( | ||
Poco::Net::HTTPServerRequest& request, | ||
Poco::Net::HTTPServerResponse& response | ||
) override; | ||
|
||
private: | ||
static std::string getRequestId(Poco::Net::HTTPServerRequest& request); | ||
}; | ||
} // namespace silo_api |
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
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
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,34 @@ | ||
#include "silo_api/request_id_handler.h" | ||
|
||
#include <spdlog/spdlog.h> | ||
#include <boost/uuid/uuid_generators.hpp> | ||
#include <boost/uuid/uuid_io.hpp> | ||
|
||
using boost::uuids::random_generator; | ||
|
||
namespace silo_api { | ||
|
||
RequestIdHandler::RequestIdHandler(Poco::Net::HTTPRequestHandler* wrapped_handler) | ||
: wrapped_handler(wrapped_handler) {} | ||
|
||
void RequestIdHandler::handleRequest( | ||
Poco::Net::HTTPServerRequest& request, | ||
Poco::Net::HTTPServerResponse& response | ||
) { | ||
const auto request_id = getRequestId(request); | ||
response.set(REQUEST_ID_HEADER, request_id); | ||
|
||
wrapped_handler->handleRequest(request, response); | ||
} | ||
|
||
std::string RequestIdHandler::getRequestId(Poco::Net::HTTPServerRequest& request) { | ||
try { | ||
return request.get(REQUEST_ID_HEADER); | ||
} catch (const Poco::NotFoundException& not_found_exception) { | ||
random_generator generator; | ||
const auto request_id = generator(); | ||
return boost::uuids::to_string(request_id); | ||
} | ||
} | ||
|
||
} // namespace silo_api |
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,49 @@ | ||
#include <Poco/Net/HTTPResponse.h> | ||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "silo_api/manual_poco_mocks.test.h" | ||
#include "silo_api/request_id_handler.h" | ||
|
||
using silo_api::RequestIdHandler; | ||
|
||
namespace { | ||
|
||
class MockRequestHandler : public Poco::Net::HTTPRequestHandler { | ||
public: | ||
MOCK_METHOD( | ||
void, | ||
handleRequest, | ||
(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse& response), | ||
() | ||
); | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST(RequestIdHandler, givenNoRequestIdIsSet_thenGeneratesOne) { | ||
auto* wrapped_handler_mock = new MockRequestHandler; | ||
auto under_test = RequestIdHandler(wrapped_handler_mock); | ||
EXPECT_CALL(*wrapped_handler_mock, handleRequest); | ||
|
||
silo_api::test::MockResponse response; | ||
silo_api::test::MockRequest request(response); | ||
under_test.handleRequest(request, response); | ||
|
||
EXPECT_THAT(response.get("X-Request-Id"), ::testing::ContainsRegex("-[A-Za-z0-9]{4}-")); | ||
} | ||
|
||
TEST(RequestIdHandler, givenRequestIdIsSet_thenResponseAlsoContainsIt) { | ||
const std::string request_id_value = "request id value"; | ||
|
||
auto* wrapped_handler_mock = new MockRequestHandler; | ||
auto under_test = RequestIdHandler(wrapped_handler_mock); | ||
EXPECT_CALL(*wrapped_handler_mock, handleRequest); | ||
|
||
silo_api::test::MockResponse response; | ||
silo_api::test::MockRequest request(response); | ||
request.set("X-Request-Id", request_id_value); | ||
under_test.handleRequest(request, response); | ||
|
||
EXPECT_EQ(response.get("X-Request-Id"), request_id_value); | ||
} |