summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/out_datadog/datadog_remap.c
blob: 7599a8f80a229c8ab2546c9c775e64307e8ae063 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- 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 <fluent-bit/flb_info.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_pack.h>

#include "datadog.h"
#include "datadog_remap.h"

const char *ECS_ARN_PREFIX = "arn:aws:ecs:";
const char *ECS_CLUSTER_PREFIX = "cluster/";
const char *ECS_TASK_PREFIX = "task/";

static int dd_remap_append_kv_to_ddtags(const char *key,
                                        const char *val, size_t val_len, flb_sds_t *dd_tags_buf)
{
    flb_sds_t tmp;

    if (flb_sds_len(*dd_tags_buf) != 0) {
        tmp = flb_sds_cat(*dd_tags_buf, FLB_DATADOG_TAG_SEPERATOR, strlen(FLB_DATADOG_TAG_SEPERATOR));
        if (!tmp) {
            flb_errno();
            return -1;
        }
        *dd_tags_buf = tmp;
    }

    tmp = flb_sds_cat(*dd_tags_buf, key, strlen(key));
    if (!tmp) {
            flb_errno();
            return -1;
        }
    *dd_tags_buf = tmp;

    tmp = flb_sds_cat(*dd_tags_buf, ":", 1);
    if (!tmp) {
            flb_errno();
            return -1;
        }
    *dd_tags_buf = tmp;

    tmp = flb_sds_cat(*dd_tags_buf, val, val_len);
    if (!tmp) {
            flb_errno();
            return -1;
        }
    *dd_tags_buf = tmp;

    return 0;
}

/* default remapping: just move the key/val pair under dd_tags */
static int dd_remap_move_to_tags(const char *tag_name,
                                  msgpack_object attr_value, flb_sds_t *dd_tags_buf)
{
    return dd_remap_append_kv_to_ddtags(tag_name, attr_value.via.str.ptr,
                                        attr_value.via.str.size, dd_tags_buf);
}

/* remapping function for container_name */
static int dd_remap_container_name(const char *tag_name,
                                    msgpack_object attr_value, flb_sds_t *dd_tags_buf)
{
    /* remove the first / if present */
    unsigned int adjust;
    flb_sds_t buf = NULL;
    int ret;

    adjust = attr_value.via.str.ptr[0] == '/' ? 1 : 0;
    buf = flb_sds_create_len(attr_value.via.str.ptr + adjust,
                             attr_value.via.str.size - adjust);
    if (!buf) {
        flb_errno();
        return -1;
    }
    ret = dd_remap_append_kv_to_ddtags(tag_name, buf, strlen(buf), dd_tags_buf);
    flb_sds_destroy(buf);
    if (ret < 0) {
        return -1;
    }

    return 0;
}

/* remapping function for ecs_cluster */
static int dd_remap_ecs_cluster(const char *tag_name,
                                 msgpack_object attr_value, flb_sds_t *dd_tags_buf)
{
    flb_sds_t buf = NULL;
    char *cluster_name;
    int ret;

    buf = flb_sds_create_len(attr_value.via.str.ptr, attr_value.via.str.size);
    if (!buf) {
        flb_errno();
        return -1;
    }
    cluster_name = strstr(buf, ECS_CLUSTER_PREFIX);

    if (cluster_name != NULL) {
        cluster_name += strlen(ECS_CLUSTER_PREFIX);
        ret = dd_remap_append_kv_to_ddtags(tag_name, cluster_name, strlen(cluster_name), dd_tags_buf);
        if (ret < 0) {
            flb_sds_destroy(buf);
            return -1;
        }
    }
    else {
        /*
         * here the input is invalid: not in form of "XXXXXXcluster/"cluster-name
         * we preverse the original value under tag "cluster_name".
         */
        ret = dd_remap_append_kv_to_ddtags(tag_name, buf, strlen(buf), dd_tags_buf);
        if (ret < 0) {
            flb_sds_destroy(buf);
            return -1;
        }
    }
    flb_sds_destroy(buf);
    return 0;
}

