Skip to content

Commit

Permalink
fixes #364: Add option to set extra resource attributes on the traces…
Browse files Browse the repository at this point in the history
… in Apache and Nginx
  • Loading branch information
Roy Teeuwen committed Jan 23, 2024
1 parent a77c960 commit 5d1842c
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 41 deletions.
84 changes: 43 additions & 41 deletions instrumentation/otel-webserver-module/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ NginxModuleEnabled ON;
NginxModuleOtelSpanExporter otlp;
NginxModuleOtelExporterEndpoint docker.for.mac.localhost:4317;
#NginxModuleOtelExporterOtlpHeaders Authorization=AuthorizationToken;
NginxModuleOtelResourceAttributes some.key=value;
# SSL Certificates
#NginxModuleOtelSslEnabled ON
#NginxModuleOtelSslCertificatePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class otel_cfg
const char* getOtelExporterOtlpHeaders() { return otelExporterOtlpHeaders; }
int otelExporterOtlpHeadersInitialized() { return otelExporterOtlpHeaders_initialized; }

const char* getOtelResourceAttributes() { return otelResourceAttributes; }
int otelResourceAttributesInitialized() { return otelResourceAttributes_initialized; }

int getOtelSslEnabled() { return otelSslEnabled; }
int getOtelSslEnabledInitialized() { return otelSslEnabled_initialized; }

Expand Down Expand Up @@ -129,6 +132,9 @@ class otel_cfg
const char *otelExporterOtlpHeaders; // OPTIONAL: AppDynamics Custom metadata for OTEL Exporter EX: OTEL_EXPORTER_OTLP_HEADERS="api-key=key,other-config-value=value"
int otelExporterOtlpHeaders_initialized;

const char *otelResourceAttributes; // OPTIONAL: Custom resource attributes for OTEL Exporter EX: OTEL_RESOURCE_ATTRIBUTES="subsystem=key,other.value=value"
int otelResourceAttributes_initialized;

int otelSslEnabled; // OPTIONAL: Decision whether connection to the Exporter endpoint is secured
int otelSslEnabled_initialized;

