summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/out_influxdb/influxdb_bulk.c
blob: 2542ee3c428bdbf0d6d1148346ea357d530650a5 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <fluent-bit.h>
#include "influxdb.h"
#include "influxdb_bulk.h"

static const uint64_t ONE_BILLION = 1000000000;

static int influxdb_escape(char *out, const char *str, int size, bool quote) {
    int out_size = 0;
    int i;
    for (i = 0; i < size; ++i) {
        char ch = str[i];
        if (quote ? (ch == '"' || ch == '\\') : (isspace(ch) || ch == ',' || ch == '=')) {
            out[out_size++] = '\\';
        } else if (ch == '\\') {
            out[out_size++] = '\\';
        }
        out[out_size++] = ch;
    }
    return out_size;
}

static int influxdb_bulk_buffer(struct influxdb_bulk *bulk, int required)
{
    int new_size;
    int available;
    char *ptr;

    available = (bulk->size - bulk->len);
    if (available < required) {
        new_size = bulk->size + available + required + INFLUXDB_BULK_CHUNK;
        ptr = flb_realloc(bulk->ptr, new_size);
        if (!ptr) {
            flb_errno();
            return -1;
        }
        bulk->ptr  = ptr;
        bulk->size = new_size;
    }

    return 0;
}

struct influxdb_bulk *influxdb_bulk_create()
{
    struct influxdb_bulk *b;

    b = flb_malloc(sizeof(struct influxdb_bulk));
    if (!b) {
        perror("calloc");
        return NULL;
    }

    b->ptr = flb_malloc(INFLUXDB_BULK_CHUNK);
    if (!b->ptr) {
        perror("malloc");
        flb_free(b);
        return NULL;
    }

    b->size = INFLUXDB_BULK_CHUNK;
    b->len  = 0;

    return b;
}

void influxdb_bulk_destroy(struct influxdb_bulk *bulk)
{
    if (bulk->size > 0) {
        flb_free(bulk->ptr);
    }
    flb_free(bulk);
}

int influxdb_bulk_append_header(struct influxdb_bulk *bulk,
                                const char *tag, int tag_len,
                                uint64_t seq_n, const char *seq, int seq_len)
{
    int ret;
    int required;

    required = tag_len + 1 + seq_len + 1 + 32;

    /* Make sure we have enough space */
    ret = influxdb_bulk_buffer(bulk, required);
    if (ret != 0) {
        return -1;
    }

    /* Tag, sequence and final space */
    memcpy(bulk->ptr + bulk->len, tag, tag_len);
    bulk->len += tag_len;

    if (seq_len != 0) {
        bulk->ptr[bulk->len] = ',';
        bulk->len++;

        /* Sequence number */
        memcpy(bulk->ptr + bulk->len, seq, seq_len);
        bulk->len += seq_len;

        bulk->ptr[bulk->len] = '=';
        bulk->len++;

        ret = snprintf(bulk->ptr + bulk->len, 32, "%" PRIu64, seq_n);
        bulk->len += ret;
    }

    /* Add a NULL byte for debugging purposes */
    bulk->ptr[bulk->len] = '\0';

    return 0;
}

int influxdb_bulk_append_kv(struct influxdb_bulk *bulk,
                            const char *key, int k_len,
                            const char *val, int v_len,
                            int quote)
{
    int ret;
    int required;

    /* Reserve double space for keys and values in case of escaping */
    required = k_len * 2 + 1 + v_len * 2 + 1 + 1;
    if (quote) {
        required += 2;
    }

    /* Make sure we have enough space */
    ret = influxdb_bulk_buffer(bulk, required);
    if (ret != 0) {
        return -1;
    }

    if (bulk->len > 0) {
        bulk->ptr[bulk->len] = ',';
        bulk->len++;
    }

    /* key */
    bulk->len += influxdb_escape(bulk->ptr + bulk->len, key, k_len, false);

    /* separator */
    bulk->ptr[bulk->len] = '=';
    bulk->len++;

    /* value */
    if (quote) {
        bulk->ptr[bulk->len] = '"';
        bulk->len++;
    }
    bulk->len += influxdb_escape(bulk->ptr + bulk->len, val, v_len, quote);
    if (quote) {
        bulk->ptr[bulk->len] = '"';
        bulk->len++;
    }

    /* Add a NULL byte for debugging purposes */
    bulk->ptr[bulk->len] = '\0';

    return 0;
};

int influxdb_bulk_append_timestamp(struct influxdb_bulk *bulk,
                                   struct flb_time *t)
{
    int ret;
    int len;
    uint64_t timestamp;

    /* Make sure we have enough space */
    ret = influxdb_bulk_buffer(bulk, 128);
    if (ret != 0) {
        return -1;
    }

    /* Timestamp is in Nanoseconds */
    timestamp = (t->tm.tv_sec * ONE_BILLION) + t->tm.tv_nsec;
    len = snprintf(bulk->ptr + bulk->len, 127, " %" PRIu64, timestamp);
    if (len == -1) {
        return -1;
    }
    bulk->len += len;
    bulk->ptr[bulk->len] = '\0';

    return 0;
};

int influxdb_bulk_append_bulk(struct influxdb_bulk *bulk_to,
                              struct influxdb_bulk *bulk_from,
                              char separator)
{
    if (influxdb_bulk_buffer(bulk_to, bulk_from->len + 2) != 0) {
        return -1;
    }

    if (bulk_to->len > 0) {
        bulk_to->ptr[bulk_to->len] = separator;
        bulk_to->len += 1;
    }

    memcpy(bulk_to->ptr + bulk_to->len,
           bulk_from->ptr, bulk_from->len * sizeof(char));
    bulk_to->len += bulk_from->len;

    /* Add a NULL byte for always terminating with NULL */
    bulk_to->ptr[bulk_to->len] = '\0';

    return 0;
};