-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgotel.cpp
196 lines (170 loc) · 4.49 KB
/
pgotel.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-------------------------------------------------------------------------
*
* pgotel.cc
* POC to interact with OpenTelemetry
*
* Copyright (c) 1996-2024, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pgotel/pgotel.cpp
*
*-------------------------------------------------------------------------
*/
#include "metrics_grpc.h"
#ifdef __cplusplus
extern "C"
{
#endif
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/jsonb.h"
#include "storage/ipc.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pgotel_counter);
void _PG_init(void);
/* GUC variables */
static bool pgotel_enabled = false;
static int pgotel_interval = 2000;
static int pgotel_timeout = 500;
static char *pgotel_endpoint = NULL;
Datum
pgotel_counter(PG_FUNCTION_ARGS)
{
char *counter_name = PG_ARGISNULL(0) ? NULL : text_to_cstring(PG_GETARG_TEXT_PP(0));
float8 value = PG_ARGISNULL(1) ? -1 : PG_GETARG_FLOAT8(1);
Jsonb *labels = PG_ARGISNULL(2) ? NULL : PG_GETARG_JSONB_P(2);
std::map<std::string, std::string> labels_map;
if (!counter_name)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("counter name cannot be NULL")));
if (value < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("counter value cannot be negative")));
if (labels == NULL && PG_NARGS() == 3)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("labels cannot be NULL")));
/* iterate over the labels JSONB to include key/values to the C++ map data structure */
if (labels != NULL)
{
JsonbIterator *it = JsonbIteratorInit(&labels->root);
JsonbValue v;
JsonbIteratorToken type;
while ((type = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
{
if (type == WJB_KEY)
{
char *key = pnstrdup(v.val.string.val, v.val.string.len);
type = JsonbIteratorNext(&it, &v, true);
if (type == WJB_VALUE)
{
char *val = pnstrdup(v.val.string.val, v.val.string.len);
elog(DEBUG1, "Label: %s = %s", key, val);
labels_map[key] = val;
}
}
}
}
elog(DEBUG1, "labels_map size: %d", (int) labels_map.size());
if (labels != NULL && (int) labels_map.size() == 0)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid labels JSONB")));
/* Send the counter to the OTEL-Collector */
pgotel::Counter(counter_name, value, labels_map);
PG_RETURN_NULL();
}
static void
pgotel_assign_interval_guc(int newval, void *extra)
{
elog(DEBUG1, "pgotel_interval: %d", newval);
elog(DEBUG1, "pgotel_timeout: %d", pgotel_timeout);
pgotel::RestartMetrics(pgotel_endpoint,
std::chrono::milliseconds(newval),
std::chrono::milliseconds(pgotel_timeout));
}
static void
pgotel_assign_timeout_guc(int newval, void *extra)
{
elog(DEBUG1, "pgotel_interval: %d", pgotel_interval);
elog(DEBUG1, "pgotel_timeout: %d", newval);
pgotel::RestartMetrics(pgotel_endpoint,
std::chrono::milliseconds(pgotel_interval),
std::chrono::milliseconds(newval));
}
static void
load_params(void)
{
DefineCustomStringVariable("pgotel.endpoint",
"Endpoint of the OTEL-Collector",
NULL,
&pgotel_endpoint,
"localhost:4317",
PGC_USERSET,
0,
NULL,
NULL,
NULL);
/* Enable/Disable */
DefineCustomBoolVariable("pgotel.enabled",
"Enable/Disable Send Logs to OTEL-Collector",
NULL,
&pgotel_enabled,
false,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgotel.interval",
"Interval to send metrics to OTEL-Collector",
NULL,
&pgotel_interval,
2000,
1000,
INT_MAX,
PGC_USERSET,
0,
NULL,
pgotel_assign_interval_guc,
NULL);
DefineCustomIntVariable("pgotel.timeout",
"Timeout to send metrics to OTEL-Collector",
NULL,
&pgotel_timeout,
500,
100,
INT_MAX,
PGC_USERSET,
0,
NULL,
pgotel_assign_timeout_guc,
NULL);
}
/*
* _PG_init
* Entry point loading hooks
*/
void
_PG_init(void)
{
load_params();
elog(DEBUG1, "endpoint: %s", pgotel_endpoint);
pgotel::InitMetrics(pgotel_endpoint,
std::chrono::milliseconds(pgotel_interval),
std::chrono::milliseconds(pgotel_timeout));
}
/*
* _PG_fini
* Exit point unloading hooks
*/
void
_PG_fini(void)
{
pgotel::CleanupMetrics();
}
/* declaration out of file scope */
StaticAssertDecl(true, "declaration assert test");
#ifdef __cplusplus
}
#endif