Skip to content

Commit 0071c1d

Browse files
royteeuwenRoy Teeuwen
authored andcommitted
fixes #364: Add option to set extra resource attributes on the traces in Apache and Nginx
1 parent 56c11f6 commit 0071c1d

File tree

13 files changed

+121
-41
lines changed

13 files changed

+121
-41
lines changed

instrumentation/otel-webserver-module/README.md

Lines changed: 43 additions & 41 deletions
Large diffs are not rendered by default.

instrumentation/otel-webserver-module/conf/nginx/opentelemetry_module.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ NginxModuleEnabled ON;
33
NginxModuleOtelSpanExporter otlp;
44
NginxModuleOtelExporterEndpoint docker.for.mac.localhost:4317;
55
#NginxModuleOtelExporterOtlpHeaders Authorization=AuthorizationToken;
6+
NginxModuleOtelResourceAttributes some.key=value;
67
# SSL Certificates
78
#NginxModuleOtelSslEnabled ON
89
#NginxModuleOtelSslCertificatePath

instrumentation/otel-webserver-module/include/apache/ApacheConfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class otel_cfg
4242
const char* getOtelExporterOtlpHeaders() { return otelExporterOtlpHeaders; }
4343
int otelExporterOtlpHeadersInitialized() { return otelExporterOtlpHeaders_initialized; }
4444

45+
const char* getOtelResourceAttributes() { return otelResourceAttributes; }
46+
int otelResourceAttributesInitialized() { return otelResourceAttributes_initialized; }
47+
4548
int getOtelSslEnabled() { return otelSslEnabled; }
4649
int getOtelSslEnabledInitialized() { return otelSslEnabled_initialized; }
4750

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

135+
const char *otelResourceAttributes; // OPTIONAL: Custom resource attributes for OTEL Exporter EX: OTEL_RESOURCE_ATTRIBUTES="subsystem=key,other.value=value"
136+
int otelResourceAttributes_initialized;
137+
132138
int otelSslEnabled; // OPTIONAL: Decision whether connection to the Exporter endpoint is secured
133139
int otelSslEnabled_initialized;
134140

@@ -231,6 +237,7 @@ class ApacheConfigHandlers
231237
static const char* otel_set_otelExporterType(cmd_parms *cmd, void *conf, const char *arg);
232238
static const char* otel_set_otelExporterEndpoint(cmd_parms *cmd, void *conf, const char *arg);
233239
static const char* otel_set_otelExporterOtlpHeaders(cmd_parms *cmd, void *conf, const char *arg);
240+
static const char* otel_set_otelResourceAttributes(cmd_parms *cmd, void *conf, const char *arg);
234241
static const char* otel_set_otelSslEnabled(cmd_parms *cmd, void *conf, const char *arg);
235242
static const char* otel_set_otelSslCertificatePath(cmd_parms *cmd, void *conf, const char *arg);
236243
static const char* otel_set_otelProcessorType(cmd_parms *cmd, void *conf, const char *arg);

instrumentation/otel-webserver-module/include/core/api/OpentelemetrySdk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define OTEL_SDK_ENV_OTEL_EXPORTER_TYPE "OTEL_SDK_ENV_OTEL_EXPORTER_TYPE"
2929
#define OTEL_SDK_ENV_OTEL_EXPORTER_ENDPOINT "OTEL_SDK_ENV_OTEL_EXPORTER_ENDPOINT" /*required*/
3030
#define OTEL_SDK_ENV_OTEL_EXPORTER_OTLPHEADERS "OTEL_SDK_ENV_OTEL_EXPORTER_OTLPHEADERS" /*optional*/
31+
#define OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES "OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES" /*optional*/
3132
#define OTEL_SDK_ENV_OTEL_SSL_ENABLED "OTEL_SDK_ENV_OTEL_SSL_ENABLED" /*optional*/
3233
#define OTEL_SDK_ENV_OTEL_SSL_CERTIFICATE_PATH "OTEL_SDK_ENV_OTEL_SSL_CERTIFICATE_PATH" /*optional*/
3334
#define OTEL_SDK_ENV_OTEL_PROCESSOR_TYPE "OTEL_SDK_ENV_OTEL_PROCESSOR_TYPE"

