Skip to content

Commit ff2b6df

Browse files
committed
feat(metrics): Completed metricsserver.
1 parent 276a538 commit ff2b6df

File tree

17 files changed

+331
-54
lines changed

17 files changed

+331
-54
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ SET(libsrc
2626
./source/sslConnection/sslConnection.cpp
2727
./source/metricsServer/metricsServer.cpp
2828
./source/metricBase/metricBase.cpp
29-
./source/counterMetric/counterMetric.cpp)
29+
./source/counterMetric/counterMetric.cpp
30+
./source/histogramMetric/histogramMetric.cpp
31+
./source/gaugeMetric/gaugeMetric.cpp)
3032

3133
SET(libhead
3234
./source/socketBase/socketBase.h
@@ -42,6 +44,8 @@ SET(libhead
4244
./source/metricsServer/metricsServer.h
4345
./source/metricBase/metricBase.h
4446
./source/counterMetric/counterMetric.h
47+
./source/histogramMetric/histogramMetric.h
48+
./source/gaugeMetric/gaugeMetric.h
4549
./source/templates.h)
4650

4751
ADD_LIBRARY(kleinsHTTP-shared SHARED "${libsrc}")

examples/simpleHelloWorld/main.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#include "../../libkleinsHTTP.h"
22

3-
int main(){
4-
kleins::httpServer server;
3+
int main() {
4+
kleins::httpServer server;
5+
server.startMetricsServer(9090);
56

6-
server.printVersion();
7+
server.printVersion();
78

8-
server.addSocket(new kleins::tcpSocket("0.0.0.0",8080));
9+
server.addSocket(new kleins::tcpSocket("0.0.0.0", 8080));
910

10-
server.on(kleins::httpMethod::GET,"/",[](kleins::httpParser* data){
11-
data->respond("200",{},"Hello!");
12-
});
11+
server.on(kleins::httpMethod::GET, "/", [](kleins::httpParser* data) { data->respond("200", {}, "Hello!"); });
1312

14-
for(;;) {
15-
usleep(90000);
16-
}
13+
server.on(kleins::httpMethod::GET, "/hi", [](kleins::httpParser* data) { data->respond("200", {}, "Hi!"); });
14+
15+
for (;;) {
16+
usleep(90000);
17+
}
1718
}

preheader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#define SINGLE_HEADER
22

33
namespace kleins {
4+
namespace metrics {
5+
class metricBase;
6+
class counterMetric;
7+
class histogramMetric;
8+
class gaugeMetric;
9+
class metricServer;
10+
};
411
class packet;
512
class connectionBase;
613
class tcpConnection;
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
#include "counterMetric.h"
22

3-
4-
kleins::metrics::counterMetric::counterMetric(/* args */)
5-
{
3+
kleins::metrics::counterMetric::counterMetric(const char* name, const char* help) {
4+
setName(name);
5+
setHelp(help);
66
}
77

8-
kleins::metrics::counterMetric::~counterMetric()
9-
{
8+
kleins::metrics::counterMetric::~counterMetric() {
109
}
1110

1211
const char* kleins::metrics::counterMetric::getType() {
13-
return "counter";
12+
return "counter";
1413
}
1514

1615
std::unique_ptr<char*> kleins::metrics::counterMetric::construct() {
17-
char* targetBuffer = new char[4096];
16+
char* targetBuffer = new char[4096];
1817

19-
snprintf(targetBuffer,4096,"# HELP %s %s\r\n# TYPE %s %s\r\n%s %i",nameString, helpString, nameString, getType(), nameString, counterValue);
18+
snprintf(targetBuffer, 4096, "# HELP %s %s\r\n# TYPE %s %s\r\n%s %i\r\n", nameString, helpString, nameString, getType(), nameString, counterValue);
2019

21-
return std::make_unique<char*>(targetBuffer);
20+
return std::make_unique<char*>(targetBuffer);
2221
};
2322

2423
uint64_t kleins::metrics::counterMetric::get() {
25-
return counterValue;
24+
return counterValue;
2625
}
2726

28-
void kleins::metrics::counterMetric::set(uint64_t value){
29-
assert(("Attemped to decrease counter value",value<counterValue));
27+
void kleins::metrics::counterMetric::set(uint64_t value) {
28+
assert(("Attemped to decrease counter value", value < counterValue));
3029

31-
counterValue = value;
30+
counterValue = value;
3231
}
3332

34-
void kleins::metrics::counterMetric::inc(uint64_t value = 1){
35-
counterValue += value;
33+
void kleins::metrics::counterMetric::inc(uint64_t value) {
34+
counterValue += value;
3635
}
3736

3837
void kleins::metrics::counterMetric::reset() {
39-
counterValue = 0;
38+
counterValue = 0;
4039
}

source/counterMetric/counterMetric.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef COUNTERMETRIC_H
22
#define COUNTERMETRIC_H
33

4-
#include <stdio.h>
54
#include <cassert>
5+
#include <stdio.h>
66

77
#ifndef SINGLE_HEADER
88
#include "../metricBase/metricBase.h"
@@ -12,27 +12,25 @@ namespace kleins {
1212

1313
namespace metrics {
1414

15-
class counterMetric : public metricBase
16-
{
15+
class counterMetric : public metricBase {
1716
private:
18-
uint64_t counterValue = 0;
17+
uint64_t counterValue = 0;
18+
1919
public:
20-
counterMetric(/* args */);
21-
~counterMetric();
20+
counterMetric(const char* name, const char* help);
21+
~counterMetric();
2222

23-
uint64_t get();
24-
void set(uint64_t value);
25-
void inc(uint64_t value = 1);
26-
void reset();
23+
uint64_t get();
24+
void set(uint64_t value);
25+
void inc(uint64_t value = 1);
26+
void reset();
2727

28-
virtual const char* getType();
28+
virtual const char* getType();
2929

30-
virtual std::unique_ptr<char*> construct() = 0;
30+
virtual std::unique_ptr<char*> construct();
3131
};
3232

33-
34-
35-
}
33+
} // namespace metrics
3634

3735
} // namespace kleins
3836

source/gaugeMetric/gaugeMetric.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "gaugeMetric.h"
2+
3+
kleins::metrics::gaugeMetric::gaugeMetric(const char* name, const char* help) {
4+
setName(name);
5+
setHelp(help);
6+
}
7+
8+
kleins::metrics::gaugeMetric::~gaugeMetric() {
9+
}
10+
11+
const char* kleins::metrics::gaugeMetric::getType() {
12+
return "gauge";
13+
}
14+
15+
std::unique_ptr<char*> kleins::metrics::gaugeMetric::construct() {
16+
char* targetBuffer = new char[4096];
17+
18+
snprintf(targetBuffer, 4096, "# HELP %s %s\r\n# TYPE %s %s\r\n%s %i\r\n", nameString, helpString, nameString, getType(), nameString, counterValue);
19+
20+
return std::make_unique<char*>(targetBuffer);
21+
};
22+
23+
uint64_t kleins::metrics::gaugeMetric::get() {
24+
return counterValue;
25+
}
26+
27+
void kleins::metrics::gaugeMetric::set(uint64_t value) {
28+
counterValue = value;
29+
}

source/gaugeMetric/gaugeMetric.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef GAUGEMETRIC_H
2+
#define GAUGEMETRIC_H
3+
4+
#include <cassert>
5+
#include <stdio.h>
6+
7+
#ifndef SINGLE_HEADER
8+
#include "../metricBase/metricBase.h"
9+
#endif
10+
11+
namespace kleins {
12+
13+
namespace metrics {
14+
15+
class gaugeMetric : public metricBase {
16+
private:
17+
uint64_t counterValue = 0;
18+
19+
public:
20+
gaugeMetric(const char* name, const char* help);
21+
~gaugeMetric();
22+
23+
uint64_t get();
24+
void set(uint64_t value);
25+
26+
virtual const char* getType();
27+
28+
virtual std::unique_ptr<char*> construct();
29+
};
30+
31+
} // namespace metrics
32+
33+
} // namespace kleins
34+
35+
#endif
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "histogramMetric.h"
2+
3+
uint64_t kleins::metrics::counterBucketMetricData::get() {
4+
return counterValue;
5+
}
6+
7+
void kleins::metrics::counterBucketMetricData::set(uint64_t value) {
8+
assert(("Attemped to decrease counter value", value < counterValue));
9+
10+
counterValue = value;
11+
}
12+
13+
void kleins::metrics::counterBucketMetricData::inc(uint64_t value) {
14+
counterValue += value;
15+
}
16+
17+
void kleins::metrics::counterBucketMetricData::reset() {
18+
counterValue = 0;
19+
}
20+
21+
kleins::metrics::histogramMetric::histogramMetric(const char* name, const char* help) {
22+
setName(name);
23+
setHelp(help);
24+
}
25+
26+
kleins::metrics::histogramMetric::~histogramMetric() {
27+
}
28+
29+
const char* kleins::metrics::histogramMetric::getType() {
30+
return "counter";
31+
}
32+
33+
void kleins::metrics::histogramMetric::addBucket(const char* bucketName) {
34+
counterValues.insert(std::make_pair(std::string(bucketName), new counterBucketMetricData));
35+
}
36+
37+
kleins::metrics::counterBucketMetricData* kleins::metrics::histogramMetric::operator[](std::string& input) {
38+
for (auto x : counterValues) {
39+
if (x.first == input) {
40+
return x.second;
41+
}
42+
}
43+
assert("A Bucket was accessed that never existed");
44+
return 0;
45+
}
46+
47+
kleins::metrics::counterBucketMetricData* kleins::metrics::histogramMetric::operator[](const char* input) {
48+
for (auto x : counterValues) {
49+
if (x.first == input) {
50+
return x.second;
51+
}
52+
}
53+
assert("A Bucket was accessed that never existed");
54+
return 0;
55+
}
56+
57+
std::unique_ptr<char*> kleins::metrics::histogramMetric::construct() {
58+
std::stringstream ss;
59+
ss << "# HELP " << nameString << ' ' << helpString << "\r\n"
60+
<< "# TYPE " << nameString << ' ' << getType() << "\r\n";
61+
62+
for (auto x : counterValues) {
63+
ss << nameString << '{' << x.first << "} " << x.second->get() << "\r\n";
64+
}
65+
int totalLength = ss.str().length();
66+
67+
char* outputBuffer = new char[totalLength];
68+
std::memcpy(outputBuffer, ss.str().c_str(), totalLength);
69+
70+
return std::make_unique<char*>(outputBuffer);
71+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef HISTOGRAMMETRIC_H
2+
#define HISTOGRAMMETRIC_H
3+
4+
#include <cassert>
5+
#include <cstring>
6+
#include <map>
7+
#include <sstream>
8+
#include <stdio.h>
9+
#include <string>
10+
11+
#ifndef SINGLE_HEADER
12+
#include "../metricBase/metricBase.h"
13+
#endif
14+
15+
namespace kleins {
16+
17+
namespace metrics {
18+
19+
class counterBucketMetricData {
20+
private:
21+
uint64_t counterValue = 0;
22+
23+
public:
24+
uint64_t get();
25+
void set(uint64_t value);
26+
void inc(uint64_t value = 1);
27+
void reset();
28+
};
29+
30+
class histogramMetric : public metricBase {
31+
private:
32+
std::map<std::string, counterBucketMetricData*> counterValues;
33+
34+
public:
35+
histogramMetric(const char* name, const char* help);
36+
~histogramMetric();
37+
38+
counterBucketMetricData* operator[](std::string& input);
39+
counterBucketMetricData* operator[](const char* input);
40+
41+
void addBucket(const char* bucketName);
42+
43+
virtual const char* getType();
44+
45+
virtual std::unique_ptr<char*> construct();
46+
};
47+
48+
} // namespace metrics
49+
50+
} // namespace kleins
51+
52+
#endif

source/httpParser/httpParser.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ bool kleins::httpParser::parse() {
7171
if (search != functionTable.end()) {
7272
search->second(this);
7373
} else {
74+
if (server->metric_notfound) {
75+
server->metric_notfound->inc();
76+
}
7477
connsocket->sendData(
7578
"HTTP/1.0 404\r\ncontent-type:text/html; "
76-
"charset=UTF-8\r\n\r\n<html><head></head><body>Not "
79+
"charset=UTF-8\r\nContent-Length: 51\r\n\r\n<html><head></head><body>Not "
7780
"found</body></html>\r\n",
78-
104);
81+
127);
7982
}
8083

8184
return true;

0 commit comments

Comments
 (0)