Skip to content

Commit

Permalink
Merge pull request collectd#4181 from octo/6/otep119
Browse files Browse the repository at this point in the history
Update standard system metric names in accordance with OpenTelemetry.
  • Loading branch information
octo authored Dec 6, 2023
2 parents dc7303c + eb249dc commit 815e2cd
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 316 deletions.
8 changes: 4 additions & 4 deletions src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,16 @@ static void aggregate(gauge_t *sum_by_state) /* {{{ */
static void cpu_commit_one(int cpu_num, /* {{{ */
gauge_t rates[static COLLECTD_CPU_STATE_MAX]) {
metric_family_t fam = {
.name = "cpu_usage_percent",
.name = "system.cpu.utilization",
.type = METRIC_TYPE_GAUGE,
};

metric_t m = {0};
if (cpu_num == -1) {
metric_label_set(&m, "cpu", "total");
} else {
char cpu_num_str[16];
snprintf(cpu_num_str, sizeof(cpu_num_str), "%d", cpu_num);
char cpu_num_str[32];
ssnprintf(cpu_num_str, sizeof(cpu_num_str), "%d", cpu_num);
metric_label_set(&m, "cpu", cpu_num_str);
}

Expand Down Expand Up @@ -515,7 +515,7 @@ static void cpu_reset(void) /* {{{ */
static void cpu_commit_without_aggregation(void) /* {{{ */
{
metric_family_t fam = {
.name = "cpu_usage_total",
.name = "system.cpu.time",
.type = METRIC_TYPE_COUNTER,
};

Expand Down
181 changes: 63 additions & 118 deletions src/df.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,86 +143,32 @@ static int df_read(void) {
#elif HAVE_STATFS
struct statfs statbuf;
#endif
metric_family_t fam_fs_free = {
.name = "filesystem_free_bytes",
metric_family_t fam_usage = {
.name = "system.filesystem.usage",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_reserved = {
.name = "filesystem_reserved_bytes",
metric_family_t fam_utilization = {
.name = "system.filesystem.utilization",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_used = {
.name = "filesystem_used_bytes",
metric_family_t fam_inode_usage = {
.name = "system.filesystem.inodes.usage",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_free_pct = {
.name = "filesystem_free_percent",
metric_family_t fam_inode_utilization = {
.name = "system.filesystem.inodes.utilization",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_reserved_pct = {
.name = "filesystem_reserved_percent",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_used_pct = {
.name = "filesystem_used_percent",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_inodes_free_pct = {
.name = "filesystem_inodes_free_percent",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_inodes_reserved_pct = {
.name = "filesystem_inodes_reserved_percent",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_inodes_used_pct = {
.name = "filesystem_inodes_used_percent",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_inodes_free = {
.name = "filesystem_inodes_free",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_inodes_reserved = {
.name = "filesystem_inodes_reserved",
.type = METRIC_TYPE_GAUGE,
};
metric_family_t fam_fs_inodes_used = {
.name = "filesystem_inodes_used",
.type = METRIC_TYPE_GAUGE,
};

metric_family_t *fams[] = {&fam_fs_free,
&fam_fs_reserved,
&fam_fs_used,
&fam_fs_free_pct,
&fam_fs_reserved_pct,
&fam_fs_used_pct,
&fam_fs_inodes_free,
&fam_fs_inodes_reserved,
&fam_fs_inodes_used,
&fam_fs_inodes_free_pct,
&fam_fs_inodes_reserved_pct,
&fam_fs_inodes_used_pct,
NULL};

int retval = 0;
/* struct STATANYFS statbuf; */
cu_mount_t *mnt_list;

mnt_list = NULL;
cu_mount_t *mnt_list = NULL;
if (cu_mount_getlist(&mnt_list) == NULL) {
ERROR("df plugin: cu_mount_getlist failed.");
return -1;
}

int retval = 0;
for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL;
mnt_ptr = mnt_ptr->next) {
unsigned long long blocksize;
uint64_t blk_free;
uint64_t blk_reserved;
uint64_t blk_used;

char const *dev =
(mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device;

Expand Down Expand Up @@ -250,7 +196,7 @@ static int df_read(void) {
if (!statbuf.f_blocks)
continue;

blocksize = BLOCKSIZE(statbuf);
gauge_t blocksize = (gauge_t)BLOCKSIZE(statbuf);

/*
* Sanity-check for the values in the struct
Expand All @@ -275,88 +221,79 @@ static int df_read(void) {
if (statbuf.f_blocks < statbuf.f_bfree)
statbuf.f_blocks = statbuf.f_bfree;

blk_free = (uint64_t)statbuf.f_bavail;
blk_reserved = (uint64_t)(statbuf.f_bfree - statbuf.f_bavail);
blk_used = (uint64_t)(statbuf.f_blocks - statbuf.f_bfree);
gauge_t blk_free = (gauge_t)statbuf.f_bavail;
gauge_t blk_reserved = (gauge_t)(statbuf.f_bfree - statbuf.f_bavail);
gauge_t blk_used = (gauge_t)(statbuf.f_blocks - statbuf.f_bfree);

metric_t m = {0};
metric_label_set(&m, "device", dev);
metric_label_set(&m, "fstype", mnt_ptr->type);
metric_label_set(&m, "mountpoint", mnt_ptr->dir);

if (values_absolute) {
m.value.gauge = (gauge_t)(blk_free * blocksize);
metric_family_metric_append(&fam_fs_free, m);
metric_family_append(&fam_usage, "state", "used",
(value_t){.gauge = blk_used * blocksize}, &m);

m.value.gauge = (gauge_t)(blk_reserved * blocksize);
metric_family_metric_append(&fam_fs_reserved, m);
metric_family_append(&fam_usage, "state", "free",
(value_t){.gauge = blk_free * blocksize}, &m);

m.value.gauge = (gauge_t)(blk_used * blocksize);
metric_family_metric_append(&fam_fs_used, m);
metric_family_append(&fam_usage, "state", "reserved",
(value_t){.gauge = blk_reserved * blocksize}, &m);
}

if (values_percentage) {
if (statbuf.f_blocks > 0) {
m.value.gauge = (gauge_t)((float_t)(blk_free) / statbuf.f_blocks * 100);
metric_family_metric_append(&fam_fs_free_pct, m);

m.value.gauge =
(gauge_t)((float_t)(blk_reserved) / statbuf.f_blocks * 100);
metric_family_metric_append(&fam_fs_reserved_pct, m);

m.value.gauge = (gauge_t)((float_t)(blk_used) / statbuf.f_blocks * 100);
metric_family_metric_append(&fam_fs_used_pct, m);
} else {
metric_reset(&m);
retval = -1;
break;
}
assert(statbuf.f_blocks != 0); // checked above
gauge_t f = 100.0 / (gauge_t)statbuf.f_blocks;

metric_family_append(&fam_utilization, "state", "used",
(value_t){.gauge = blk_used * f}, &m);

metric_family_append(&fam_utilization, "state", "free",
(value_t){.gauge = blk_free * f}, &m);

metric_family_append(&fam_utilization, "state", "reserved",
(value_t){.gauge = blk_reserved * f}, &m);
}

/* inode handling */
if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0) {
uint64_t inode_free;
uint64_t inode_reserved;
uint64_t inode_used;

/* Sanity-check for the values in the struct */
if (statbuf.f_ffree < statbuf.f_favail)
statbuf.f_ffree = statbuf.f_favail;
if (statbuf.f_files < statbuf.f_ffree)
statbuf.f_files = statbuf.f_ffree;

inode_free = (uint64_t)statbuf.f_favail;
inode_reserved = (uint64_t)(statbuf.f_ffree - statbuf.f_favail);
inode_used = (uint64_t)(statbuf.f_files - statbuf.f_ffree);
gauge_t inode_free = (gauge_t)statbuf.f_favail;
gauge_t inode_reserved = (gauge_t)(statbuf.f_ffree - statbuf.f_favail);
gauge_t inode_used = (gauge_t)(statbuf.f_files - statbuf.f_ffree);

if (values_percentage) {
if (statbuf.f_files > 0) {
m.value.gauge =
(gauge_t)((float_t)(inode_free) / statbuf.f_files * 100);
metric_family_metric_append(&fam_fs_inodes_free_pct, m);
gauge_t f = 100.0 / (gauge_t)statbuf.f_files;

metric_family_append(&fam_inode_usage, "state", "used",
(value_t){.gauge = inode_used * f}, &m);

m.value.gauge =
(gauge_t)((float_t)(inode_reserved) / statbuf.f_files * 100);
metric_family_metric_append(&fam_fs_inodes_reserved_pct, m);
metric_family_append(&fam_inode_usage, "state", "free",
(value_t){.gauge = inode_free * f}, &m);

m.value.gauge =
(gauge_t)((float_t)(inode_used) / statbuf.f_files * 100);
metric_family_metric_append(&fam_fs_inodes_used_pct, m);
metric_family_append(&fam_inode_usage, "state", "reserved",
(value_t){.gauge = inode_reserved * f}, &m);
} else {
metric_reset(&m);
retval = -1;
break;
}
}
if (values_absolute) {
m.value.gauge = (gauge_t)inode_free;
metric_family_metric_append(&fam_fs_inodes_free, m);
metric_family_append(&fam_inode_usage, "state", "used",
(value_t){.gauge = inode_used}, &m);

m.value.gauge = (gauge_t)inode_reserved;
metric_family_metric_append(&fam_fs_inodes_reserved, m);
metric_family_append(&fam_inode_usage, "state", "free",
(value_t){.gauge = inode_free}, &m);

m.value.gauge = (gauge_t)inode_used;
metric_family_metric_append(&fam_fs_inodes_used, m);
metric_family_append(&fam_inode_usage, "state", "reserved",
(value_t){.gauge = inode_reserved}, &m);
}
}

Expand All @@ -365,14 +302,22 @@ static int df_read(void) {

cu_mount_freelist(mnt_list);

for (size_t i = 0; fams[i] != NULL; i++) {
if (fams[i]->metric.num > 0) {
int status = plugin_dispatch_metric_family(fams[i]);
if (status != 0) {
ERROR("df: plugin_dispatch_metric_family failed: %s", STRERROR(status));
}
metric_family_metric_reset(fams[i]);
metric_family_t *families[] = {
&fam_usage, &fam_utilization, &fam_inode_usage, &fam_inode_utilization,
NULL,
};

for (size_t i = 0; families[i] != NULL; i++) {
if (families[i]->metric.num == 0) {
continue;
}

int status = plugin_dispatch_metric_family(families[i]);
if (status != 0) {
ERROR("df: plugin_dispatch_metric_family failed: %s", STRERROR(status));
retval = status;
}
metric_family_metric_reset(families[i]);
}

return retval;
Expand Down
Loading

0 comments on commit 815e2cd

Please sign in to comment.