instrumentation/otel-webserver-module/include/core/api/TenantConfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class TenantConfig
4646
const std::string& getOtelExporterType() const {return otelExporterType;}
4747
const std::string& getOtelExporterEndpoint() const {return otelExporterEndpoint;}
4848
const std::string& getOtelExporterOtlpHeaders() const {return otelExporterOtlpHeaders;}
49+
const std::string& getOtelResourceAttributes() const {return otelResourceAttributes;}
4950
const std::string& getOtelProcessorType() const {return otelProcessorType;}
5051
const unsigned getOtelMaxQueueSize() const {return otelMaxQueueSize;}
5152
const unsigned getOtelScheduledDelayMillis() const {return otelScheduledDelayMillis;}
@@ -63,6 +64,7 @@ class TenantConfig
6364
void setOtelExporterType(const std::string& otelExporterType) { this->otelExporterType = otelExporterType; }
6465
void setOtelExporterEndpoint(const std::string& otelExporterEndpoint) { this->otelExporterEndpoint = otelExporterEndpoint; }
6566
void setOtelExporterOtlpHeaders(const std::string& otelExporterOtlpHeaders) { this->otelExporterOtlpHeaders = otelExporterOtlpHeaders; }
67+
void setOtelResourceAttributes(const std::string& otelResourceAttributes) { this->otelResourceAttributes = otelResourceAttributes; }
6668
void setOtelProcessorType(const std::string& otelProcessorType) { this->otelProcessorType = otelProcessorType; }
6769
void setOtelMaxQueueSize(const unsigned int otelMaxQueueSize) { this->otelMaxQueueSize = otelMaxQueueSize; }
6870
void setOtelScheduledDelayMillis(const unsigned int otelScheduledDelayMillis) { this->otelScheduledDelayMillis = otelScheduledDelayMillis; }
@@ -83,6 +85,7 @@ class TenantConfig
8385
std::string otelExporterType;
8486
std::string otelExporterEndpoint;
8587
std::string otelExporterOtlpHeaders;
88+
std::string otelResourceAttributes;
8689
bool otelSslEnabled;
8790
std::string otelSslCertPath;
8891

@@ -112,6 +115,7 @@ inline std::ostream& operator<< (std::ostream &os, const otel::core::TenantConfi
112115
<< "\n OtelSslEnabled " << config.getOtelSslEnabled()
113116
<< "\n OtelSslCertPath " << config.getOtelSslCertPath()
114117
<< "\n OtelExportOtlpHeaders " << config.getOtelExporterOtlpHeaders()
118+
<< "\n OtelResourceAttributes " << config.getOtelResourceAttributes()
115119
<< "";
116120
return os;
117121
}

instrumentation/otel-webserver-module/opentelemetry_module.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ApacheModuleEnabled ON
1515
ApacheModuleOtelSpanExporter otlp
1616
ApacheModuleOtelExporterEndpoint collector:4317
1717
#ApacheModuleOtelExporterHeaders api-key=abc123
18+
#ApacheModuleOtelResourceAttributes some.key=value
1819

1920
# SSL Certificates
2021
#ApacheModuleOtelSslEnabled ON

instrumentation/otel-webserver-module/src/apache/ApacheConfig.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ const char* ApacheConfigHandlers::otel_set_otelExporterOtlpHeaders(cmd_parms *cm
129129
return helperChar(cmd, cfg, arg, cfg->otelExporterOtlpHeaders, cfg->otelExporterOtlpHeaders_initialized, "otel_set_otelExporterOtlpHeaders");
130130
}
131131

132+
// char *otelResourceAttributes;
133+
// int otelResourceAttributes_initialized;
134+
const char* ApacheConfigHandlers::otel_set_otelResourceAttributes(cmd_parms *cmd, void *conf, const char *arg)
135+
{
136+
otel_cfg* cfg = (otel_cfg*) conf;
137+
return helperChar(cmd, cfg, arg, cfg->otelResourceAttributes, cfg->otelResourceAttributes_initialized, "otel_set_otelResourceAttributes");
138+
}
139+
132140
// char *otelSslEnabled;
133141
// int otelSslEnabled_initialized;
134142
const char* ApacheConfigHandlers::otel_set_otelSslEnabled(cmd_parms *cmd, void *conf, const char *arg)
@@ -444,6 +452,10 @@ void otel_cfg::init()
444452
otelExporterOtlpHeaders = "";
445453
otelExporterOtlpHeaders_initialized = 0;
446454

