Skip to content

Commit 0929a48

Browse files
cosmo0920edsiper
authored andcommitted
in_windows_exporter_metrics: Support retrival of second values of perflib
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 1262391 commit 0929a48

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

plugins/in_windows_exporter_metrics/we_metric.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ void we_deinitialize_perflib_metric_sources(struct we_perflib_metric_source *sou
201201
for (source_index = 0 ;
202202
sources[source_index].name != NULL;
203203
source_index++) {
204+
if (sources[source_index].name != NULL) {
205+
flb_free(sources[source_index].name);
206+
}
207+
204208
if (sources[source_index].label_set_size) {
205209
flb_sds_destroy(sources[source_index].label_set[0]);
206210
flb_free(sources[source_index].label_set);
@@ -215,10 +219,11 @@ int we_initialize_perflib_metric_sources(
215219
{
216220
size_t source_array_size;
217221
struct we_perflib_metric_source *source_array_copy;
218-
struct we_perflib_metric_spec *source_entry;
222+
struct we_perflib_metric_source *source_entry;
219223
size_t source_index;
220224
size_t source_count;
221225
int result;
226+
char *flag_ptr;
222227

223228
if (out_sources == NULL) {
224229
return -1;
@@ -241,7 +246,7 @@ int we_initialize_perflib_metric_sources(
241246
source_array_size = sizeof(struct we_perflib_metric_source);
242247
source_array_size *= (source_count + 1);
243248

244-
source_array_copy = (struct we_perflib_metric_spec *) flb_calloc(1, source_array_size);
249+
source_array_copy = (struct we_perflib_metric_source *) flb_calloc(1, source_array_size);
245250

246251
if (source_array_copy == NULL) {
247252
return -4;
@@ -252,6 +257,23 @@ int we_initialize_perflib_metric_sources(
252257
for (source_index = 0 ; source_index < source_count; source_index++) {
253258
source_entry = &source_array_copy[source_index];
254259

260+
source_entry->name = flb_strdup(source_entry->name);
261+
if (source_entry->name == NULL) {
262+
/* Handle memory allocation failure */
263+
we_deinitialize_perflib_metric_sources(source_array_copy);
264+
flb_free(source_array_copy);
265+
return -1; /* Or appropriate error code */
266+
}
267+
268+
/* Now it is safe to search and modify the writable copy */
269+
source_entry->use_secondary_value = FLB_FALSE;
270+
flag_ptr = strstr(source_entry->name, ",secondvalue");
271+
272+
if (flag_ptr != NULL) {
273+
source_entry->use_secondary_value = FLB_TRUE;
274+
*flag_ptr = '\0'; /* This now modifies the heap copy, not read-only memory */
275+
}
276+
255277
result = we_expand_perflib_metric_source_labels(source_entry);
256278

257279
if (result != 0) {

plugins/in_windows_exporter_metrics/we_metric.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct we_perflib_metric_source {
4343
char *raw_label_set;
4444
char **label_set;
4545
size_t label_set_size;
46+
int use_secondary_value;
4647
};
4748

4849
#define WE_PERFLIB_SPEC(type_, name_, description_, raw_label_set_) \

plugins/in_windows_exporter_metrics/we_perflib.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
#include "we_metric.h"
2525
#include "we_perflib.h"
2626

27-
double we_perflib_get_adjusted_counter_value(struct we_perflib_counter *counter)
27+
double we_perflib_get_adjusted_counter_value(struct we_perflib_counter *counter,
28+
struct we_perflib_metric_source *source)
2829
{
2930
double result;
3031

32+
if (source->use_secondary_value) {
33+
return (double) counter->secondary_value.as_qword;
34+
}
35+
3136
result = (double) counter->primary_value.as_qword;
3237

3338
switch(counter->definition->type) {
@@ -625,6 +630,7 @@ static int we_perflib_process_counter(
625630
struct we_perflib_counter **out_counter)
626631
{
627632
struct we_perflib_counter *perflib_instance_counter;
633+
uint32_t counter_type = counter_definition->type;
628634

629635
perflib_instance_counter = we_perflib_create_counter(counter_definition);
630636

@@ -636,6 +642,18 @@ static int we_perflib_process_counter(
636642
&input_data_block[counter_definition->offset],
637643
counter_definition->size);
638644

645+
if (counter_type == PERF_AVERAGE_BULK ||
646+
counter_type == PERF_RAW_FRACTION ||
647+
counter_type == PERF_100NSEC_TIMER_INV ||
648+
counter_type == PERF_COUNTER_TIMER_INV ||
649+
counter_type == PERF_100NSEC_MULTI_TIMER_INV ||
650+
counter_type == PERF_COUNTER_MULTI_TIMER_INV) {
651+
652+
memcpy(&perflib_instance_counter->secondary_value,
653+
&input_data_block[counter_definition->offset + counter_definition->size],
654+
sizeof(union we_perflib_value));
655+
}
656+
639657
if (counter_definition->size > sizeof(union we_perflib_value)) {
640658
we_perflib_destroy_counter(perflib_instance_counter);
641659

@@ -1024,12 +1042,12 @@ int we_perflib_update_counters(struct flb_we *ctx,
10241042

10251043
if (metric_source->parent->type == CMT_COUNTER) {
10261044
cmt_counter_set(metric_entry, timestamp,
1027-
we_perflib_get_adjusted_counter_value(counter),
1045+
we_perflib_get_adjusted_counter_value(counter, metric_source),
10281046
metric_label_count, metric_label_list);
10291047
}
10301048
else if (metric_source->parent->type == CMT_GAUGE) {
10311049
cmt_gauge_set(metric_entry, timestamp,
1032-
we_perflib_get_adjusted_counter_value(counter),
1050+
we_perflib_get_adjusted_counter_value(counter, metric_source),
10331051
metric_label_count, metric_label_list);
10341052
}
10351053
}

0 commit comments

Comments
 (0)