Expand Down Expand Up @@ -231,6 +237,7 @@ class ApacheConfigHandlers
static const char* otel_set_otelExporterType(cmd_parms *cmd, void *conf, const char *arg);
static const char* otel_set_otelExporterEndpoint(cmd_parms *cmd, void *conf, const char *arg);
static const char* otel_set_otelExporterOtlpHeaders(cmd_parms *cmd, void *conf, const char *arg);
static const char* otel_set_otelResourceAttributes(cmd_parms *cmd, void *conf, const char *arg);
static const char* otel_set_otelSslEnabled(cmd_parms *cmd, void *conf, const char *arg);
static const char* otel_set_otelSslCertificatePath(cmd_parms *cmd, void *conf, const char *arg);
static const char* otel_set_otelProcessorType(cmd_parms *cmd, void *conf, const char *arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define OTEL_SDK_ENV_OTEL_EXPORTER_TYPE "OTEL_SDK_ENV_OTEL_EXPORTER_TYPE"
#define OTEL_SDK_ENV_OTEL_EXPORTER_ENDPOINT "OTEL_SDK_ENV_OTEL_EXPORTER_ENDPOINT" /*required*/
#define OTEL_SDK_ENV_OTEL_EXPORTER_OTLPHEADERS "OTEL_SDK_ENV_OTEL_EXPORTER_OTLPHEADERS" /*optional*/
#define OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES "OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES" /*optional*/
#define OTEL_SDK_ENV_OTEL_SSL_ENABLED "OTEL_SDK_ENV_OTEL_SSL_ENABLED" /*optional*/
#define OTEL_SDK_ENV_OTEL_SSL_CERTIFICATE_PATH "OTEL_SDK_ENV_OTEL_SSL_CERTIFICATE_PATH" /*optional*/
#define OTEL_SDK_ENV_OTEL_PROCESSOR_TYPE "OTEL_SDK_ENV_OTEL_PROCESSOR_TYPE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TenantConfig
const std::string& getOtelExporterType() const {return otelExporterType;}
const std::string& getOtelExporterEndpoint() const {return otelExporterEndpoint;}
const std::string& getOtelExporterOtlpHeaders() const {return otelExporterOtlpHeaders;}
const std::string& getOtelResourceAttributes() const {return otelResourceAttributes;}
const std::string& getOtelProcessorType() const {return otelProcessorType;}
const unsigned getOtelMaxQueueSize() const {return otelMaxQueueSize;}
const unsigned getOtelScheduledDelayMillis() const {return otelScheduledDelayMillis;}
Expand All @@ -63,6 +64,7 @@ class TenantConfig
void setOtelExporterType(const std::string& otelExporterType) { this->otelExporterType = otelExporterType; }
void setOtelExporterEndpoint(const std::string& otelExporterEndpoint) { this->otelExporterEndpoint = otelExporterEndpoint; }
void setOtelExporterOtlpHeaders(const std::string& otelExporterOtlpHeaders) { this->otelExporterOtlpHeaders = otelExporterOtlpHeaders; }
void setOtelResourceAttributes(const std::string& otelResourceAttributes) { this->otelResourceAttributes = otelResourceAttributes; }
void setOtelProcessorType(const std::string& otelProcessorType) { this->otelProcessorType = otelProcessorType; }
void setOtelMaxQueueSize(const unsigned int otelMaxQueueSize) { this->otelMaxQueueSize = otelMaxQueueSize; }
void setOtelScheduledDelayMillis(const unsigned int otelScheduledDelayMillis) { this->otelScheduledDelayMillis = otelScheduledDelayMillis; }
Expand All @@ -83,6 +85,7 @@ class TenantConfig
std::string otelExporterType;
std::string otelExporterEndpoint;
std::string otelExporterOtlpHeaders;
std::string otelResourceAttributes;
bool otelSslEnabled;
std::string otelSslCertPath;

Expand Down Expand Up @@ -112,6 +115,7 @@ inline std::ostream& operator<< (std::ostream &os, const otel::core::TenantConfi
<< "\n OtelSslEnabled " << config.getOtelSslEnabled()
<< "\n OtelSslCertPath " << config.getOtelSslCertPath()
<< "\n OtelExportOtlpHeaders " << config.getOtelExporterOtlpHeaders()
<< "\n OtelResourceAttributes " << config.getOtelResourceAttributes()
<< "";
return os;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ApacheModuleEnabled ON
ApacheModuleOtelSpanExporter otlp
ApacheModuleOtelExporterEndpoint collector:4317
#ApacheModuleOtelExporterHeaders api-key=abc123
#ApacheModuleOtelResourceAttributes some.key=value

# SSL Certificates
#ApacheModuleOtelSslEnabled ON
Expand Down
15 changes: 15 additions & 0 deletions instrumentation/otel-webserver-module/src/apache/ApacheConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ const char* ApacheConfigHandlers::otel_set_otelExporterOtlpHeaders(cmd_parms *cm
return helperChar(cmd, cfg, arg, cfg->otelExporterOtlpHeaders, cfg->otelExporterOtlpHeaders_initialized, "otel_set_otelExporterOtlpHeaders");
}

// char *otelResourceAttributes;
// int otelResourceAttributes_initialized;
const char* ApacheConfigHandlers::otel_set_otelResourceAttributes(cmd_parms *cmd, void *conf, const char *arg)
{
otel_cfg* cfg = (otel_cfg*) conf;
return helperChar(cmd, cfg, arg, cfg->otelResourceAttributes, cfg->otelResourceAttributes_initialized, "otel_set_otelResourceAttributes");
}

// char *otelSslEnabled;
// int otelSslEnabled_initialized;
const char* ApacheConfigHandlers::otel_set_otelSslEnabled(cmd_parms *cmd, void *conf, const char *arg)
Expand Down Expand Up @@ -444,6 +452,10 @@ void otel_cfg::init()
otelExporterOtlpHeaders = "";
otelExporterOtlpHeaders_initialized = 0;

// otelResourceAttributes Optional: OTLP resource attributes as key value pairs
otelResourceAttributes = "";
otelResourceAttributes_initialized = 0;

// otelSslEnabled OPTIONAL: Decides whether the connection to the endpoint is secured
otelSslEnabled = 0;
otelSslEnabled_initialized = 0;
Expand Down Expand Up @@ -786,6 +798,9 @@ otel_cfg* ApacheConfigHandlers::getProcessConfig(const request_rec* r)
process_cfg->otelExporterOtlpHeaders = apr_pstrdup(r->server->process->pool, our_config->otelExporterOtlpHeaders);
process_cfg->otelExporterOtlpHeaders_initialized = our_config->otelExporterOtlpHeaders_initialized;

process_cfg->otelResourceAttributes = apr_pstrdup(r->server->process->pool, our_config->otelResourceAttributes);
process_cfg->otelResourceAttributes_initialized = our_config->otelResourceAttributes_initialized;

process_cfg->otelSslEnabled = our_config->otelSslEnabled;
process_cfg->otelSslEnabled_initialized = our_config->otelSslEnabled_initialized;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ bool ApacheHooks::initialize_opentelemetry(const request_rec *r)
env_config[ix].value = our_config->getOtelExporterOtlpHeaders();
++ix;

// Resource attributes
env_config[ix].name = OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES;
env_config[ix].value = our_config->getOtelResourceAttributes();
++ix;

// !!!
// Remember to update the apr_pcalloc call size if we add another parameter to the input array!
// !!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ static const command_rec otel_cmds[] =
NULL,
OR_ALL,
"AppDynamics Otel export Headers key value pairs"),
AP_INIT_TAKE1(
"apacheModuleOtelResourceAttributes",
(CMD_HAND_TYPE)ApacheConfigHandlers::otel_set_otelResourceAttributes,
NULL,
OR_ALL,
"Otel resource attributes key value pairs"),
AP_INIT_TAKE1(
"apacheModuleOtelSslEnabled",
(CMD_HAND_TYPE)ApacheConfigHandlers::otel_set_otelSslEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ OTEL_SDK_STATUS_CODE ApiUtils::ReadSettingsFromReader(
std::string otelExporterType;
std::string otelExporterEndpoint;
std::string otelExporterOtlpHeaders;
std::string otelResourceAttributes;
bool otelSslEnabled;
std::string otelSslCertPath;
std::string otelLibraryName;
Expand Down Expand Up @@ -259,13 +260,17 @@ OTEL_SDK_STATUS_CODE ApiUtils::ReadSettingsFromReader(
reader.ReadOptional(
std::string(OTEL_SDK_ENV_OTEL_EXPORTER_OTLPHEADERS), otelExporterOtlpHeaders);

reader.ReadOptional(
std::string(OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES), otelResourceAttributes);


tenantConfig.setServiceNamespace(serviceNamespace);
tenantConfig.setServiceName(serviceName);
tenantConfig.setServiceInstanceId(serviceInstanceId);
tenantConfig.setOtelExporterType(otelExporterType);
tenantConfig.setOtelExporterEndpoint(otelExporterEndpoint);
tenantConfig.setOtelExporterOtlpHeaders(otelExporterOtlpHeaders);
tenantConfig.setOtelResourceAttributes(otelResourceAttributes);
tenantConfig.setOtelLibraryName(otelLibraryName);
tenantConfig.setOtelProcessorType(otelProcessorType);
tenantConfig.setOtelSamplerType(otelSamplerType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ SdkHelperFactory::SdkHelperFactory(
attributes[kServiceNamespace] = config->getServiceNamespace();
attributes[kServiceInstanceId] = config->getServiceInstanceId();

opentelemetry::common::KeyValueStringTokenizer tokenizer{config->getOtelResourceAttributes()};
opentelemetry::nostd::string_view resource_key;
opentelemetry::nostd::string_view resource_value;
bool resource_valid = true;

while (tokenizer.next(resource_valid, resource_key, resource_value))
{
if (resource_valid)
{
std::string key = static_cast<std::string>(resource_key);
std::string value = static_cast<std::string>(resource_value);
attributes[key] = value;
}
}


// NOTE : resource attribute values are nostd::variant and so we need to explicitely set it to std::string
std::string libraryVersion = MODULE_VERSION;
std::string cppSDKVersion = CPP_SDK_VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ static ngx_command_t ngx_http_opentelemetry_commands[] = {
offsetof(ngx_http_opentelemetry_loc_conf_t, nginxModuleOtelExporterOtlpHeaders),
NULL},

{ ngx_string("NginxModuleOtelResourceAttributes"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_opentelemetry_loc_conf_t, nginxModuleOtelResourceAttributes),
NULL},

{ ngx_string("NginxModuleOtelSpanProcessor"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
Expand Down Expand Up @@ -467,6 +474,7 @@ static char* ngx_http_opentelemetry_merge_loc_conf(ngx_conf_t *cf, void *parent,
ngx_conf_merge_str_value(conf->nginxModuleOtelSpanExporter, prev->nginxModuleOtelSpanExporter, "");
ngx_conf_merge_str_value(conf->nginxModuleOtelExporterEndpoint, prev->nginxModuleOtelExporterEndpoint, "");
ngx_conf_merge_str_value(conf->nginxModuleOtelExporterOtlpHeaders, prev->nginxModuleOtelExporterOtlpHeaders, "");
ngx_conf_merge_str_value(conf->nginxModuleOtelResourceAttributes, prev->nginxModuleOtelResourceAttributes, "");
ngx_conf_merge_value(conf->nginxModuleOtelSslEnabled, prev->nginxModuleOtelSslEnabled, 0);
ngx_conf_merge_str_value(conf->nginxModuleOtelSslCertificatePath, prev->nginxModuleOtelSslCertificatePath, "");
ngx_conf_merge_str_value(conf->nginxModuleOtelSpanProcessor, prev->nginxModuleOtelSpanProcessor, "");
Expand Down Expand Up @@ -959,6 +967,13 @@ static ngx_flag_t ngx_initialize_opentelemetry(ngx_http_request_t *r)
env_config[ix].value = (const char*)(conf->nginxModuleOtelExporterOtlpHeaders).data;
++ix;

// Otel Exporter Resource Attributes
env_config[ix].name = OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES;
env_config[ix].value = (const char*)(conf->nginxModuleOtelResourceAttributes).data;
++ix;

// TODO should resource attributes be added here?

// Otel SSL Enabled
env_config[ix].name = OTEL_SDK_ENV_OTEL_SSL_ENABLED;
env_config[ix].value = conf->nginxModuleOtelSslEnabled == 1 ? "1" : "0";
Expand Down Expand Up @@ -1493,6 +1508,7 @@ static void traceConfig(ngx_http_request_t *r, ngx_http_opentelemetry_loc_conf_t
conf->nginxModuleEnabled,
(conf->nginxModuleOtelExporterEndpoint).data,
(conf->nginxModuleOtelExporterOtlpHeaders).data,
(conf->nginxModuleOtelResourceAttributes).data,
conf->nginxModuleOtelSslEnabled,
(conf->nginxModuleOtelSslCertificatePath).data,
(conf->nginxModuleOtelSpanExporter).data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ typedef struct {
ngx_str_t nginxModuleRequestHeaders;
ngx_str_t nginxModuleResponseHeaders;
ngx_str_t nginxModuleOtelExporterOtlpHeaders;
ngx_str_t nginxModuleOtelResourceAttributes;
ngx_flag_t nginxTrustIncomingSpans;

} ngx_http_opentelemetry_loc_conf_t;
Expand Down

0 comments on commit 5d1842c

Please sign in to comment.