diff options
Diffstat (limited to 'src/zenhttp/httpserver.cpp')
| -rw-r--r-- | src/zenhttp/httpserver.cpp | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/src/zenhttp/httpserver.cpp b/src/zenhttp/httpserver.cpp index e529fb76e..085275195 100644 --- a/src/zenhttp/httpserver.cpp +++ b/src/zenhttp/httpserver.cpp @@ -26,6 +26,7 @@ #include <zencore/testing.h> #include <zencore/thread.h> #include <zenhttp/packageformat.h> +#include <zentelemetry/otlptrace.h> #include <charconv> #include <mutex> @@ -462,7 +463,7 @@ HttpService::HandlePackageRequest(HttpServerRequest& HttpServiceRequest) ////////////////////////////////////////////////////////////////////////// -HttpServerRequest::HttpServerRequest() +HttpServerRequest::HttpServerRequest(HttpService& Service) : m_BaseUri(Service.BaseUri()) { } @@ -896,7 +897,7 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request) // First try new-style matcher routes - for (const auto& Handler : m_MatcherEndpoints) + for (const MatcherEndpoint& Handler : m_MatcherEndpoints) { if ((Handler.Verbs & Verb) == Verb) { @@ -965,6 +966,16 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request) if (IsMatch && UriPos == UriLen) { +#if ZEN_WITH_OTEL + if (otel::Span* ActiveSpan = otel::Span::GetCurrentSpan()) + { + ExtendableStringBuilder<128> RoutePath; + RoutePath.Append(Request.BaseUri()); + RoutePath.Append(Handler.Pattern); + ActiveSpan->AddAttribute("http.route"sv, RoutePath.ToView()); + } +#endif + RouterRequest.m_CapturedSegments = std::move(CapturedSegments); Handler.Handler(RouterRequest); @@ -979,6 +990,16 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request) { if ((Handler.Verbs & Verb) == Verb && regex_match(begin(Uri), end(Uri), RouterRequest.m_Match, Handler.RegEx)) { +#if ZEN_WITH_OTEL + if (otel::Span* ActiveSpan = otel::Span::GetCurrentSpan()) + { + ExtendableStringBuilder<128> RoutePath; + RoutePath.Append(Request.BaseUri()); + RoutePath.Append(Handler.Pattern); + ActiveSpan->AddAttribute("http.route"sv, RoutePath.ToView()); + } +#endif + Handler.Handler(RouterRequest); return true; // Route matched @@ -1277,9 +1298,17 @@ TEST_CASE("http.common") { using namespace std::literals; + struct TestHttpService : public HttpService + { + TestHttpService() = default; + + virtual const char* BaseUri() const override { return "/test"; } + virtual void HandleRequest(HttpServerRequest& HttpServiceRequest) override { ZEN_UNUSED(HttpServiceRequest); } + }; + struct TestHttpServerRequest : public HttpServerRequest { - TestHttpServerRequest(std::string_view Uri) { m_Uri = Uri; } + TestHttpServerRequest(HttpService& Service, std::string_view Uri) : HttpServerRequest(Service) { m_Uri = Uri; } virtual IoBuffer ReadPayload() override { return IoBuffer(); } virtual void WriteResponse(HttpResponseCode ResponseCode, HttpContentType ContentType, std::span<IoBuffer> Blobs) override { @@ -1308,6 +1337,8 @@ TEST_CASE("http.common") HandledA = HandledAA = false; }; + TestHttpService Service; + HttpRequestRouter r; r.AddPattern("a", "([[:alpha:]]+)"); r.RegisterRoute( @@ -1328,7 +1359,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"abc"sv}; + TestHttpServerRequest req(Service, "abc"sv); r.HandleRequest(req); CHECK(HandledA); CHECK(!HandledAA); @@ -1338,7 +1369,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"abc/def"sv}; + TestHttpServerRequest req{Service, "abc/def"sv}; r.HandleRequest(req); CHECK(!HandledA); CHECK(HandledAA); @@ -1349,14 +1380,14 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"123"sv}; + TestHttpServerRequest req{Service, "123"sv}; r.HandleRequest(req); CHECK(!HandledA); } { Reset(); - TestHttpServerRequest req{"a123"sv}; + TestHttpServerRequest req{Service, "a123"sv}; r.HandleRequest(req); CHECK(!HandledA); } @@ -1374,6 +1405,7 @@ TEST_CASE("http.common") Captures.clear(); }; + TestHttpService Service; HttpRequestRouter r; r.AddMatcher("a", [](std::string_view In) -> bool { return In.length() % 2 == 0; }); r.AddMatcher("b", [](std::string_view In) -> bool { return In.length() % 3 == 0; }); @@ -1408,7 +1440,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"ab"sv}; + TestHttpServerRequest req{Service, "ab"sv}; r.HandleRequest(req); CHECK(HandledA); CHECK(!HandledAA); @@ -1420,7 +1452,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"ab/def"sv}; + TestHttpServerRequest req{Service, "ab/def"sv}; r.HandleRequest(req); CHECK(!HandledA); CHECK(!HandledAA); @@ -1432,7 +1464,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"ab/and/def"sv}; + TestHttpServerRequest req{Service, "ab/and/def"sv}; r.HandleRequest(req); CHECK(!HandledA); CHECK(!HandledAA); @@ -1445,7 +1477,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"123"sv}; + TestHttpServerRequest req{Service, "123"sv}; r.HandleRequest(req); CHECK(!HandledA); CHECK(!HandledAA); @@ -1454,7 +1486,7 @@ TEST_CASE("http.common") { Reset(); - TestHttpServerRequest req{"a123"sv}; + TestHttpServerRequest req{Service, "a123"sv}; r.HandleRequest(req); CHECK(HandledA); CHECK(!HandledAA); |