-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpt_json.c
177 lines (140 loc) · 3.68 KB
/
pt_json.c
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
/*-------------------------------------------------------------------------
*
* pt_json.c
* For building the required json structure for telemetry.
*
* IDENTIFICATION
* contrib/percona_pg_telemetry/pt_json.c
*
*-------------------------------------------------------------------------
*/
#include "pt_json.h"
#include "percona_pg_telemetry.h"
#include <sys/stat.h>
/* Local functions */
static char *json_escape_str(char *str);
/*
* Escapes a JSON string to avoid malformation of a json value. Returns
* a palloced string that caller must pfree.
*/
char *
json_escape_str(char *str)
{
int len;
int maxlen;
char *str_escaped;
char *s;
if (str == NULL)
return NULL;
len = strlen(str);
maxlen = (len > 0) ? len * 2 : 1;
/* Max we'd need twice the space. */
str_escaped = (char *) palloc(maxlen);
s = str_escaped;
for (int i = 0; i < len; i++)
{
/* Escape the quote and backslash characters. */
if (str[i] == '"' || str[i] == '\\')
*s++ = '\\';
*s++ = str[i];
}
/* Ensure that we always end up with a string value. */
*s = '\0';
return str_escaped;
}
/*
* Construct a JSON block for writing. The function created json segment based
* on flags argument and provided key and value. json_file_indent is used
* to keep track of the indentation level. Result is written to the provided buffer.
*/
char *
construct_json_block(char *buf, size_t buf_sz, char *key, char *raw_value, int flags, int *json_file_indent)
{
char *value = NULL;
char str[2048] = {0};
char fmt_str[2048] = {0};
char comma = (flags & PT_JSON_LAST_ELEMENT) ? '\0' : ',';
/* Make the string empty so that we can always concat. */
buf[0] = '\0';
if (raw_value)
value = json_escape_str(raw_value);
if (flags & PT_JSON_KEY)
snprintf(str, sizeof(str), "\"%s\": ", key);
if (flags & PT_JSON_OBJECT_START)
{
strlcat(str, "{", sizeof(str));
PT_FORMAT_JSON(fmt_str, sizeof(fmt_str), str, (*json_file_indent));
strlcat(buf, fmt_str, buf_sz);
(*json_file_indent)++;
}
if (flags & PT_JSON_VALUE)
{
char v[2048] = {0};
snprintf(v, sizeof(v), "\"%s\"%c", value, comma);
strlcat(str, v, sizeof(str));
PT_FORMAT_JSON(fmt_str, sizeof(fmt_str), str, (*json_file_indent));
strlcat(buf, fmt_str, buf_sz);
}
if (flags & PT_JSON_ARRAY_START)
{
strlcat(str, "[", sizeof(str));
PT_FORMAT_JSON(fmt_str, sizeof(fmt_str), str, (*json_file_indent));
strlcat(buf, fmt_str, buf_sz);
(*json_file_indent)++;
}
/* Value is not an array so we can close the block. */
if (flags & PT_JSON_ARRAY_END)
{
char closing[3] = {']', comma, '\0'};
(*json_file_indent)--;
PT_FORMAT_JSON(fmt_str, sizeof(fmt_str), closing, (*json_file_indent));
strlcat(buf, fmt_str, buf_sz);
}
/* Value is not an array so we can close the block. */
if (flags & PT_JSON_OBJECT_END)
{
char closing[3] = {'}', comma, '\0'};
(*json_file_indent)--;
PT_FORMAT_JSON(fmt_str, sizeof(fmt_str), closing, (*json_file_indent));
strlcat(buf, fmt_str, buf_sz);
}
if (value)
pfree(value);
return buf;
}
/*
* Open telemetry file in the given mode.
*/
FILE *
open_telemetry_file(char *filename, char *mode)
{
FILE *fp;
fp = fopen(filename, mode);
if (fp == NULL)
{
ereport(LOG,
(errcode_for_file_access(),
errmsg("Could not open file %s for writing.", filename)));
PT_WORKER_EXIT(PT_FILE_ERROR);
}
return fp;
}
/*
* Write data to telemetry file.
*/
void
write_telemetry_file(FILE *fp, char *data)
{
int len;
int bytes_written;
len = strlen(data);
bytes_written = fwrite(data, 1, len, fp);
if (len != bytes_written)
{
ereport(LOG,
(errcode_for_file_access(),
errmsg("Could not write to json file.")));
fclose(fp);
PT_WORKER_EXIT(PT_FILE_ERROR);
}
}