Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otel):add traces #2194

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: DoozyX/clang-format-lint-action@v0.12
with:
source: 'src plugins include tests'
exclude: 'include/neuron/utils/uthash.h include/neuron/utils/utarray.h include/neuron/utils/utlist.h include/neuron/utils/zlog.h'
exclude: 'include/neuron/utils/uthash.h include/neuron/utils/utarray.h include/neuron/utils/utlist.h include/neuron/utils/zlog.h src/otel/trace.pb-c.h src/otel/trace.pb-c.c src/otel/resource.pb-c.h src/otel/resource.pb-c.c src/otel/common.pb-c.c src/otel/common.pb-c.h'
clangFormatVersion: 10
style: file

Expand All @@ -36,7 +36,7 @@ jobs:
- name: cppcheck
run: |
sudo apt install cppcheck
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=unusedFunction ./plugins ./src
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=unusedFunction --suppress=*:src/otel/trace.pb-c.c --suppress=*:src/otel/common.pb-c.c --suppress=*:src/otel/resource.pb-c.c ./plugins ./src
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=variableScope ./simulator

ut:
Expand Down
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ set(PERSIST_SOURCES
src/persist/sqlite.c
src/persist/json/persist_json_plugin.c)
aux_source_directory(src/parser NEURON_SRC_PARSE)
aux_source_directory(src/otel NEURON_SRC_OTEL)
set(NEURON_BASE_SOURCES
src/base/tag.c
src/base/neu_plugin_common.c
Expand Down Expand Up @@ -109,12 +110,12 @@ endif()


add_library(neuron-base SHARED)
target_sources(neuron-base PRIVATE ${NEURON_BASE_SOURCES} ${NEURON_SRC_PARSE})
target_sources(neuron-base PRIVATE ${NEURON_BASE_SOURCES} ${NEURON_SRC_PARSE} ${NEURON_SRC_OTEL})
target_include_directories(neuron-base
PRIVATE include/neuron src)
target_link_libraries(neuron-base libssl.a libcrypto.a)
target_link_libraries(neuron-base nng libzlog.so jansson jwt
${CMAKE_THREAD_LIBS_INIT} -lm)
${CMAKE_THREAD_LIBS_INIT} -lm protobuf-c)
add_dependencies(neuron-base neuron-version)

# dependency imposed by nng
Expand Down Expand Up @@ -148,6 +149,7 @@ set(NEURON_SOURCES
plugins/restful/plugin_handle.c
plugins/restful/version_handle.c
plugins/restful/scan_handle.c
plugins/restful/otel_handle.c
plugins/restful/rest.c
plugins/restful/user.c)

