Skip to content

Commit

Permalink
Ignore paths feature implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanishan1001 committed Mar 14, 2024
1 parent bd72311 commit 3eafe7e
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 8 deletions.
16 changes: 16 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp",
"opentelemetry_ngx_api.h": "c"
"opentelemetry_ngx_api.h": "c",
"opentelemetrysdk.h": "c"
}
}
15 changes: 15 additions & 0 deletions instrumentation/otel-webserver-module/include/util/RegexResolver.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion instrumentation/otel-webserver-module/src/build.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "ngx_http_opentelemetry_module.h"
#include "ngx_http_opentelemetry_log.h"
#include "../../include/util/RegexResolver.h"
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
// }



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions instrumentation/otel-webserver-module/src/util/RegexResolver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "RegexResolver.h"
#include <string>
#include <regex>

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;
}
}

0 comments on commit 3eafe7e

Please sign in to comment.