/src/fluent-bit/lib/ctraces/src/ctr_resource.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* CTraces |
4 | | * ======= |
5 | | * Copyright 2022 The CTraces Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <ctraces/ctraces.h> |
21 | | #include <ctraces/ctr_resource.h> |
22 | | |
23 | | struct ctrace_resource *ctr_resource_create() |
24 | 35.6k | { |
25 | 35.6k | struct ctrace_resource *res; |
26 | 35.6k | struct ctrace_attributes *attr; |
27 | | |
28 | 35.6k | res = calloc(1, sizeof(struct ctrace_resource)); |
29 | 35.6k | if (!res) { |
30 | 0 | ctr_errno(); |
31 | 0 | return NULL; |
32 | 0 | } |
33 | | |
34 | 35.6k | attr = ctr_attributes_create(); |
35 | 35.6k | if (!attr) { |
36 | 0 | ctr_resource_destroy(res); |
37 | 0 | return NULL; |
38 | 0 | } |
39 | 35.6k | res->attr = attr; |
40 | | |
41 | 35.6k | return res; |
42 | 35.6k | } |
43 | | |
44 | | struct ctrace_resource *ctr_resource_create_default() |
45 | 0 | { |
46 | 0 | struct ctrace_resource *res; |
47 | |
|
48 | 0 | res = ctr_resource_create(); |
49 | 0 | if (!res) { |
50 | 0 | return NULL; |
51 | 0 | } |
52 | | |
53 | | /* some default attributes */ |
54 | 0 | ctr_attributes_set_string(res->attr, "service.name", "Fluent Bit"); |
55 | 0 | ctr_attributes_set_int64(res->attr, "release_year", 2014); |
56 | |
|
57 | 0 | return res; |
58 | 0 | } |
59 | | |
60 | | int ctr_resource_set_attributes(struct ctrace_resource *res, struct ctrace_attributes *attr) |
61 | 27.8k | { |
62 | 27.8k | if (!attr) { |
63 | 0 | return -1; |
64 | 0 | } |
65 | | |
66 | 27.8k | if (res->attr) { |
67 | 27.8k | ctr_attributes_destroy(res->attr); |
68 | 27.8k | } |
69 | | |
70 | 27.8k | res->attr = attr; |
71 | 27.8k | return 0; |
72 | 27.8k | } |
73 | | |
74 | | void ctr_resource_set_dropped_attr_count(struct ctrace_resource *res, uint32_t count) |
75 | 57.4k | { |
76 | 57.4k | res->dropped_attr_count = count; |
77 | 57.4k | } |
78 | | |
79 | | void ctr_resource_destroy(struct ctrace_resource *res) |
80 | 35.6k | { |
81 | 35.6k | if (res->attr) { |
82 | 35.6k | ctr_attributes_destroy(res->attr); |
83 | 35.6k | } |
84 | 35.6k | free(res); |
85 | 35.6k | } |
86 | | |
87 | | /* |
88 | | * resource_span API |
89 | | * ----------------- |
90 | | */ |
91 | | |
92 | | /* creates a resource_span context */ |
93 | | struct ctrace_resource_span *ctr_resource_span_create(struct ctrace *ctx) |
94 | 35.6k | { |
95 | 35.6k | struct ctrace_resource_span *resource_span; |
96 | | |
97 | 35.6k | resource_span = calloc(1, sizeof(struct ctrace_resource_span)); |
98 | 35.6k | if (!resource_span) { |
99 | 0 | ctr_errno(); |
100 | 0 | return NULL; |
101 | 0 | } |
102 | 35.6k | cfl_list_init(&resource_span->scope_spans); |
103 | | |
104 | | /* link to ctraces context */ |
105 | 35.6k | cfl_list_add(&resource_span->_head, &ctx->resource_spans); |
106 | | |
107 | | /* create an empty resource */ |
108 | 35.6k | resource_span->resource = ctr_resource_create(); |
109 | 35.6k | if (!resource_span->resource) { |
110 | 0 | free(resource_span); |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | 35.6k | return resource_span; |
115 | 35.6k | } |
116 | | |
117 | | struct ctrace_resource *ctr_resource_span_get_resource(struct ctrace_resource_span *resource_span) |
118 | 29.5k | { |
119 | 29.5k | return resource_span->resource; |
120 | 29.5k | } |
121 | | |
122 | | /* Set the schema_url for a resource_span */ |
123 | | int ctr_resource_span_set_schema_url(struct ctrace_resource_span *resource_span, char *url) |
124 | 29.5k | { |
125 | 29.5k | if (resource_span->schema_url) { |
126 | 0 | cfl_sds_destroy(resource_span->schema_url); |
127 | 0 | } |
128 | | |
129 | 29.5k | resource_span->schema_url = cfl_sds_create(url); |
130 | 29.5k | if (!resource_span->schema_url) { |
131 | 0 | return -1; |
132 | 0 | } |
133 | | |
134 | 29.5k | return 0; |
135 | 29.5k | } |
136 | | |
137 | | void ctr_resource_span_destroy(struct ctrace_resource_span *resource_span) |
138 | 35.6k | { |
139 | 35.6k | struct cfl_list *tmp; |
140 | 35.6k | struct cfl_list *head; |
141 | 35.6k | struct ctrace_scope_span *scope_span; |
142 | | |
143 | | /* release resource if set */ |
144 | 35.6k | if (resource_span->resource) { |
145 | 35.6k | ctr_resource_destroy(resource_span->resource); |
146 | 35.6k | } |
147 | | |
148 | 35.6k | if (resource_span->schema_url) { |
149 | 30.1k | cfl_sds_destroy(resource_span->schema_url); |
150 | 30.1k | } |
151 | | |
152 | | /* remove scope spans */ |
153 | 35.6k | cfl_list_foreach_safe(head, tmp, &resource_span->scope_spans) { |
154 | 8.35k | scope_span = cfl_list_entry(head, struct ctrace_scope_span, _head); |
155 | 8.35k | ctr_scope_span_destroy(scope_span); |
156 | 8.35k | } |
157 | | |
158 | 35.6k | free(resource_span); |
159 | 35.6k | } |