Expand Down
18 changes: 18 additions & 0 deletions Install-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ $ tar xzf sqlite3.tar.gz --strip-components=1 -C sqlite3
$ cd sqlite3
$ ./configure CFLAGS=-fPIC && make && sudo make install
```

[protobuf](https://github.com/protocolbuffers/protobuf)
```shell
$ wget --no-check-certificate --content-disposition https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protobuf-cpp-3.20.1.tar.gz
$ tar -xzvf protobuf-cpp-3.20.1.tar.gz
$ cd protobuf-3.20.1
$ ./configure --enable-shared=no CFLAGS=-fPIC CXXFLAGS=-fPIC
$ make && make install
```

[protobuf-c](https://github.com/protobuf-c/protobuf-c.git)
```shell
$ git clone -b v1.4.0 https://github.com/protobuf-c/protobuf-c.git
$ cd protobuf-c
$ ./autogen.sh
$ ./configure --disable-protoc --enable-shared=no CFLAGS=-fPIC CXXFLAGS=-fPIC
$ make && make install
```
8 changes: 8 additions & 0 deletions codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ignore:
- "src/otel/*.pb-c.c"
coverage:
status:
project:
default:
target: auto # the required coverage value
threshold: 0.1% # the leniency in hitting the target
4 changes: 4 additions & 0 deletions include/neuron/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ extern int default_log_level;
extern bool disable_jwt;
extern char host_port[32];
extern char g_status[32];
extern bool otel_flag;
extern char otel_host[32];
extern int otel_port;
extern char otel_traces_url[32];

typedef enum neu_plugin_kind {
NEU_PLUGIN_KIND_STATIC = 0,
Expand Down
2 changes: 2 additions & 0 deletions include/neuron/utils/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ int neu_http_internal_error(nng_aio *aio, char *content);
int neu_http_response_file(nng_aio *aio, void *data, size_t len,
const char *disposition);

int neu_http_post_otel_trace(uint8_t *data, int len);

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions plugins/restful/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "log_handle.h"
#include "metric_handle.h"
#include "normal_handle.h"
#include "otel_handle.h"
#include "plugin_handle.h"
#include "rw_handle.h"
#include "scan_handle.h"
Expand Down Expand Up @@ -127,6 +128,9 @@ static struct neu_http_handler cors_handler[] = {
{
.url = "/api/v2/scan/tags",
},
{
.url = "/api/v2/otel",
},
};

static struct neu_http_handler rest_handlers[] = {
Expand Down Expand Up @@ -413,6 +417,12 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/status",
.value.handler = handle_status,
},
{
.method = NEU_HTTP_METHOD_POST,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/otel",
.value.handler = handle_otel,
},
};

void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size)
Expand Down
69 changes: 69 additions & 0 deletions plugins/restful/otel_handle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/

#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <jwt.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "persist/persist.h"
#include "user.h"
#include "utils/asprintf.h"
#include "utils/log.h"

#include "argparse.h"
#include "parser/neu_json_otel.h"
#include "json/neu_json_fn.h"

#include "handle.h"
#include "utils/http.h"
#include "utils/neu_jwt.h"

#include "otel/otel_manager.h"
#include "otel_handle.h"

void handle_otel(nng_aio *aio)
{
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_otel_conf_req_t, neu_json_decode_otel_conf_req, {
if (strcmp(req->action, "start") == 0) {
otel_flag = true;
strcpy(otel_host, req->host);
otel_port = req->port;
strcpy(otel_traces_url, req->traces_url);
neu_http_ok(aio, "{\"error\": 0 }");
neu_otel_start();
} else if (strcmp(req->action, "stop") == 0) {
otel_flag = false;
neu_http_ok(aio, "{\"error\": 0 }");
neu_otel_stop();
} else {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
neu_http_response(aio, NEU_ERR_PARAM_IS_WRONG,
result_error);
});
}
})
}
27 changes: 27 additions & 0 deletions plugins/restful/otel_handle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/

#ifndef _NEU_OTEL_HANDLE_H_
#define _NEU_OTEL_HANDLE_H_

#include <nng/nng.h>

void handle_otel(nng_aio *aio);

#endif
28 changes: 28 additions & 0 deletions plugins/restful/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
#include "global_config_handle.h"
#include "group_config_handle.h"
#include "handle.h"
#include "otel/otel_manager.h"
#include "plugin_handle.h"
#include "rest.h"
#include "rw_handle.h"
#include "scan_handle.h"
#include "utils/http.h"
#include "utils/log.h"
#include "utils/neu_jwt.h"
#include "utils/time.h"
#include "json/neu_json_fn.h"

#define neu_plugin_module default_dashboard_plugin_module
Expand Down Expand Up @@ -172,12 +174,37 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
return 0;
}

neu_otel_trace_ctx *trace = NULL;
neu_otel_scope_ctx scope = NULL;
if (otel_flag) {
trace = neu_otel_find_trace(header->ctx);
if (trace) {
scope = neu_otel_add_span(trace);
neu_otel_scope_set_span_name(scope, "rest response");
char new_span_id[36] = { 0 };
neu_otel_new_span_id(new_span_id);
neu_otel_scope_set_span_id(scope, new_span_id);
uint8_t *p_sp_id = neu_otel_scope_get_pre_span_id(scope);
if (p_sp_id) {
neu_otel_scope_set_parent_span_id2(scope, p_sp_id, 8);
}
neu_otel_scope_add_span_attr_int(scope, "thread id",
(int64_t)(pthread_self()));
neu_otel_scope_set_span_start_time(scope, neu_time_ms());
}
}

switch (header->type) {
case NEU_RESP_ERROR: {
neu_resp_error_t *error = (neu_resp_error_t *) data;
NEU_JSON_RESPONSE_ERROR(error->error, {
neu_http_response(header->ctx, error->error, result_error);
});
if (otel_flag && trace) {
neu_otel_scope_add_span_attr_int(scope, "error", error->error);
neu_otel_scope_set_span_end_time(scope, neu_time_ms());
neu_otel_trace_set_final(trace);
}
break;
}
case NEU_RESP_GET_PLUGIN:
Expand Down Expand Up @@ -241,6 +268,7 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
assert(false);
break;
}

return 0;
}

Expand Down
Loading
Loading