}
label_set_reset(&m->label);
- label_set_reset(&m->resource);
meta_data_destroy(m->meta);
memset(m, 0, sizeof(*m));
if ((buf == NULL) || (m == NULL) || (m->family == NULL)) {
return EINVAL;
}
+ label_set_t const *resource = &m->family->resource;
int status = strbuf_print(buf, m->family->name);
- if (m->resource.num == 0 && m->label.num == 0) {
+ if (resource->num == 0 && m->label.num == 0) {
return status;
}
status = status || strbuf_print(buf, "{");
bool first_label = true;
- if (m->resource.num == 0) {
- status = status || format_label_set(buf, &m->resource,
- RESOURCE_LABEL_PREFIX, first_label);
+ if (resource->num != 0) {
+ status = status || format_label_set(buf, resource, RESOURCE_LABEL_PREFIX,
+ first_label);
first_label = false;
}
status = status || format_label_set(buf, &m->label, "", first_label);
return label_set_update(&m->label, name, value);
}
-int metric_resource_attribute_update(metric_t *m, char const *name,
- char const *value) {
- if (m == NULL) {
- return EINVAL;
- }
-
- return label_set_update(&m->resource, name, value);
-}
-
char const *metric_label_get(metric_t const *m, char const *name) {
if ((m == NULL) || (name == NULL)) {
errno = EINVAL;
.interval = src.ptr[i].interval,
};
- int status = label_set_clone(&ret.ptr[i].resource, src.ptr[i].resource);
- if (status != 0) {
- metric_list_reset(&ret);
- return status;
- }
-
- status = label_set_clone(&ret.ptr[i].label, src.ptr[i].label);
+ int status = label_set_clone(&ret.ptr[i].label, src.ptr[i].label);
if (status != 0) {
metric_list_reset(&ret);
return status;
free(fam->name);
free(fam->help);
+ label_set_reset(&fam->resource);
metric_list_reset(&fam->metric);
free(fam);
}
}
ret->type = fam->type;
- int status = metric_list_clone(&ret->metric, fam->metric, ret);
+ int status = label_set_clone(&ret->resource, fam->resource);
+ if (status != 0) {
+ metric_family_free(ret);
+ errno = status;
+ return NULL;
+ }
+
+ status = metric_list_clone(&ret->metric, fam->metric, ret);
if (status != 0) {
metric_family_free(ret);
errno = status;
return 0;
}
+int metric_family_resource_attribute_update(metric_family_t *fam,
+ char const *name,
+ char const *value) {
+ if (fam == NULL) {
+ return EINVAL;
+ }
+
+ return label_set_update(&fam->resource, name, value);
+}
+
/* metric_family_unmarshal_identity parses the metric identity and updates
* "inout" to point to the first character following the identity. With valid
* input, this means that "inout" will then point either to a '\0' (null byte)
assert(fam->metric.num >= 1);
if (is_resource_label) {
- status = label_set_add(&m->resource, key, value.ptr);
+ status = metric_family_resource_attribute_update(fam, key, value.ptr);
} else {
status = metric_label_set(m, key, value.ptr);
}
metric_family_t *family; /* backreference for family->name and family->type */
label_set_t label;
- label_set_t resource;
value_t value;
cdtime_t time; /* TODO(octo): use ms or µs instead? */
* label that does not exist is *not* an error. */
int metric_label_set(metric_t *m, char const *name, char const *value);
-/* metric_resource_attribute_update adds, updates, or deletes a resource
- * attribute. If "value" is NULL or an empty string, the attribute is removed.
- * Removing an attribute that does not exist is *not* an error. */
-int metric_resource_attribute_update(metric_t *m, char const *name,
- char const *value);
-
/* metric_label_get efficiently looks up and returns the value of the "name"
* label. If a label does not exist, NULL is returned and errno is set to
* ENOENT. The returned pointer may not be valid after a subsequent call to
char *help;
metric_type_t type;
+ label_set_t resource;
metric_list_t metric;
};
* allocates memory which must be freed using metric_family_metric_reset. */
int metric_family_metric_append(metric_family_t *fam, metric_t m);
+/* metric_family_resource_attribute_update adds, updates, or deletes a
+ * resource attribute. If "value" is NULL or an empty string, the attribute
+ * is removed.
+ * Removing an attribute that does not exist is *not* an error. */
+int metric_family_resource_attribute_update(metric_family_t *fam,
+ char const *name,
+ char const *value);
+
/* metric_family_append constructs a new metric_t and appends it to fam. It is
* a convenience function that is funcitonally approximately equivalent to the
* following code, but without modifying templ:
cases[i].labels[j].value));
}
for (size_t j = 0; j < cases[i].rattr_num; j++) {
- CHECK_ZERO(metric_resource_attribute_update(&m, cases[i].rattr[j].name,
- cases[i].rattr[j].value));
+ CHECK_ZERO(metric_family_resource_attribute_update(
+ &fam, cases[i].rattr[j].name, cases[i].rattr[j].value));
}
strbuf_t buf = STRBUF_CREATE;
STRBUF_DESTROY(buf);
metric_family_metric_reset(&fam);
+ label_set_reset(&fam.resource);
metric_reset(&m);
}
return 0;
} /* int plugin_dispatch_values_internal */
-static void set_default_resource_attributes(metric_t *m) {
- if (m->resource.num > 0) {
+static void set_default_resource_attributes(metric_family_t *fam) {
+ if (fam->resource.num > 0) {
return;
}
- label_set_clone(&m->resource, default_resource_attributes());
+ label_set_clone(&fam->resource, default_resource_attributes());
}
EXPORT int plugin_dispatch_metric_family(metric_family_t const *fam) {
return status;
}
+ set_default_resource_attributes(fam_copy);
+
cdtime_t time = cdtime();
cdtime_t interval = plugin_get_interval();
if (m->interval == 0) {
m->interval = interval;
}
-
- set_default_resource_attributes(m);
}
int status = plugin_dispatch_metric_internal(fam_copy);
return 0;
}
-static void gr_format_label_set(strbuf_t *buf, label_set_t const *labels, char const escape_char, unsigned int flags) {
+static void gr_format_label_set(strbuf_t *buf, label_set_t const *labels,
+ char const escape_char, unsigned int flags) {
for (size_t i = 0; i < labels->num; i++) {
label_pair_t *l = labels->ptr + i;
strbuf_print(buf, ".");
}
static void gr_format_name(strbuf_t *buf, metric_t const *m, char const *prefix,
- char const *suffix, char const escape_char,
- unsigned int flags) {
+ char const *suffix, char const escape_char,
+ unsigned int flags) {
if (prefix != NULL) {
strbuf_print(buf, prefix);
}
strbuf_print(buf, suffix);
}
- gr_format_label_set(buf, &m->resource, escape_char, flags);
+ gr_format_label_set(buf, &m->family->resource, escape_char, flags);
gr_format_label_set(buf, &m->label, escape_char, flags);
}