summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/ctraces/examples/otlp-decoder.c
blob: 2b3d390da35822550d0a2207e6477d8891f912b3 (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
#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;
}