From 3eafe7ecb755648d1b8af39fef5ff703c8cabd60 Mon Sep 17 00:00:00 2001 From: aryanishan1001 Date: Thu, 14 Mar 2024 12:26:14 +0000 Subject: [PATCH] Ignore paths feature implementation --- .vscode/c_cpp_properties.json | 16 ++++ .vscode/settings.json | 3 +- .../include/util/RegexResolver.h | 15 ++++ .../otel-webserver-module/src/build.gyp | 3 +- .../src/nginx/ngx_http_opentelemetry_module.c | 79 +++++++++++++++++-- .../src/nginx/ngx_http_opentelemetry_module.h | 3 + .../src/util/RegexResolver.cpp | 17 ++++ 7 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 instrumentation/otel-webserver-module/include/util/RegexResolver.h create mode 100644 instrumentation/otel-webserver-module/src/util/RegexResolver.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..a9f5fe195 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index a46286338..e1dc68adf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,6 +56,7 @@ "streambuf": "cpp", "thread": "cpp", "typeinfo": "cpp", - "opentelemetry_ngx_api.h": "c" + "opentelemetry_ngx_api.h": "c", + "opentelemetrysdk.h": "c" } } \ No newline at end of file diff --git a/instrumentation/otel-webserver-module/include/util/RegexResolver.h b/instrumentation/otel-webserver-module/include/util/RegexResolver.h new file mode 100644 index 000000000..e4a03b46f --- /dev/null +++ b/instrumentation/otel-webserver-module/include/util/RegexResolver.h @@ -0,0 +1,15 @@ +#ifndef REGEX_RESOLVER_H +#define REGEX_RESOLVER_H + +#ifdef __cplusplus +extern "C" { +#endif + + +bool matchIgnorePathRegex(char * uri , char * regexVar); + + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/instrumentation/otel-webserver-module/src/build.gyp b/instrumentation/otel-webserver-module/src/build.gyp index ac31bd3e3..67530acf9 100644 --- a/instrumentation/otel-webserver-module/src/build.gyp +++ b/instrumentation/otel-webserver-module/src/build.gyp @@ -28,7 +28,8 @@ 'core/sdkwrapper/ScopedSpan.cpp', 'core/sdkwrapper/ServerSpan.cpp', 'core/sdkwrapper/SdkWrapper.cpp', - 'util/SpanNamingUtils.cpp' + 'util/SpanNamingUtils.cpp', + 'util/RegexResolver.cpp' ], 'conditions': [ diff --git a/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.c b/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.c index 51e8c2fbb..bc7e055ef 100644 --- a/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.c +++ b/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.c @@ -16,6 +16,7 @@ #include "ngx_http_opentelemetry_module.h" #include "ngx_http_opentelemetry_log.h" +#include "../../include/util/RegexResolver.h" #include #include #include @@ -390,6 +391,13 @@ static ngx_command_t ngx_http_opentelemetry_commands[] = { NGX_HTTP_LOC_CONF_OFFSET, 0, NULL}, + + { ngx_string("NginxIgnorePaths"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, + ngx_conf_ignore_path_set, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL}, /* command termination */ ngx_null_command @@ -464,6 +472,7 @@ static char* ngx_http_opentelemetry_merge_loc_conf(ngx_conf_t *cf, void *parent, ngx_http_opentelemetry_loc_conf_t *conf = child; ngx_otel_set_global_context(prev); ngx_otel_set_attributes(prev, conf); + ngx_conf_merge_ignore_paths(prev, conf); ngx_conf_merge_value(conf->nginxModuleEnabled, prev->nginxModuleEnabled, 1); ngx_conf_merge_value(conf->nginxModuleReportAllInstrumentedModules, prev->nginxModuleReportAllInstrumentedModules, 0); @@ -683,6 +692,7 @@ static void ngx_http_opentelemetry_exit_worker(ngx_cycle_t *cycle) ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "mod_opentelemetry: ngx_http_opentelemetry_exit_worker: Exiting Nginx Worker for process with PID: %s**********", worker_conf->pid); } } + static char* ngx_otel_attributes_set(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) { ngx_http_opentelemetry_loc_conf_t * my_conf=(ngx_http_opentelemetry_loc_conf_t *)conf; @@ -712,6 +722,33 @@ static char* ngx_otel_attributes_set(ngx_conf_t* cf, ngx_command_t* cmd, void* c } +static char* ngx_conf_ignore_path_set(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) { + ngx_http_opentelemetry_loc_conf_t * my_conf=(ngx_http_opentelemetry_loc_conf_t *)conf; + + ngx_str_t *value = cf->args->elts; + + ngx_array_t *arr; + ngx_str_t *elt; + ngx_int_t arg_count = cf->args->nelts; + + arr = ngx_array_create(cf->pool, arg_count, sizeof(ngx_str_t)); + if (arr == NULL) { + return NGX_CONF_ERROR; + } + + for (ngx_int_t i = 1; i < arg_count; i++) { + elt = ngx_array_push(arr); + if (elt == NULL) { + return NGX_CONF_ERROR; + } + ngx_str_set(elt, value[i].data); + elt->len = ngx_strlen(value[i].data); + } + my_conf->nginxIgnorePaths = arr; + return NGX_CONF_OK; + +} + static char* ngx_otel_context_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ ngx_str_t* value; @@ -756,6 +793,20 @@ static void ngx_otel_set_attributes(ngx_http_opentelemetry_loc_conf_t * prev, ng return; } +static void ngx_conf_merge_ignore_paths(ngx_http_opentelemetry_loc_conf_t * prev, ngx_http_opentelemetry_loc_conf_t * conf) +{ + if (conf->nginxIgnorePaths && (conf->nginxIgnorePaths->nelts) > 0) { + return; + } + if (prev->nginxIgnorePaths && (prev->nginxIgnorePaths->nelts) > 0) { + if((conf->nginxIgnorePaths) == NULL) + { + conf->nginxIgnorePaths = prev->nginxIgnorePaths; + } + } + return; +} + /* Begin a new interaction */ @@ -980,10 +1031,25 @@ static void resolve_attributes_variables(ngx_http_request_t* r) static ngx_flag_t check_ignore_paths(ngx_http_request_t *r) { - ngx_http_opentelemetry_loc_conf_t conf = ngx_http_get_module_loc_conf(r, ngx_http_opentelemetry_module); - if(conf->NginixIgnorePaths){ - + ngx_http_opentelemetry_loc_conf_t * conf = ngx_http_get_module_loc_conf(r, ngx_http_opentelemetry_module); + + ngx_uint_t uriLen = r->uri.len; + char *pathToCheck = ngx_pnalloc(r->pool, uriLen + 1); + ngx_memcpy(pathToCheck, r->uri.data, uriLen); + pathToCheck[uriLen] = '\0'; + + if (conf->nginxIgnorePaths && (conf->nginxIgnorePaths->nelts) > 0) { + + for (ngx_uint_t j = 0; j < conf->nginxIgnorePaths->nelts; j++) { + const char* data = (const char*)(((ngx_str_t *)(conf->nginxIgnorePaths->elts))[j]).data; + + bool ans = matchIgnorePathRegex(pathToCheck , data); + if(ans){ + return true; + } + } } + return false; } static ngx_flag_t ngx_initialize_opentelemetry(ngx_http_request_t *r) @@ -1259,10 +1325,11 @@ static void startMonitoringRequest(ngx_http_request_t* r){ { ngx_writeError(r->connection->log, __func__, "Opentelemetry Agent Core did not get initialized"); return; - }else if(check_ignore_paths){ - ngx_writeTrace(r->connection->log, __func__, "This path is not allowed to be monitored"); - return; } + // else if(check_ignore_paths(r)){ + // ngx_writeTrace(r->connection->log, __func__, "This path is not allowed to be monitored"); + // return; + // } diff --git a/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.h b/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.h index 7253f130e..83c6113ed 100644 --- a/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.h +++ b/instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.h @@ -105,6 +105,7 @@ typedef struct { ngx_str_t nginxModuleOtelExporterOtlpHeaders; ngx_flag_t nginxTrustIncomingSpans; ngx_array_t *nginxAttributes; + ngx_array_t *nginxIgnorePaths; } ngx_http_opentelemetry_loc_conf_t; @@ -156,8 +157,10 @@ static ngx_flag_t otel_requestHasErrors(ngx_http_request_t* r); static ngx_uint_t otel_getErrorCode(ngx_http_request_t* r); static char* ngx_otel_context_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char* ngx_otel_attributes_set(ngx_conf_t* cf, ngx_command_t*, void* conf); +static char* ngx_conf_ignore_path_set(ngx_conf_t* cf, ngx_command_t* cmd, void* conf); static void ngx_otel_set_global_context(ngx_http_opentelemetry_loc_conf_t * prev); static void ngx_otel_set_attributes(ngx_http_opentelemetry_loc_conf_t * prev, ngx_http_opentelemetry_loc_conf_t * conf); +static void ngx_conf_merge_ignore_paths(ngx_http_opentelemetry_loc_conf_t * prev, ngx_http_opentelemetry_loc_conf_t * conf); static void removeUnwantedHeader(ngx_http_request_t* r); /* Module specific handler diff --git a/instrumentation/otel-webserver-module/src/util/RegexResolver.cpp b/instrumentation/otel-webserver-module/src/util/RegexResolver.cpp new file mode 100644 index 000000000..cae25efe7 --- /dev/null +++ b/instrumentation/otel-webserver-module/src/util/RegexResolver.cpp @@ -0,0 +1,17 @@ +#include "RegexResolver.h" +#include +#include + +bool matchIgnorePathRegex(char * uri , char * regexVar){ + const std::string uriString(uri); + std::string regexVarString(regexVar); + + std::cmatch match; + const std::regex pattern(regexVarString); + bool ans = std::regex_match(uriString, pattern); + if(ans){ + return true; + }else{ + return false; + } +} \ No newline at end of file