nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
metrics.cc
Go to the documentation of this file.
1#include "server/metrics.h"
2
3#include <string>
4
5#include "protocol/version.h"
6
7namespace nanommo {
8
9namespace {
10
11prometheus::Histogram::BucketBoundaries TimeBuckets() {
12 return {0.0001, 0.00025, 0.0005, 0.001, 0.0025, 0.005, 0.01, 0.02,
13 0.03, 0.04, 0.05, 0.075, 0.1, 0.25, 0.5, 1};
14}
15
16// A story takes seconds, where a tick takes milliseconds.
17prometheus::Histogram::BucketBoundaries StoryBuckets() {
18 return {0.25, 0.5, 1, 1.5, 2, 3, 4, 5, 7.5, 10};
19}
20
21// A login takes tens of milliseconds, most of it hashing the password, and
22// one that takes over five seconds is one the player's given up on.
23prometheus::Histogram::BucketBoundaries LoginBuckets() {
24 return {0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10};
25}
26
27} // namespace
28
30 : registry_(std::make_shared<prometheus::Registry>()),
31 players_(prometheus::BuildGauge()
32 .Name("nanommo_players_online")
33 .Help("Players connected to the server, by land")
34 .Register(*registry_)),
35 copies_(prometheus::BuildGauge()
36 .Name("nanommo_copies")
37 .Help("Copies of each land the server runs, by land")
38 .Register(*registry_)),
39 tick_seconds_(prometheus::BuildHistogram()
40 .Name("nanommo_tick_duration_seconds")
41 .Help("Time spent in each tick")
42 .Register(*registry_)
43 .Add({}, TimeBuckets())),
44 save_seconds_(prometheus::BuildHistogram()
45 .Name("nanommo_save_duration_seconds")
46 .Help("Time spent saving players, in each tick")
47 .Register(*registry_)
48 .Add({}, TimeBuckets())),
49 bytes_sent_(prometheus::BuildCounter()
50 .Name("nanommo_bytes_sent_total")
51 .Help("Bytes queued to send to clients")
52 .Register(*registry_)
53 .Add({})),
54 messages_received_(prometheus::BuildCounter()
55 .Name("nanommo_messages_received_total")
56 .Help("Messages received from clients")
57 .Register(*registry_)
58 .Add({})),
59 chat_lines_(prometheus::BuildCounter()
60 .Name("nanommo_chat_lines_total")
61 .Help("Lines of chat, said or refused")
62 .Register(*registry_)),
63 stories_(prometheus::BuildCounter()
64 .Name("nanommo_stories_total")
65 .Help("Stories asked for, told, refused or failed")
66 .Register(*registry_)),
67 story_seconds_(prometheus::BuildHistogram()
68 .Name("nanommo_story_duration_seconds")
69 .Help("Time from asking for a story to its telling")
70 .Register(*registry_)
71 .Add({}, StoryBuckets())),
72 dropped_(prometheus::BuildCounter()
73 .Name("nanommo_players_dropped_total")
74 .Help("Players let go of without saving them, by why")
75 .Register(*registry_)),
76 war_kills_(prometheus::BuildCounter()
77 .Name("nanommo_war_kills_total")
78 .Help("Kills in guild wars, handed to the saver")
79 .Register(*registry_)
80 .Add({})) {
81 // The version goes in a label, the way Prometheus gives its own, so that
82 // a query can tell the worlds a deploy has reached from the others.
83 prometheus::BuildGauge()
84 .Name("nanommo_build_info")
85 .Help("The server's version, in its label; always 1")
86 .Register(*registry_)
87 .Add({{"version", std::string(kVersion)}})
88 .Set(1);
89 // At zero from the start, so that the first player dropped counts as one
90 // more than none, which a rate or an increase sees.
91 for (const char* why : {"lease", "trade"}) {
92 dropped_.Add({{"why", why}});
93 }
94}
95
96void Metrics::SetPlayers(const char* land, std::size_t count) {
97 // Add returns the land's gauge if it has one already.
98 players_.Add({{"land", land}}).Set(static_cast<double>(count));
99}
100
101void Metrics::SetCopies(const char* land, std::size_t count) {
102 copies_.Add({{"land", land}}).Set(static_cast<double>(count));
103}
104
105void Metrics::ObserveTick(std::chrono::nanoseconds time) {
106 tick_seconds_.Observe(std::chrono::duration<double>(time).count());
107}
108
109void Metrics::ObserveSave(std::chrono::nanoseconds time) {
110 save_seconds_.Observe(std::chrono::duration<double>(time).count());
111}
112
113void Metrics::AddBytesSent(std::size_t bytes) {
114 bytes_sent_.Increment(static_cast<double>(bytes));
115}
116
117void Metrics::AddMessageReceived() { messages_received_.Increment(); }
118
119void Metrics::AddChatLine(const char* result) {
120 chat_lines_.Add({{"result", result}}).Increment();
121}
122
123void Metrics::AddStory(const char* result) {
124 stories_.Add({{"result", result}}).Increment();
125}
126
127void Metrics::ObserveStory(std::chrono::nanoseconds time) {
128 story_seconds_.Observe(std::chrono::duration<double>(time).count());
129}
130
131void Metrics::AddDropped(const char* why) {
132 dropped_.Add({{"why", why}}).Increment();
133}
134
135void Metrics::AddWarKills(std::size_t count) {
136 war_kills_.Increment(static_cast<double>(count));
137}
138
140 : registry_(std::make_shared<prometheus::Registry>()),
141 seconds_(prometheus::BuildHistogram()
142 .Name("nanommo_login_duration_seconds")
143 .Help("Time to answer each request, by what came of it")
144 .Register(*registry_)) {
145 // Each result is there from the start, at zero, so that a rate of failures
146 // is zero before the first one, rather than nothing.
147 for (const char* result : {"ok", "refused", "failed"}) {
148 seconds_.Add({{"result", result}}, LoginBuckets());
149 }
150 prometheus::BuildGauge()
151 .Name("nanommo_build_info")
152 .Help("The server's version, in its label; always 1")
153 .Register(*registry_)
154 .Add({{"version", std::string(kVersion)}})
155 .Set(1);
156}
157
158void LoginMetrics::ObserveRequest(const char* result,
159 std::chrono::nanoseconds time) {
160 seconds_.Add({{"result", result}}, LoginBuckets())
161 .Observe(std::chrono::duration<double>(time).count());
162}
163
164} // namespace nanommo
void ObserveRequest(const char *result, std::chrono::nanoseconds time)
Definition metrics.cc:158
void AddMessageReceived()
Definition metrics.cc:117
void SetCopies(const char *land, std::size_t count)
How many copies of the land the server runs.
Definition metrics.cc:101
void AddBytesSent(std::size_t bytes)
Definition metrics.cc:113
void AddDropped(const char *why)
Definition metrics.cc:131
void AddChatLine(const char *result)
A line of chat, by what became of it: said, or refused.
Definition metrics.cc:119
void AddStory(const char *result)
Definition metrics.cc:123
void ObserveStory(std::chrono::nanoseconds time)
From when the player asked to when the story was told.
Definition metrics.cc:127
void ObserveSave(std::chrono::nanoseconds time)
Definition metrics.cc:109
void ObserveTick(std::chrono::nanoseconds time)
Definition metrics.cc:105
void SetPlayers(const char *land, std::size_t count)
Definition metrics.cc:96
void AddWarKills(std::size_t count)
Kills in a guild war, which go to the saver, for their wars' scores.
Definition metrics.cc:135
Definition chat.cc:9
Part Name(std::string_view name)
Definition messages.cc:113