aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/monitoring/httpstats.cpp
blob: b097a0d3fc524d2ce2dc2732d56b6b62ab8eb6d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zenhttp/httpstats.h"

#include <zencore/compactbinarybuilder.h>

namespace zen {

HttpStatsService::HttpStatsService() : m_Log(logging::Get("stats"))
{
}

HttpStatsService::~HttpStatsService()
{
}

const char*
HttpStatsService::BaseUri() const
{
	return "/stats";
}

void
HttpStatsService::RegisterHandler(std::string_view Id, IHttpStatsProvider& Provider)
{
	RwLock::ExclusiveLockScope _(m_Lock);
	m_Providers.insert_or_assign(std::string(Id), &Provider);
}

void
HttpStatsService::UnregisterHandler(std::string_view Id, IHttpStatsProvider& Provider)
{
	ZEN_UNUSED(Provider);

	RwLock::ExclusiveLockScope _(m_Lock);
	m_Providers.erase(std::string(Id));
}

void
HttpStatsService::HandleRequest(HttpServerRequest& Request)
{
	using namespace std::literals;

	std::string_view Key = Request.RelativeUri();

	switch (Request.RequestVerb())
	{
		case HttpVerb::kHead:
		case HttpVerb::kGet:
			{
				if (Key.empty())
				{
					CbObjectWriter Cbo;

					Cbo.BeginArray("providers");

					{
						RwLock::SharedLockScope _(m_Lock);
						for (auto& Kv : m_Providers)
						{
							Cbo << Kv.first;
						}
					}

					Cbo.EndArray();

					Request.WriteResponse(HttpResponseCode::OK, Cbo.Save());
				}
				else if (Key[0] == '/')
				{
					Key.remove_prefix(1);
					size_t SlashPos = Key.find_first_of("/?");
					if (SlashPos != std::string::npos)
					{
						Key = Key.substr(0, SlashPos);
					}

					RwLock::SharedLockScope _(m_Lock);
					if (auto It = m_Providers.find(std::string{Key}); It != end(m_Providers))
					{
						return It->second->HandleStatsRequest(Request);
					}
				}
			}

			[[fallthrough]];
		default:
			return;
	}
}

}  // namespace zen