diff --git a/opentelemetry_api/opentelemetry/context/TBD b/opentelemetry_api/opentelemetry/context/TBD deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/opentelemetry_api/opentelemetry/context/hello.c b/opentelemetry_api/opentelemetry/context/hello.c new file mode 100644 index 0000000000..95cb93b30e --- /dev/null +++ b/opentelemetry_api/opentelemetry/context/hello.c @@ -0,0 +1,14 @@ +/* this file will be removed once we have unit test and CI */ + +#include + +#include "traceparent.h" +#include "tracestate.h" + +int main(int argc, char* argv[]) +{ + TraceParent trace_parent; + status_t retval = trace_parent_from_string(&trace_parent, "00-12345678901234567890123456789012-1234567890123456-01"); + printf("Hello, World! %d\n", retval); + return 0; +} diff --git a/opentelemetry_api/opentelemetry/context/traceparent.c b/opentelemetry_api/opentelemetry/context/traceparent.c new file mode 100644 index 0000000000..1bf87a5dfa --- /dev/null +++ b/opentelemetry_api/opentelemetry/context/traceparent.c @@ -0,0 +1,106 @@ +/* Copyright 2019, OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "traceparent.h" + +status_t _uint_from_hex_string(unsigned int* value, const char* s) { + char c = s[0]; + unsigned int result; + + if (c >= '0' && c <= '9') + { + result = c - '0'; + } + else if (c >= 'a' && c <= 'f') + { + result = c - 'a' + 10; + } + else + { + return STATUS_ERROR_INPUT_ILLEGAL; + } + result <<= 4; + c = s[1]; + if (c >= '0' && c <= '9') + { + result += c - '0'; + } + else if (c >= 'a' && c <= 'f') + { + result += c - 'a' + 10; + } + else + { + return STATUS_ERROR_INPUT_ILLEGAL; + } + *value = result; + + return STATUS_OK; +} + +status_t _trace_parent_from_string(TraceParent* trace_parent, const char* s) +{ + unsigned int value; + /* if this function failed, trace_parent could be polluted, + do we want to take a copy (perf sucks)? + */ + IF_ERROR_RETURN(_uint_from_hex_string(&value, s)); + trace_parent->version = value; + s += 2; + IF_FALSE_RETURN('-' == *s, STATUS_ERROR_INPUT_ILLEGAL) + s += 1; + for (int i = 0; i < 16; i++) + { + IF_ERROR_RETURN(_uint_from_hex_string(&value, s)); + trace_parent->trace_id[i] = value; + s += 2; + } + IF_FALSE_RETURN('-' == *s, STATUS_ERROR_INPUT_ILLEGAL) + s += 1; + for (int i = 0; i < 8; i++) + { + IF_ERROR_RETURN(_uint_from_hex_string(&value, s)); + trace_parent->parent_id[i] = value; + s += 2; + } + IF_FALSE_RETURN('-' == *s, STATUS_ERROR_INPUT_ILLEGAL) + s += 1; + IF_ERROR_RETURN(_uint_from_hex_string(&value, s)); + trace_parent->trace_flags = value; + s += 2; + IF_FALSE_RETURN(('-' == *s || ('\0' == *s)), STATUS_ERROR_INPUT_ILLEGAL) + return STATUS_OK; +} + +status_t _trace_parent_to_string(const TraceParent* trace_parent, char* s) +{ + return STATUS_ERROR_UNKNOWN; +} + +status_t trace_parent_from_string(TraceParent* trace_parent, const char* s) +{ + IF_FALSE_RETURN(trace_parent, STATUS_ERROR_INPUT_NULL_PTR) + IF_FALSE_RETURN(s, STATUS_ERROR_INPUT_NULL_PTR) + + return _trace_parent_from_string(trace_parent, s); +} + +status_t trace_parent_to_string(const TraceParent* trace_parent, char* s) +{ + IF_FALSE_RETURN(trace_parent, STATUS_ERROR_INPUT_NULL_PTR) + IF_FALSE_RETURN(s, STATUS_ERROR_INPUT_NULL_PTR) + + return _trace_parent_to_string(trace_parent, s); +} diff --git a/opentelemetry_api/opentelemetry/context/traceparent.h b/opentelemetry_api/opentelemetry/context/traceparent.h new file mode 100644 index 0000000000..1ddc4fd7c1 --- /dev/null +++ b/opentelemetry_api/opentelemetry/context/traceparent.h @@ -0,0 +1,51 @@ +/* Copyright 2019, OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef OPENTELEMETRY_CONTEXT_TRACEPARENT_H_ +#define OPENTELEMETRY_CONTEXT_TRACEPARENT_H_ + +#include "../status.h" + +#ifdef __cplusplus +namespace opentelemetry { +namespace context { +extern "C" { +#endif + +#pragma pack(push, 1) +typedef struct +{ + uint8_t version; + uint8_t trace_id[16]; + uint8_t parent_id[8]; + uint8_t trace_flags; +} TraceParent; +#pragma pack(pop) + +/* unchecked version */ +status_t _trace_parent_from_string(TraceParent* trace_parent, const char* s); +status_t _trace_parent_to_string(const TraceParent* trace_parent, char* s); + +/* checked version */ +status_t trace_parent_from_string(TraceParent* trace_parent, const char* s); +status_t trace_parent_to_string(const TraceParent* trace_parent, char* s); + +#ifdef __cplusplus +} /* extern "C" */ +} /* namespace context */ +} /* namespace opentelemetry */ +#endif + +#endif /* OPENTELEMETRY_CONTEXT_TRACEPARENT_H_ */ diff --git a/opentelemetry_api/opentelemetry/context/tracestate.c b/opentelemetry_api/opentelemetry/context/tracestate.c new file mode 100644 index 0000000000..ab79ee8a6f --- /dev/null +++ b/opentelemetry_api/opentelemetry/context/tracestate.c @@ -0,0 +1,16 @@ +/* Copyright 2019, OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "tracestate.h" diff --git a/opentelemetry_api/opentelemetry/context/tracestate.h b/opentelemetry_api/opentelemetry/context/tracestate.h new file mode 100644 index 0000000000..a79c8a7582 --- /dev/null +++ b/opentelemetry_api/opentelemetry/context/tracestate.h @@ -0,0 +1,33 @@ +/* Copyright 2019, OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef OPENTELEMETRY_CONTEXT_TRACESTATE_H_ +#define OPENTELEMETRY_CONTEXT_TRACESTATE_H_ + +#include "../status.h" + +#ifdef __cplusplus +namespace opentelemetry { +namespace context { +extern "C" { +#endif + +#ifdef __cplusplus +} /* extern "C" */ +} /* namespace context */ +} /* namespace opentelemetry */ +#endif + +#endif /* OPENTELEMETRY_CONTEXT_TRACESTATE_H_ */ diff --git a/opentelemetry_api/opentelemetry/status.h b/opentelemetry_api/opentelemetry/status.h new file mode 100644 index 0000000000..d99e72668b --- /dev/null +++ b/opentelemetry_api/opentelemetry/status.h @@ -0,0 +1,41 @@ +/* Copyright 2019, OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef OPENTELEMETRY_STATUS_H_ +#define OPENTELEMETRY_STATUS_H_ + +#include + +typedef int32_t status_t; + +enum { + STATUS_OK = 0, + STATUS_ERROR_UNKNOWN = 0x00010000, + STATUS_ERROR_INPUT_NULL_PTR = 0x00010001, + STATUS_ERROR_INPUT_ILLEGAL = 0x00010002, +}; + +#define IF_ERROR_RETURN(expr) { \ + status_t status = (expr); \ + if (STATUS_OK != status) \ + return status; \ +} + +#define IF_FALSE_RETURN(expr, retval) { \ + if (!(expr)) \ + return (retval); \ +} + +#endif /* OPENTELEMETRY_STATUS_H_ */