Skip to content

Commit

Permalink
Add Avro name mapping service
Browse files Browse the repository at this point in the history
Avro metric names cannot include characters other than [\\.\\-A-Za-z0-9].
This change adds a service to provide this mapping to Avro storage
plugins.
  • Loading branch information
tom95858 committed Apr 15, 2023
1 parent 68e667b commit 263b7a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
14 changes: 14 additions & 0 deletions ldms/src/ldmsd/ldmsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,20 @@ int ldmsd_row_to_json_object(ldmsd_row_t row, char **str, int *len);
*/
int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len);

/**
* \brief ldmsd_avro_name_get
*
* Avro names may only contain the characters [A-Za-z0-9\\_\\-]. LDMS metric
* names by contrast may characters outside this set. When creating Avro
* schema, these LDMS names must be mapped a valid Avro name. The function
* returns malloc'd memory that should be freed by the caller when no
* long longer needed.
*
* \param ldms_name The LDMS metric name to be mapped to a valid Avro name
* \return char* Pointer to the allocated buffer or NULL if ENOMEM
*/
char *ldmsd_avro_name_get(const char *ldms_name);

/**
* Configure strgp decomposer.
*
Expand Down
33 changes: 30 additions & 3 deletions ldms/src/ldmsd/ldmsd_decomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>

#include <openssl/sha.h>

Expand Down Expand Up @@ -779,8 +780,30 @@ static const char *col_type_str(enum ldms_value_type type)
return type_str[type];
}

static char get_avro_char(char c)
{
if (isalnum(c))
return c;
if (c == '_' || c == '-')
return c;
return '_';
}

char *ldmsd_avro_name_get(const char *ldms_name)
{
char *avro_name_buf = calloc(1, strlen(ldms_name) + 1);
char *avro_name = avro_name_buf;
while (*ldms_name != '\0') {
*avro_name++ = get_avro_char(*ldms_name++);
}
if (*avro_name)
*avro_name = '\0';
return avro_name_buf;
}

int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
{
char *avro_name = NULL;
struct strbuf_tailq_s h = TAILQ_HEAD_INITIALIZER(h);
ldmsd_col_t col;
int i, rc;
Expand All @@ -796,6 +819,7 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)

for (i = 0; i < row->col_count; i++) {
col = &row->cols[i];
avro_name = ldmsd_avro_name_get(col->name);
if (i) { /* comma */
rc = strbuf_printf(&h, ",");
if (rc)
Expand All @@ -808,7 +832,7 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
"\"type\":\"long\","
"\"logicalType\":\"timestamp-millis\""
"}}",
col->name);
avro_name);
if (rc)
goto err_0;
break;
Expand All @@ -825,7 +849,7 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
case LDMS_V_D64:
case LDMS_V_CHAR_ARRAY:
rc = strbuf_printf(&h, "{\"name\":\"%s\",\"type\":\"%s\"}",
col->name, col_type_str(col->type));
avro_name, col_type_str(col->type));
if (rc)
goto err_0;
break;
Expand All @@ -842,7 +866,7 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
rc = strbuf_printf(&h,
"{\"name\":\"%s\","
"\"type\":{ \"type\" : \"array\", \"items\": \"%s\" }}",
col->name, col_type_str(col->type));
avro_name, col_type_str(col->type));
if (rc)
goto err_0;
break;
Expand All @@ -862,9 +886,12 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)

rc = strbuf_str(&h, str, (int *)len);
strbuf_purge(&h);
free(avro_name);
return rc;

err_0:
if (avro_name)
free(avro_name);
strbuf_purge(&h);
return rc;
}

0 comments on commit 263b7a4

Please sign in to comment.