/* remapping function for ecs_task_definition */
static int dd_remap_ecs_task_definition(const char *tag_name,
                                         msgpack_object attr_value, flb_sds_t *dd_tags_buf)
{
    flb_sds_t buf = NULL;
    char *split;
    int ret;

    buf = flb_sds_create_len(attr_value.via.str.ptr, attr_value.via.str.size);
    if (!buf) {
        flb_errno();
        return -1;
    }
    split = strchr(buf, ':');

    if (split != NULL) {
        ret = dd_remap_append_kv_to_ddtags("task_family", buf, split-buf, dd_tags_buf);
        if (ret < 0) {
            flb_sds_destroy(buf);
            return -1;
        }
        ret = dd_remap_append_kv_to_ddtags("task_version", split+1, strlen(split+1), dd_tags_buf);
        if (ret < 0) {
            flb_sds_destroy(buf);
            return -1;
        }
    }
    else {
        /*
         * here the input is invalid: not in form of task_name:task_version
         * we preverse the original value under tag "ecs_task_definition".
         */
        ret = dd_remap_append_kv_to_ddtags(tag_name, buf, strlen(buf), dd_tags_buf);
        if (ret < 0) {
            flb_sds_destroy(buf);
            return -1;
        }
    }
    flb_sds_destroy(buf);
    return 0;
}

/* remapping function for ecs_task_arn */
static int dd_remap_ecs_task_arn(const char *tag_name,
                                  msgpack_object attr_value, flb_sds_t *dd_tags_buf)
{
    flb_sds_t buf;
    char *remain;
    char *split;
    char *task_arn;
    int ret;

    buf = flb_sds_create_len(attr_value.via.str.ptr, attr_value.via.str.size);
    if (!buf) {
        flb_errno();
        return -1;
    }

    /*
     * if the input is invalid, not in the form of "arn:aws:ecs:region:XXXX"
     * then we won't add the "region" in the dd_tags.
     */
    if ((strlen(buf) > strlen(ECS_ARN_PREFIX)) &&
        (strncmp(buf, ECS_ARN_PREFIX, strlen(ECS_ARN_PREFIX)) == 0)) {

        remain = buf + strlen(ECS_ARN_PREFIX);
        split = strchr(remain, ':');

        if (split != NULL) {
            ret = dd_remap_append_kv_to_ddtags("region", remain, split-remain, dd_tags_buf);
            if (ret < 0) {
                flb_sds_destroy(buf);
                return -1;
            }
        }
    }

    task_arn = strstr(buf, ECS_TASK_PREFIX);
    if (task_arn != NULL) {
        /* parse out the task_arn */
        task_arn += strlen(ECS_TASK_PREFIX);
        ret = dd_remap_append_kv_to_ddtags(tag_name, task_arn, strlen(task_arn), dd_tags_buf);
    }
    else {
        /*
         * if the input is invalid, not in the form of "XXXXXXXXtask/"task-arn
         * then we preverse the original value under tag "task_arn".
         */
        ret = dd_remap_append_kv_to_ddtags(tag_name, buf, strlen(buf), dd_tags_buf);
    }
    flb_sds_destroy(buf);
    if (ret < 0) {
         return -1;
    }

    return 0;
}

/*
 * Statically defines the set of remappings rules in the form of
 * 1) original attr name 2) remapped tag name 3) remapping functions
 * The remapping functions assume the input is valid, and will always
 * produce one or more tags to be added in dd_tags.
 */
const struct dd_attr_tag_remapping remapping[] = {
    {"container_id", "container_id", dd_remap_move_to_tags},
    {"container_name", "container_name", dd_remap_container_name},
    {"container_image", "container_image", dd_remap_move_to_tags},
    {"ecs_cluster", "cluster_name", dd_remap_ecs_cluster},
    {"ecs_task_definition", "ecs_task_definition", dd_remap_ecs_task_definition},
    {"ecs_task_arn", "task_arn", dd_remap_ecs_task_arn}
};

/*
 * Check against dd_attr_tag_remapping to see if a given attributes key/val
 * pair need remapping. The key has to match origin_attr_name, and the val
 * has to be of type string and non-empty.
 * return value is the index of the remapping rule in dd_attr_tag_remapping,
 * or -1 if no need to remap
 */
int dd_attr_need_remapping(const msgpack_object key, const msgpack_object val)
{
    int i;

    if ((val.type != MSGPACK_OBJECT_STR) || (val.via.str.size == 0)) {
        return -1;
    }

    for (i = 0; i < sizeof(remapping) / sizeof(struct dd_attr_tag_remapping); i++) {
        if ((key.via.str.size == strlen(remapping[i].origin_attr_name) &&
             memcmp(key.via.str.ptr,
                    remapping[i].origin_attr_name, key.via.str.size) == 0)) {
            return i;
        }
    }

    return -1;
}