static gboolean afamqp_worker_publish(AMQPDestDriver *self, LogMessage *msg) { gint pos = 0, ret; amqp_table_t table; amqp_basic_properties_t props; gboolean success = TRUE; SBGString *routing_key = sb_gstring_acquire(); SBGString *body = sb_gstring_acquire(); amqp_bytes_t body_bytes = amqp_cstring_bytes(""); gpointer user_data[] = { &self->entries, &pos, &self->max_entries }; value_pairs_foreach(self->vp, afamqp_vp_foreach, msg, self->seq_num, &self->template_options, user_data); table.num_entries = pos; table.entries = self->entries; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG | AMQP_BASIC_HEADERS_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = self->persistent; props.headers = table; log_template_format(self->routing_key_template, msg, NULL, LTZ_LOCAL, self->seq_num, NULL, sb_gstring_string(routing_key)); if (self->body_template) { log_template_format(self->body_template, msg, NULL, LTZ_LOCAL, self->seq_num, NULL, sb_gstring_string(body)); body_bytes = amqp_cstring_bytes(sb_gstring_string(body)->str); } ret = amqp_basic_publish(self->conn, 1, amqp_cstring_bytes(self->exchange), amqp_cstring_bytes(sb_gstring_string(routing_key)->str), 0, 0, &props, body_bytes); sb_gstring_release(routing_key); sb_gstring_release(body); if (ret < 0) { msg_error("Network error while inserting into AMQP server", evt_tag_str("driver", self->super.super.super.id), evt_tag_int("time_reopen", self->super.time_reopen), NULL); success = FALSE; } while (--pos >= 0) { amqp_bytes_free(self->entries[pos].key); amqp_bytes_free(self->entries[pos].value.value.bytes); } return success; }
void testcase(const gchar *scope, const gchar *exclude, const gchar *expected, GPtrArray *transformers) { ValuePairs *vp; GList *vp_keys_list = NULL; GString *vp_keys; LogMessage *msg = create_message(); gpointer args[2]; gboolean test_key_found = FALSE; vp_keys = g_string_sized_new(0); vp = value_pairs_new(); value_pairs_add_scope(vp, scope); if (exclude) value_pairs_add_glob_pattern(vp, exclude, FALSE); value_pairs_add_pair(vp, configuration, "test.key", "$MESSAGE"); if (transformers) { gint i; ValuePairsTransformSet *vpts = value_pairs_transform_set_new("*"); for (i = 0; i < transformers->len; i++) value_pairs_transform_set_add_func(vpts, g_ptr_array_index(transformers, i)); value_pairs_add_transforms(vp, (gpointer *)vpts); } args[0] = &vp_keys_list; args[1] = &test_key_found; value_pairs_foreach(vp, vp_keys_foreach, msg, 11, args); g_list_foreach(vp_keys_list, (GFunc) cat_keys_foreach, vp_keys); if (strcmp(vp_keys->str, expected) != 0) { fprintf(stderr, "Scope keys mismatch, scope=[%s], exclude=[%s], value=[%s], expected=[%s]\n", scope, exclude ? exclude : "(none)", vp_keys->str, expected); success = FALSE; } if (!test_key_found) { fprintf(stderr, "test.key is not found in the result set\n"); success = FALSE; } g_list_foreach(vp_keys_list, (GFunc) g_free, NULL); g_list_free(vp_keys_list); g_string_free(vp_keys, TRUE); log_msg_unref(msg); value_pairs_free(vp); }
static gboolean tf_graphite_format(GString *result, ValuePairs *vp, LogMessage *msg, const LogTemplateOptions *template_options, LogTemplate *timestamp_template, gint time_zone_mode) { TFGraphiteForeachUserData userdata; gboolean return_value; userdata.result = result; userdata.formatted_unixtime = g_string_new(""); log_template_format(timestamp_template, msg, NULL, 0, 0, NULL, userdata.formatted_unixtime); return_value = value_pairs_foreach(vp, tf_graphite_foreach_func, msg, 0, time_zone_mode, template_options, &userdata); g_string_free(userdata.formatted_unixtime, FALSE); return return_value; }
static gboolean afstomp_worker_publish(STOMPDestDriver *self, LogMessage *msg) { gboolean success = TRUE; SBGString *body = NULL; stomp_frame frame; stomp_frame recv_frame; gchar seq_num[16]; if (!self->conn) { msg_error("STOMP server is not connected, not sending message!", NULL); return FALSE; } body = sb_gstring_acquire(); stomp_frame_init(&frame, "SEND", sizeof("SEND")); if (self->persistent) stomp_frame_add_header(&frame, "persistent", "true"); stomp_frame_add_header(&frame, "destination", self->destination); if (self->ack_needed) { g_snprintf(seq_num, sizeof(seq_num), "%i", self->super.seq_num); stomp_frame_add_header(&frame, "receipt", seq_num); }; value_pairs_foreach(self->vp, afstomp_vp_foreach, msg, self->super.seq_num, LTZ_SEND, &self->template_options, &frame); afstomp_set_frame_body(self, body, &frame, msg); if (!afstomp_send_frame(self, &frame)) { msg_error("Error while inserting into STOMP server", NULL); success = FALSE; } if (success && self->ack_needed) success = stomp_receive_frame(self->conn, &recv_frame); sb_gstring_release(body); return success; }
static gboolean afmongodb_worker_insert (MongoDBDestDriver *self) { gboolean success; guint8 *oid; LogMessage *msg; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; afmongodb_dd_connect(self, TRUE); g_mutex_lock(self->queue_mutex); log_queue_reset_parallel_push(self->queue); success = log_queue_pop_head(self->queue, &msg, &path_options, FALSE, FALSE); g_mutex_unlock(self->queue_mutex); if (!success) return TRUE; msg_set_context(msg); bson_reset (self->bson_sel); bson_reset (self->bson_upd); bson_reset (self->bson_set); oid = mongo_util_oid_new_with_time (self->last_msg_stamp, self->seq_num); bson_append_oid (self->bson_sel, "_id", oid); g_free (oid); bson_finish (self->bson_sel); value_pairs_foreach (self->vp, afmongodb_vp_foreach, msg, self->seq_num, self->bson_set); bson_finish (self->bson_set); bson_append_document (self->bson_upd, "$set", self->bson_set); bson_finish (self->bson_upd); if (!mongo_sync_cmd_update (self->conn, self->ns, MONGO_WIRE_FLAG_UPDATE_UPSERT, self->bson_sel, self->bson_upd)) { msg_error ("Network error while inserting into MongoDB", evt_tag_int("time_reopen", self->time_reopen), NULL); success = FALSE; } msg_set_context(NULL); if (success) { stats_counter_inc(self->stored_messages); step_sequence_number(&self->seq_num); log_msg_ack(msg, &path_options); log_msg_unref(msg); } else { g_mutex_lock(self->queue_mutex); log_queue_push_head(self->queue, msg, &path_options); g_mutex_unlock(self->queue_mutex); } return success; }
static gboolean perl_worker_eval(LogThrDestDriver *d) { PerlDestDriver *self = (PerlDestDriver *)d; gboolean success, vp_ok; LogMessage *msg; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; PerlInterpreter *my_perl = self->perl; int count; HV *kvmap; gpointer args[3]; dSP; success = log_queue_pop_head(self->super.queue, &msg, &path_options, FALSE, FALSE); if (!success) return TRUE; msg_set_context(msg); ENTER; SAVETMPS; PUSHMARK(SP); kvmap = newHV(); args[0] = self->perl; args[1] = kvmap; args[2] = self; vp_ok = value_pairs_foreach(self->vp, perl_worker_vp_add_one, msg, self->seq_num, LTZ_SEND, &self->template_options, args); if (!vp_ok && (self->template_options.on_error & ON_ERROR_DROP_MESSAGE)) goto exit; XPUSHs(sv_2mortal(newRV_noinc((SV *)kvmap))); PUTBACK; count = call_pv(self->queue_func_name, G_EVAL | G_SCALAR); SPAGAIN; msg_set_context(NULL); if (SvTRUE(ERRSV)) { msg_error("Error while calling a Perl function", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("script", self->filename), evt_tag_str("function", self->queue_func_name), evt_tag_str("error-message", SvPV_nolen(ERRSV)), NULL); (void) POPs; success = FALSE; } if (count != 1) { msg_error("Too many values returned by a Perl function", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("script", self->filename), evt_tag_str("function", self->queue_func_name), evt_tag_int("returned-values", count), evt_tag_int("expected-values", 1), NULL); success = FALSE; } else { int r = POPi; success = (r != 0); } exit: PUTBACK; FREETMPS; LEAVE; if (success && vp_ok) { stats_counter_inc(self->super.stored_messages); step_sequence_number(&self->seq_num); log_msg_ack(msg, &path_options); log_msg_unref(msg); } else { stats_counter_inc(self->super.dropped_messages); step_sequence_number(&self->seq_num); log_msg_ack(msg, &path_options); log_msg_unref(msg); } return success; }
static worker_insert_result_t perl_worker_eval(LogThrDestDriver *d, LogMessage *msg) { PerlDestDriver *self = (PerlDestDriver *)d; gboolean success, vp_ok; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; PerlInterpreter *my_perl = self->perl; int count; HV *kvmap; gpointer args[3]; dSP; ENTER; SAVETMPS; PUSHMARK(SP); kvmap = newHV(); args[0] = self->perl; args[1] = kvmap; args[2] = self; vp_ok = value_pairs_foreach(self->vp, perl_worker_vp_add_one, msg, self->seq_num, LTZ_SEND, &self->template_options, args); if (!vp_ok && (self->template_options.on_error & ON_ERROR_DROP_MESSAGE)) goto exit; XPUSHs(sv_2mortal(newRV_noinc((SV *)kvmap))); PUTBACK; count = call_pv(self->queue_func_name, G_EVAL | G_SCALAR); SPAGAIN; if (SvTRUE(ERRSV)) { msg_error("Error while calling a Perl function", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("script", self->filename), evt_tag_str("function", self->queue_func_name), evt_tag_str("error-message", SvPV_nolen(ERRSV)), NULL); (void) POPs; success = FALSE; } if (count != 1) { msg_error("Too many values returned by a Perl function", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("script", self->filename), evt_tag_str("function", self->queue_func_name), evt_tag_int("returned-values", count), evt_tag_int("expected-values", 1), NULL); success = FALSE; } else { int r = POPi; success = (r != 0); } exit: PUTBACK; FREETMPS; LEAVE; if (success && vp_ok) { return WORKER_INSERT_RESULT_SUCCESS; } else { return WORKER_INSERT_RESULT_DROP; } }