455+
// otelResourceAttributes Optional: OTLP resource attributes as key value pairs
456+
otelResourceAttributes = "";
457+
otelResourceAttributes_initialized = 0;
458+
447459
// otelSslEnabled OPTIONAL: Decides whether the connection to the endpoint is secured
448460
otelSslEnabled = 0;
449461
otelSslEnabled_initialized = 0;
@@ -786,6 +798,9 @@ otel_cfg* ApacheConfigHandlers::getProcessConfig(const request_rec* r)
786798
process_cfg->otelExporterOtlpHeaders = apr_pstrdup(r->server->process->pool, our_config->otelExporterOtlpHeaders);
787799
process_cfg->otelExporterOtlpHeaders_initialized = our_config->otelExporterOtlpHeaders_initialized;
788800

801+
process_cfg->otelResourceAttributes = apr_pstrdup(r->server->process->pool, our_config->otelResourceAttributes);
802+
process_cfg->otelResourceAttributes_initialized = our_config->otelResourceAttributes_initialized;
803+
789804
process_cfg->otelSslEnabled = our_config->otelSslEnabled;
790805
process_cfg->otelSslEnabled_initialized = our_config->otelSslEnabled_initialized;
791806

instrumentation/otel-webserver-module/src/apache/ApacheHooks.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ bool ApacheHooks::initialize_opentelemetry(const request_rec *r)
431431
env_config[ix].value = our_config->getOtelExporterOtlpHeaders();
432432
++ix;
433433

434+
// Resource attributes
435+
env_config[ix].name = OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES;
436+
env_config[ix].value = our_config->getOtelResourceAttributes();
437+
++ix;
438+
434439
// !!!
435440
// Remember to update the apr_pcalloc call size if we add another parameter to the input array!
436441
// !!!

instrumentation/otel-webserver-module/src/apache/mod_apache_otel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ static const command_rec otel_cmds[] =
7070
NULL,
7171
OR_ALL,
7272
"AppDynamics Otel export Headers key value pairs"),
73+
AP_INIT_TAKE1(
74+
"apacheModuleOtelResourceAttributes",
75+
(CMD_HAND_TYPE)ApacheConfigHandlers::otel_set_otelResourceAttributes,
76+
NULL,
77+
OR_ALL,
78+
"Otel resource attributes key value pairs"),
7379
AP_INIT_TAKE1(
7480
"apacheModuleOtelSslEnabled",
7581
(CMD_HAND_TYPE)ApacheConfigHandlers::otel_set_otelSslEnabled,

instrumentation/otel-webserver-module/src/core/api/ApiUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ OTEL_SDK_STATUS_CODE ApiUtils::ReadSettingsFromReader(
168168
std::string otelExporterType;
169169
std::string otelExporterEndpoint;
170170
std::string otelExporterOtlpHeaders;
171+
std::string otelResourceAttributes;
171172
bool otelSslEnabled;
172173
std::string otelSslCertPath;
173174
std::string otelLibraryName;
@@ -259,13 +260,17 @@ OTEL_SDK_STATUS_CODE ApiUtils::ReadSettingsFromReader(
259260
reader.ReadOptional(
260261
std::string(OTEL_SDK_ENV_OTEL_EXPORTER_OTLPHEADERS), otelExporterOtlpHeaders);
261262

263+
reader.ReadOptional(
264+
std::string(OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES), otelResourceAttributes);
265+
262266

263267
tenantConfig.setServiceNamespace(serviceNamespace);
264268
tenantConfig.setServiceName(serviceName);
265269
tenantConfig.setServiceInstanceId(serviceInstanceId);
266270
tenantConfig.setOtelExporterType(otelExporterType);
267271
tenantConfig.setOtelExporterEndpoint(otelExporterEndpoint);
268272
tenantConfig.setOtelExporterOtlpHeaders(otelExporterOtlpHeaders);
273+
tenantConfig.setOtelResourceAttributes(otelResourceAttributes);
269274
tenantConfig.setOtelLibraryName(otelLibraryName);
270275
tenantConfig.setOtelProcessorType(otelProcessorType);
271276
tenantConfig.setOtelSamplerType(otelSamplerType);

0 commit comments

Comments
 (0)