summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/ctraces/examples
diff options
context:
space:
mode:
Diffstat (limited to 'src/fluent-bit/lib/ctraces/examples')
-rw-r--r--src/fluent-bit/lib/ctraces/examples/CMakeLists.txt24
-rw-r--r--src/fluent-bit/lib/ctraces/examples/otlp-decoder.c61
-rw-r--r--src/fluent-bit/lib/ctraces/examples/otlp-encoder/README.md26
-rw-r--r--src/fluent-bit/lib/ctraces/examples/otlp-encoder/otlp-encoder.c186
-rw-r--r--src/fluent-bit/lib/ctraces/examples/sample_trace.binbin0 -> 534 bytes
-rw-r--r--src/fluent-bit/lib/ctraces/examples/simple-c-api.c160
6 files changed, 457 insertions, 0 deletions
diff --git a/src/fluent-bit/lib/ctraces/examples/CMakeLists.txt b/src/fluent-bit/lib/ctraces/examples/CMakeLists.txt
new file mode 100644
index 000000000..4c65c5b6e
--- /dev/null
+++ b/src/fluent-bit/lib/ctraces/examples/CMakeLists.txt
@@ -0,0 +1,24 @@
+# example: simple C api usage
+set(src
+ simple-c-api.c
+ )
+
+add_executable(ctraces-simple-c-api ${src})
+target_link_libraries(ctraces-simple-c-api ctraces-static fluent-otel-proto)
+
+# example: opentelemetry decoder -> ctrace
+set(src
+ otlp-decoder.c
+ )
+
+add_executable(ctraces-otlp-decoder ${src})
+target_link_libraries(ctraces-otlp-decoder ctraces-static)
+configure_file(sample_trace.bin sample_trace.bin COPYONLY)
+
+# example: opentelemetry encoder
+set(src
+ otlp-encoder/otlp-encoder.c
+ )
+
+add_executable(ctraces-otlp-encoder ${src})
+target_link_libraries(ctraces-otlp-encoder ctraces-static curl) \ No newline at end of file
diff --git a/src/fluent-bit/lib/ctraces/examples/otlp-decoder.c b/src/fluent-bit/lib/ctraces/examples/otlp-decoder.c
new file mode 100644
index 000000000..2b3d390da
--- /dev/null
+++ b/src/fluent-bit/lib/ctraces/examples/otlp-decoder.c
@@ -0,0 +1,61 @@
+#include <ctraces/ctraces.h>
+#include <fluent-otel-proto/fluent-otel.h>
+
+int main()
+{
+ FILE *fp;
+ struct ctrace *ctr;
+
+ char *text;
+ char *buf;
+ int result;
+ int bufsize;
+ size_t offset;
+ size_t newLen;
+
+ offset = 0;
+ bufsize = 0;
+ buf = NULL;
+
+ fp = fopen("examples/sample_trace.bin", "rb");
+
+ if (fp != NULL) {
+ if (fseek(fp, 0L, SEEK_END) == 0) {
+
+ bufsize = ftell(fp);
+ if (bufsize == -1)
+ {
+ printf("error in reading file size");
+ }
+
+ buf = malloc(sizeof(char) * (bufsize + 1));
+
+ if (fseek(fp, 0L, SEEK_SET) != 0) {
+ printf("seek error");
+ }
+
+ newLen = fread(buf, sizeof(char), bufsize, fp);
+ if (ferror(fp) != 0) {
+ fputs("Error reading file", stderr);
+ }
+ else {
+ buf[newLen++] = '\0';
+ }
+ }
+ fclose(fp);
+ }
+
+ result = ctr_decode_opentelemetry_create(&ctr, buf, bufsize, &offset);
+ if (result == -1) {
+ printf("Unable to decode trace sample");
+ }
+
+ text = ctr_encode_text_create(ctr);
+ printf("%s\n", text);
+ ctr_encode_text_destroy(text);
+
+ ctr_destroy(ctr);
+ free(buf);
+
+ return 0;
+} \ No newline at end of file
diff --git a/src/fluent-bit/lib/ctraces/examples/otlp-encoder/README.md b/src/fluent-bit/lib/ctraces/examples/otlp-encoder/README.md
new file mode 100644
index 000000000..78e65aca3
--- /dev/null
+++ b/src/fluent-bit/lib/ctraces/examples/otlp-encoder/README.md
@@ -0,0 +1,26 @@
+# Instructions to run
+
+After building the project with `-CTR_DEV=on`, the example can be found at `build/examples/ctraces-otlp-encoder`
+
+The example encodes a ctraces context to a buffer and sends it to a locally running instance of the [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/). After setting up the collector locally by [following the instructions from their docs](https://opentelemetry.io/docs/collector/getting-started/), you can start the collector with the following config file:
+
+```yaml
+receivers:
+ otlp:
+ protocols:
+ http:
+ endpoint: "0.0.0.0:4318"
+
+exporters:
+ logging:
+ loglevel: debug
+
+service:
+ pipelines:
+ traces:
+ receivers: [otlp]
+ exporters: [logging]
+```
+
+Now you can run the example and see the trace data logged by the collector instance.
+
diff --git a/src/fluent-bit/lib/ctraces/examples/otlp-encoder/otlp-encoder.c b/src/fluent-bit/lib/ctraces/examples/otlp-encoder/otlp-encoder.c
new file mode 100644
index 000000000..00b984a40
--- /dev/null
+++ b/src/fluent-bit/lib/ctraces/examples/otlp-encoder/otlp-encoder.c
@@ -0,0 +1,186 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+#include <ctraces/ctraces.h>
+#include <unistd.h>
+#include <curl/curl.h>
+
+int main()
+{
+ cfl_sds_t buf;
+ struct ctrace *ctx;
+ struct ctrace_opts opts;
+ struct ctrace_span *span_root;
+ struct ctrace_span *span_child;
+ struct ctrace_span_event *event;
+ struct ctrace_resource_span *resource_span;
+ struct ctrace_resource *resource;
+ struct ctrace_scope_span *scope_span;
+ struct ctrace_instrumentation_scope *instrumentation_scope;
+ struct ctrace_link *link;
+ struct ctrace_id *span_id;
+ struct ctrace_id *trace_id;
+ struct cfl_array *array;
+ struct cfl_array *sub_array;
+ struct cfl_kvlist *kv;
+
+ struct curl_slist *headers;
+ CURL *curl;
+ CURLcode res;
+
+ /*
+ * create an options context: this is used to initialize a CTrace context only,
+ * it's not mandatory and you can pass a NULL instead on context creation.
+ *
+ * note: not used.
+ */
+ ctr_opts_init(&opts);
+
+ /* ctrace context */
+ ctx = ctr_create(&opts);
+ if (!ctx) {
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ /* resource span */
+ resource_span = ctr_resource_span_create(ctx);
+ ctr_resource_span_set_schema_url(resource_span, "https://ctraces/resource_span_schema_url");
+
+ /* create a 'resource' for the 'resource span' in question */
+ resource = ctr_resource_span_get_resource(resource_span);
+ ctr_resource_set_dropped_attr_count(resource, 5);
+
+ /* scope span */
+ scope_span = ctr_scope_span_create(resource_span);
+ ctr_scope_span_set_schema_url(scope_span, "https://ctraces/scope_span_schema_url");
+
+ /* create an optional instrumentation scope */
+ instrumentation_scope = ctr_instrumentation_scope_create("ctrace", "a.b.c", 3, NULL);
+ ctr_scope_span_set_instrumentation_scope(scope_span, instrumentation_scope);
+
+ /* generate a random trace_id */
+ trace_id = ctr_id_create_random(CTR_ID_OTEL_TRACE_SIZE);
+
+ /* generate a random ID for the new span */
+ span_id = ctr_id_create_random(CTR_ID_OTEL_SPAN_SIZE);
+
+ /* Create a root span */
+ span_root = ctr_span_create(ctx, scope_span, "main", NULL);
+ if (!span_root) {
+ ctr_destroy(ctx);
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ /* assign the random ID */
+ ctr_span_set_span_id_with_cid(span_root, span_id);
+
+ /* set random trace_id */
+ ctr_span_set_trace_id_with_cid(span_root, trace_id);
+
+ /* add some attributes to the span */
+ ctr_span_set_attribute_string(span_root, "agent", "Fluent Bit");
+ ctr_span_set_attribute_int64(span_root, "year", 2022);
+ ctr_span_set_attribute_bool(span_root, "open_source", CTR_TRUE);
+ ctr_span_set_attribute_double(span_root, "temperature", 25.5);
+
+ /* pack an array: create an array context by using the CFL api */
+ array = cfl_array_create(4);
+ cfl_array_append_string(array, "first");
+ cfl_array_append_double(array, 2.0);
+ cfl_array_append_bool(array, CFL_FALSE);
+
+ sub_array = cfl_array_create(3);
+ cfl_array_append_double(sub_array, 3.1);
+ cfl_array_append_double(sub_array, 5.2);
+ cfl_array_append_double(sub_array, 6.3);
+ cfl_array_append_array(array, sub_array);
+
+ /* add array to the attribute list */
+ ctr_span_set_attribute_array(span_root, "my_array", array);
+
+ /* event: add one event and set attributes to it */
+ event = ctr_span_event_add(span_root, "connect to remote server");
+
+ ctr_span_event_set_attribute_string(event, "syscall 1", "open()");
+ ctr_span_event_set_attribute_string(event, "syscall 2", "connect()");
+ ctr_span_event_set_attribute_string(event, "syscall 3", "write()");
+
+ /* add a key/value pair list */
+ kv = cfl_kvlist_create(1);
+ cfl_kvlist_insert_string(kv, "language", "c");
+
+ ctr_span_set_attribute_kvlist(span_root, "my-list", kv);
+
+ /* create a child span */
+ span_child = ctr_span_create(ctx, scope_span, "do-work", span_root);
+ if (!span_child) {
+ ctr_destroy(ctx);
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ /* set trace_id */
+ ctr_span_set_trace_id_with_cid(span_child, trace_id);
+
+ /* use span_root ID as parent_span_id */
+ ctr_span_set_parent_span_id_with_cid(span_child, span_id);
+
+ /* delete old span id and generate a new one */
+ ctr_id_destroy(span_id);
+ span_id = ctr_id_create_random(CTR_ID_OTEL_SPAN_SIZE);
+ ctr_span_set_span_id_with_cid(span_child, span_id);
+
+ /* destroy the IDs since is not longer needed */
+ ctr_id_destroy(span_id);
+ ctr_id_destroy(trace_id);
+
+ /* change span kind to client */
+ ctr_span_kind_set(span_child, CTRACE_SPAN_CLIENT);
+
+ /* create a Link (no valid IDs of course) */
+ trace_id = ctr_id_create_random(CTR_ID_OTEL_TRACE_SIZE);
+ span_id = ctr_id_create_random(CTR_ID_OTEL_SPAN_SIZE);
+
+ link = ctr_link_create_with_cid(span_child, trace_id, span_id);
+ ctr_link_set_trace_state(link, "aaabbbccc");
+ ctr_link_set_dropped_attr_count(link, 2);
+
+ /* delete IDs */
+ ctr_id_destroy(span_id);
+ ctr_id_destroy(trace_id);
+
+ /* Encode Trace as otlp buffer */
+ buf = ctr_encode_opentelemetry_create(ctx);
+ if (!buf) {
+ ctr_destroy(ctx);
+ ctr_encode_opentelemetry_destroy(buf);
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ curl = curl_easy_init();
+
+ if (curl){
+ headers = NULL;
+ headers = curl_slist_append(headers, "Content-Type: application/x-protobuf");
+ curl_easy_setopt(curl, CURLOPT_URL, "0.0.0.0:4318/v1/traces");
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, cfl_sds_len(buf));
+ res = curl_easy_perform(curl);
+ if(res != CURLE_OK)
+ fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
+ curl_easy_cleanup(curl);
+ }
+ curl_global_cleanup();
+
+ /* destroy the context */
+ ctr_destroy(ctx);
+ ctr_encode_opentelemetry_destroy(buf);
+
+ /* exit options (it release resources allocated) */
+ ctr_opts_exit(&opts);
+
+ return 0;
+}
diff --git a/src/fluent-bit/lib/ctraces/examples/sample_trace.bin b/src/fluent-bit/lib/ctraces/examples/sample_trace.bin
new file mode 100644
index 000000000..579a3300f
--- /dev/null
+++ b/src/fluent-bit/lib/ctraces/examples/sample_trace.bin
Binary files differ
diff --git a/src/fluent-bit/lib/ctraces/examples/simple-c-api.c b/src/fluent-bit/lib/ctraces/examples/simple-c-api.c
new file mode 100644
index 000000000..aeaeff120
--- /dev/null
+++ b/src/fluent-bit/lib/ctraces/examples/simple-c-api.c
@@ -0,0 +1,160 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+#include <ctraces/ctraces.h>
+#include <unistd.h>
+
+int main()
+{
+ char *text;
+ struct ctrace *ctx;
+ struct ctrace_opts opts;
+ struct ctrace_span *span_root;
+ struct ctrace_span *span_child;
+ struct ctrace_span_event *event;
+ struct ctrace_resource_span *resource_span;
+ struct ctrace_resource *resource;
+ struct ctrace_scope_span *scope_span;
+ struct ctrace_instrumentation_scope *instrumentation_scope;
+ struct ctrace_link *link;
+ struct ctrace_id *span_id;
+ struct ctrace_id *trace_id;
+ struct cfl_array *array;
+ struct cfl_array *sub_array;
+ struct cfl_kvlist *kv;
+
+ /*
+ * create an options context: this is used to initialize a CTrace context only,
+ * it's not mandatory and you can pass a NULL instead on context creation.
+ *
+ * note: not used.
+ */
+ ctr_opts_init(&opts);
+
+ /* ctrace context */
+ ctx = ctr_create(&opts);
+ if (!ctx) {
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ /* resource span */
+ resource_span = ctr_resource_span_create(ctx);
+ ctr_resource_span_set_schema_url(resource_span, "https://ctraces/resource_span_schema_url");
+
+ /* create a 'resource' for the 'resource span' in question */
+ resource = ctr_resource_span_get_resource(resource_span);
+ ctr_resource_set_dropped_attr_count(resource, 5);
+
+ /* scope span */
+ scope_span = ctr_scope_span_create(resource_span);
+ ctr_scope_span_set_schema_url(scope_span, "https://ctraces/scope_span_schema_url");
+
+ /* create an optional instrumentation scope */
+ instrumentation_scope = ctr_instrumentation_scope_create("ctrace", "a.b.c", 3, NULL);
+ ctr_scope_span_set_instrumentation_scope(scope_span, instrumentation_scope);
+
+ /* generate a random trace_id */
+ trace_id = ctr_id_create_random(CTR_ID_OTEL_TRACE_SIZE);
+
+ /* generate a random ID for the new span */
+ span_id = ctr_id_create_random(CTR_ID_OTEL_SPAN_SIZE);
+
+ /* Create a root span */
+ span_root = ctr_span_create(ctx, scope_span, "main", NULL);
+ if (!span_root) {
+ ctr_destroy(ctx);
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ /* assign the random ID */
+ ctr_span_set_span_id_with_cid(span_root, span_id);
+
+ /* set random trace_id */
+ ctr_span_set_trace_id_with_cid(span_root, trace_id);
+
+ /* add some attributes to the span */
+ ctr_span_set_attribute_string(span_root, "agent", "Fluent Bit");
+ ctr_span_set_attribute_int64(span_root, "year", 2022);
+ ctr_span_set_attribute_bool(span_root, "open_source", CTR_TRUE);
+ ctr_span_set_attribute_double(span_root, "temperature", 25.5);
+
+ /* pack an array: create an array context by using the CFL api */
+ array = cfl_array_create(4);
+ cfl_array_append_string(array, "first");
+ cfl_array_append_double(array, 2.0);
+ cfl_array_append_bool(array, CFL_FALSE);
+
+ sub_array = cfl_array_create(3);
+ cfl_array_append_double(sub_array, 3.1);
+ cfl_array_append_double(sub_array, 5.2);
+ cfl_array_append_double(sub_array, 6.3);
+ cfl_array_append_array(array, sub_array);
+
+ /* add array to the attribute list */
+ ctr_span_set_attribute_array(span_root, "my_array", array);
+
+ /* event: add one event and set attributes to it */
+ event = ctr_span_event_add(span_root, "connect to remote server");
+
+ ctr_span_event_set_attribute_string(event, "syscall 1", "open()");
+ ctr_span_event_set_attribute_string(event, "syscall 2", "connect()");
+ ctr_span_event_set_attribute_string(event, "syscall 3", "write()");
+
+ /* add a key/value pair list */
+ kv = cfl_kvlist_create(1);
+ cfl_kvlist_insert_string(kv, "language", "c");
+
+ ctr_span_set_attribute_kvlist(span_root, "my-list", kv);
+
+ /* create a child span */
+ span_child = ctr_span_create(ctx, scope_span, "do-work", span_root);
+ if (!span_child) {
+ ctr_destroy(ctx);
+ ctr_opts_exit(&opts);
+ exit(EXIT_FAILURE);
+ }
+
+ /* set trace_id */
+ ctr_span_set_trace_id_with_cid(span_child, trace_id);
+
+ /* use span_root ID as parent_span_id */
+ ctr_span_set_parent_span_id_with_cid(span_child, span_id);
+
+ /* delete old span id and generate a new one */
+ ctr_id_destroy(span_id);
+ span_id = ctr_id_create_random(CTR_ID_OTEL_SPAN_SIZE);
+ ctr_span_set_span_id_with_cid(span_child, span_id);
+
+ /* destroy the IDs since is not longer needed */
+ ctr_id_destroy(span_id);
+ ctr_id_destroy(trace_id);
+
+ /* change span kind to client */
+ ctr_span_kind_set(span_child, CTRACE_SPAN_CLIENT);
+
+ /* create a Link (no valid IDs of course) */
+ trace_id = ctr_id_create_random(CTR_ID_OTEL_TRACE_SIZE);
+ span_id = ctr_id_create_random(CTR_ID_OTEL_SPAN_SIZE);
+
+ link = ctr_link_create_with_cid(span_child, trace_id, span_id);
+ ctr_link_set_trace_state(link, "aaabbbccc");
+ ctr_link_set_dropped_attr_count(link, 2);
+
+ /* delete IDs */
+ ctr_id_destroy(span_id);
+ ctr_id_destroy(trace_id);
+
+ /* Encode Trace as a readable text */
+ text = ctr_encode_text_create(ctx);
+ printf("%s\n", text);
+ ctr_encode_text_destroy(text);
+
+ /* destroy the context */
+ ctr_destroy(ctx);
+
+ /* exit options (it release resources allocated) */
+ ctr_opts_exit(&opts);
+
+ return 0;
+}