File: internal_metrics.h

package info (click to toggle)
falcosecurity-libs 0.1.1dev%2Bgit20220316.e5c53d64-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,732 kB
  • sloc: cpp: 55,770; ansic: 37,330; makefile: 74; sh: 13
file content (150 lines) | stat: -rw-r--r-- 2,612 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright (C) 2021 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#pragma once
#ifdef GATHER_INTERNAL_STATS
#include <map>
#include <memory>
#include <string>

#define INTERNAL_COUNTER(X) internal_metrics::counter *X

namespace internal_metrics {

class metric;
class registry;
class counter;

class SINSP_PUBLIC metric_name
{
public:
	metric_name(std::string name, std::string description)
	{
		m_name = name;
		m_description = description;
	}

	bool operator<(const metric_name& other) const
	{
		return m_name<other.m_name;
	}

	std::string get_name() const
	{
		return m_name;
	}

	std::string get_description() const
	{
		return m_description;
	}

private:
	std::string m_name;
	std::string m_description;
};

class SINSP_PUBLIC processor
{
public:
	virtual void process(counter& metric) {};
};

class SINSP_PUBLIC metric
{
public:
	virtual ~metric() {}
	virtual void process(processor& metric_processor) = 0;
	virtual void clear() = 0;
};

class SINSP_PUBLIC registry
{
public:
	typedef std::map<metric_name,std::shared_ptr<counter>> metric_map_t;
	typedef metric_map_t::iterator metric_map_iterator_t;

	counter& register_counter(const metric_name& name)
	{
		std::shared_ptr<counter> p;
		p = std::make_shared<counter>();
    	m_metrics[name] = p;
		return *p.get();
	}

	metric_map_t& get_metrics()
	{
		return m_metrics;
	}

	void clear_all_metrics();

private:
	//template<typename T, typename... Args> T& create_metric(const metric_name& name, Args... args)
	//{
	//	if (m_metrics.find(name) == std::end(m_metrics))
	//	{
	//		m_metrics[name] = std::make_shared<T>(args...);
	//	}
	//	return dynamic_cast<T&>(*m_metrics[name]);
	//}

	metric_map_t m_metrics;

};


class SINSP_PUBLIC counter : public metric
{
public:
	~counter();
	counter();

	void increment()
	{
		m_value++;
	}

	void decrement()
	{
		m_value--;
	}

	void clear()
	{
		m_value = 0;
	}

	const uint64_t get_value()
	{
		return m_value;
	}

	void process(processor& metric_processor)
	{
		metric_processor.process(*this);
	}

private:

	uint64_t m_value;
};

}
#else
#define INTERNAL_COUNTER(X)
#endif // GATHER_INTERNAL_STATS