Skip to content

Commit 0071c1d

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

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

instrumentation/otel-webserver-module/src/core/sdkwrapper/SdkHelperFactory.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ SdkHelperFactory::SdkHelperFactory(
6565
attributes[kServiceNamespace] = config->getServiceNamespace();
6666
attributes[kServiceInstanceId] = config->getServiceInstanceId();
6767

68+
opentelemetry::common::KeyValueStringTokenizer tokenizer{config->getOtelResourceAttributes()};
69+
opentelemetry::nostd::string_view resource_key;
70+
opentelemetry::nostd::string_view resource_value;
71+
bool resource_valid = true;
72+
73+
while (tokenizer.next(resource_valid, resource_key, resource_value))
74+
{
75+
if (resource_valid)
76+
{
77+
std::string key = static_cast<std::string>(resource_key);
78+
std::string value = static_cast<std::string>(resource_value);
79+
attributes[key] = value;
80+
}
81+
}
82+
83+
6884
// NOTE : resource attribute values are nostd::variant and so we need to explicitely set it to std::string
6985
std::string libraryVersion = MODULE_VERSION;
7086
std::string cppSDKVersion = CPP_SDK_VERSION;

instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ static ngx_command_t ngx_http_opentelemetry_commands[] = {
209209
offsetof(ngx_http_opentelemetry_loc_conf_t, nginxModuleOtelExporterOtlpHeaders),
210210
NULL},
211211

212+
{ ngx_string("NginxModuleOtelResourceAttributes"),
213+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
214+
ngx_conf_set_str_slot,
215+
NGX_HTTP_LOC_CONF_OFFSET,
216+
offsetof(ngx_http_opentelemetry_loc_conf_t, nginxModuleOtelResourceAttributes),
217+
NULL},
218+
212219
{ ngx_string("NginxModuleOtelSpanProcessor"),
213220
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
214221
ngx_conf_set_str_slot,
@@ -467,6 +474,7 @@ static char* ngx_http_opentelemetry_merge_loc_conf(ngx_conf_t *cf, void *parent,
467474
ngx_conf_merge_str_value(conf->nginxModuleOtelSpanExporter, prev->nginxModuleOtelSpanExporter, "");
468475
ngx_conf_merge_str_value(conf->nginxModuleOtelExporterEndpoint, prev->nginxModuleOtelExporterEndpoint, "");
469476
ngx_conf_merge_str_value(conf->nginxModuleOtelExporterOtlpHeaders, prev->nginxModuleOtelExporterOtlpHeaders, "");
477+
ngx_conf_merge_str_value(conf->nginxModuleOtelResourceAttributes, prev->nginxModuleOtelResourceAttributes, "");
470478
ngx_conf_merge_value(conf->nginxModuleOtelSslEnabled, prev->nginxModuleOtelSslEnabled, 0);
471479
ngx_conf_merge_str_value(conf->nginxModuleOtelSslCertificatePath, prev->nginxModuleOtelSslCertificatePath, "");
472480
ngx_conf_merge_str_value(conf->nginxModuleOtelSpanProcessor, prev->nginxModuleOtelSpanProcessor, "");
@@ -959,6 +967,13 @@ static ngx_flag_t ngx_initialize_opentelemetry(ngx_http_request_t *r)
959967
env_config[ix].value = (const char*)(conf->nginxModuleOtelExporterOtlpHeaders).data;
960968
++ix;
961969

970+
// Otel Exporter Resource Attributes
971+
env_config[ix].name = OTEL_SDK_ENV_OTEL_RESOURCE_ATTRIBUTES;
972+
env_config[ix].value = (const char*)(conf->nginxModuleOtelResourceAttributes).data;
973+
++ix;
974+
975+
// TODO should resource attributes be added here?
976+
962977
// Otel SSL Enabled
963978
env_config[ix].name = OTEL_SDK_ENV_OTEL_SSL_ENABLED;
964979
env_config[ix].value = conf->nginxModuleOtelSslEnabled == 1 ? "1" : "0";
@@ -1493,6 +1508,7 @@ static void traceConfig(ngx_http_request_t *r, ngx_http_opentelemetry_loc_conf_t
14931508
conf->nginxModuleEnabled,
14941509
(conf->nginxModuleOtelExporterEndpoint).data,
14951510
(conf->nginxModuleOtelExporterOtlpHeaders).data,
1511+
(conf->nginxModuleOtelResourceAttributes).data,
14961512
conf->nginxModuleOtelSslEnabled,
14971513
(conf->nginxModuleOtelSslCertificatePath).data,
14981514
(conf->nginxModuleOtelSpanExporter).data,

instrumentation/otel-webserver-module/src/nginx/ngx_http_opentelemetry_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ typedef struct {
103103
ngx_str_t nginxModuleRequestHeaders;
104104
ngx_str_t nginxModuleResponseHeaders;
105105
ngx_str_t nginxModuleOtelExporterOtlpHeaders;
106+
ngx_str_t nginxModuleOtelResourceAttributes;
106107
ngx_flag_t nginxTrustIncomingSpans;
107108

108109
} ngx_http_opentelemetry_loc_conf_t;

0 commit comments

Comments
 (0)