-
Notifications
You must be signed in to change notification settings - Fork 74
/
log_producer_sample.c
143 lines (121 loc) · 5.64 KB
/
log_producer_sample.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
//
// Created by ZhangCheng on 26/12/2017.
//
#include "log_api.h"
#include "log_producer_config.h"
#include "log_producer_client.h"
void on_log_send_done(const char * config_name, log_producer_result result, size_t log_bytes, size_t compressed_bytes, const char * req_id, const char * message, const unsigned char * raw_buffer)
{
if (result == LOG_PRODUCER_OK)
{
// @note requst id may be NULL and printf %s NULL is undefined behavior
if (req_id == NULL)
{
req_id = "";
}
printf("send success, config : %s, result : %d, log bytes : %d, compressed bytes : %d, request id : %s \n",
config_name, (result),
(int)log_bytes, (int)compressed_bytes, req_id);
}
else
{
// @note message may be NULL and printf %s NULL is undefined behavior
if (message == NULL)
{
message = "";
}
// @note requst id may be NULL and printf %s NULL is undefined behavior
if (req_id == NULL)
{
req_id = "";
}
printf("send fail, config : %s, result : %d, log bytes : %d, compressed bytes : %d, request id : %s, error message : %s\n",
config_name, (result),
(int)log_bytes, (int)compressed_bytes, req_id, message);
}
}
log_producer * create_log_producer_wrapper(on_log_producer_send_done_function on_send_done)
{
log_producer_config * config = create_log_producer_config();
// endpoint list: https://help.aliyun.com/document_detail/29008.html
log_producer_config_set_endpoint(config, "${your_endpoint}");
log_producer_config_set_project(config, "${your_project}");
log_producer_config_set_logstore(config, "${your_logstore}");
log_producer_config_set_access_id(config, "${your_access_key_id}");
log_producer_config_set_access_key(config, "${your_access_key_secret}");
// @note, if using security token to send log, remember reset token before expired. this function is thread safe
//log_producer_config_reset_security_token(config, "${your_access_key_id}", "${your_access_key_secret}", "${your_access_security_token}");
// if you do not need topic or tag, comment it
log_producer_config_set_topic(config, "test_topic");
log_producer_config_add_tag(config, "tag_1", "val_1");
log_producer_config_add_tag(config, "tag_2", "val_2");
log_producer_config_add_tag(config, "tag_3", "val_3");
log_producer_config_add_tag(config, "tag_4", "val_4");
log_producer_config_add_tag(config, "tag_5", "val_5");
// set resource params
log_producer_config_set_packet_log_bytes(config, 4*1024*1024);
log_producer_config_set_packet_log_count(config, 4096);
log_producer_config_set_packet_timeout(config, 3000);
log_producer_config_set_max_buffer_limit(config, 64*1024*1024);
// set send thread count
log_producer_config_set_send_thread_count(config, 4);
// set compress type : lz4
log_producer_config_set_compress_type(config, 1);
// set timeout
log_producer_config_set_connect_timeout_sec(config, 10);
log_producer_config_set_send_timeout_sec(config, 15);
log_producer_config_set_destroy_flusher_wait_sec(config, 1);
log_producer_config_set_destroy_sender_wait_sec(config, 1);
// set interface
log_producer_config_set_net_interface(config, NULL);
return create_log_producer(config, on_send_done);
}
void log_producer_post_logs()
{
if (log_producer_env_init(LOG_GLOBAL_ALL) != LOG_PRODUCER_OK) {
exit(1);
}
log_producer * producer = create_log_producer_wrapper(on_log_send_done);
if (producer == NULL)
{
printf("create log producer by config fail \n");
exit(1);
}
log_producer_client * client = get_log_producer_client(producer, NULL);
if (client == NULL)
{
printf("create log producer client by config fail \n");
exit(1);
}
int i = 0;
for (; i < 10; ++i)
{
char indexStr[32];
sprintf(indexStr, "%d", i);
log_producer_client_add_log(client, 20, "content_key_1", "1abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_2", "2abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_3", "3abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_4", "4abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_5", "5abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_6", "6abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_7", "7abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_8", "8abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_9", "9abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"index", indexStr);
log_producer_result rst = log_producer_client_add_log(client, 8, "LogHub", "Real-time log collection and consumption",
"Search/Analytics", "Query and real-time analysis",
"Visualized", "dashboard and report functions",
"Interconnection", "Grafana and JDBC/SQL92");
if (rst != LOG_PRODUCER_OK)
{
printf("add log error %d \n", rst);
}
}
destroy_log_producer(producer);
log_producer_env_destroy();
}
int main(int argc, char *argv[])
{
log_producer_post_logs();
return 0;
}