Skip to content

Commit cbbc448

Browse files
committed
feat: add UUID generation logic
1 parent 42c47a1 commit cbbc448

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

axiom/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ OBJS := \
124124
nr_span_queue.o \
125125
nr_synthetics.o \
126126
nr_txn.o \
127+
nr_uuid.o \
127128
nr_version.o \
128129
nr_php_packages.o \
129130
util_apdex.o \

axiom/nr_uuid.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <stdlib.h>
7+
#include <time.h>
8+
9+
#include "nr_uuid.h"
10+
#include "util_memory.h"
11+
12+
static const char hex_values[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
13+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
14+
char* nr_uuid_create(int seed) {
15+
char* uuid = nr_zalloc(NR_UUID_SIZE + 1);
16+
if (0 < seed) {
17+
srand(seed);
18+
} else {
19+
srand(time(NULL));
20+
}
21+
22+
for (int i = 0; i < NR_UUID_SIZE; i++) {
23+
int r = rand() % NR_UUID_RANGE;
24+
25+
uuid[i] = hex_values[r];
26+
}
27+
28+
return uuid;
29+
}

axiom/nr_uuid.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/*
7+
* This file contains functions for simple uuid generation.
8+
*/
9+
#ifndef NR_UUID_HDR
10+
#define NR_UUID_HDR
11+
12+
#define NR_UUID_SIZE 32
13+
#define NR_UUID_RANGE 16
14+
15+
/**
16+
* nr_uuid_create: pseudo-implementation of uuid generation logic
17+
*
18+
* This function will simply return a randomly generated 32 character hex
19+
* string. It does not implement the spec for UUID generation, which requires
20+
* specific adherence to implementation details and setting bits within the UUID
21+
* to signify which UUID generation variant was used.
22+
*
23+
* Params: int seed (<1 for time based seed)
24+
*
25+
* Returns: 32 byte hex string (must be freed by caller)
26+
*/
27+
char* nr_uuid_create(int seed);
28+
#endif

axiom/tests/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,5 @@ test_threads
125125
test_time
126126
test_txn
127127
test_url
128+
test_uuid
128129
test_vector

axiom/tests/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ TESTS := \
199199
test_time \
200200
test_txn \
201201
test_url \
202+
test_uuid \
202203
test_vector
203204

204205
#

axiom/tests/test_uuid.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "nr_axiom.h"
7+
8+
#include "nr_uuid.h"
9+
#include "util_memory.h"
10+
11+
#include "tlib_main.h"
12+
#include "util_sleep.h"
13+
14+
static void test_uuid_create(void) {
15+
char* uuid = NULL;
16+
char* tmp = NULL;
17+
18+
uuid = nr_uuid_create(1234);
19+
tlib_pass_if_not_null("uuid create success", uuid);
20+
21+
tmp = nr_strdup(uuid);
22+
nr_free(uuid);
23+
24+
uuid = nr_uuid_create(4321);
25+
tlib_pass_if_true("new uuid != old uuid", 0 != nr_strcmp(tmp, uuid),
26+
"old=%s, new=%s", tmp, uuid);
27+
28+
nr_free(tmp);
29+
30+
tmp = nr_strdup(uuid);
31+
32+
nr_free(uuid);
33+
34+
uuid = nr_uuid_create(0);
35+
tlib_pass_if_true("rand uuid != old uuid", 0 != nr_strcmp(tmp, uuid),
36+
"old=%s, new=%s", tmp, uuid);
37+
38+
nr_free(uuid);
39+
nr_free(tmp);
40+
}
41+
42+
tlib_parallel_info_t parallel_info = {.suggested_nthreads = 2, .state_size = 0};
43+
44+
void test_main(void* p NRUNUSED) {
45+
test_uuid_create();
46+
}

0 commit comments

Comments
 (0)