Skip to content

Commit 46c406e

Browse files
authored
Httpd add possibility to configure custom attributes and resources (#14)
1 parent 5d3c853 commit 46c406e

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

instrumentation/httpd/opentelemetry.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ OpenTelemetryPath /tmp/output-spans
3232

3333
# OpenTelemetryIgnoreInbound off
3434

35+
# OpenTelemetrySetResource service.name apache-web-server
36+
# OpenTelemetrySetAttribute foo bar
37+
3538
</IfModule>

instrumentation/httpd/src/otel/mod_otel.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static int opentel_log_transaction(request_rec *r)
172172
/////////////////////////////////////////////////
173173

174174
static int proxy_fixup_handler(request_rec *r)
175-
{ // adding outbound headers and setting span attribiutes
175+
{ // adding outbound headers and setting span attributes
176176
request_rec *req = r->main ? r->main : r;
177177

178178
ExtraRequestData *req_data;
@@ -252,7 +252,7 @@ static int proxy_end_handler(int *status, request_rec *r)
252252
}
253253

254254
int st_code = (status && *status) ? *status:r->status;
255-
char *proxy_error = apr_table_get(r->notes, "error-notes");
255+
const char *proxy_error = apr_table_get(r->notes, "error-notes");
256256
if (proxy_error)
257257
{
258258
req_data_out->span->SetStatus(opentelemetry::trace::StatusCode::kError, proxy_error);
@@ -316,6 +316,18 @@ const char *otel_set_endpoint(cmd_parms *cmd, void *cfg, const char *arg)
316316
return NULL;
317317
}
318318

319+
const char *otel_set_attribute(cmd_parms *cmd, void *cfg, const char *attrName, const char *attrValue)
320+
{
321+
config.attributes[attrName] = attrValue;
322+
return NULL;
323+
}
324+
325+
const char *otel_set_resource(cmd_parms *cmd, void *cfg, const char *attrName, const char *attrValue)
326+
{
327+
config.resources[attrName] = attrValue;
328+
return NULL;
329+
}
330+
319331
const char *otel_cfg_batch(cmd_parms *cmd,
320332
void *cfg,
321333
const char *max_queue_size,
@@ -357,6 +369,16 @@ static const command_rec opentel_directives[] = {
357369
NULL,
358370
RSRC_CONF,
359371
"Set endpoint for exporter"),
372+
AP_INIT_TAKE2("OpenTelemetrySetAttribute",
373+
otel_set_attribute,
374+
NULL,
375+
RSRC_CONF,
376+
"Set additional attribute for each span"),
377+
AP_INIT_TAKE2("OpenTelemetrySetResource",
378+
otel_set_resource,
379+
NULL,
380+
RSRC_CONF,
381+
"Set resource"),
360382
AP_INIT_TAKE3("OpenTelemetryBatch",
361383
otel_cfg_batch,
362384
NULL,

instrumentation/httpd/src/otel/opentelemetry.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ void initTracer()
8585
processor = std::make_shared<sdktrace::SimpleSpanProcessor>(std::move(exporter));
8686
}
8787

88+
// add custom-configured resources
89+
for(auto &it:config.resources)
90+
{
91+
resAttrs[it.first] = it.second;
92+
}
93+
8894
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
89-
new sdktrace::TracerProvider(processor));
95+
new sdktrace::TracerProvider(processor,
96+
opentelemetry::sdk::resource::Resource::Create(resAttrs))
97+
);
9098

9199
// Set the global trace provider
92100
opentelemetry::trace::Provider::SetTracerProvider(provider);
@@ -131,6 +139,11 @@ void ExtraRequestData::StartSpan(const HttpdStartSpanAttributes& attrs)
131139
{
132140
span->SetAttribute(kAttrNETPeerIP, startAttrs.net_ip);
133141
}
142+
// add custom-configured attributes
143+
for(auto &it:config.attributes)
144+
{
145+
span->SetAttribute(it.first, it.second);
146+
}
134147
}
135148

136149
void ExtraRequestData::EndSpan(const HttpdEndSpanAttributes& attrs)

instrumentation/httpd/src/otel/opentelemetry.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define HTTPD_OPENTELEMETRY_H_
1919

2020
#include <fstream>
21+
#include <unordered_map>
2122

2223
#include "opentelemetry/exporters/ostream/span_exporter.h"
2324
#include "opentelemetry/sdk/trace/batch_span_processor.h"
@@ -54,6 +55,8 @@ struct OtelConfig
5455
// context propagation
5556
bool ignore_inbound;
5657
OtelPropagation propagation;
58+
std::unordered_map<std::string, std::string> attributes;
59+
std::unordered_map<std::string, std::string> resources;
5760
OtelConfig() : ignore_inbound(true) {}
5861
};
5962

@@ -86,7 +89,7 @@ struct ExtraRequestData
8689
nostd::shared_ptr<opentelemetry::v0::trace::Span> span;
8790
HttpdStartSpanAttributes startAttrs;
8891
HttpdEndSpanAttributes endAttrs;
89-
// Sets attribiutes for HTTP request.
92+
// Sets attributes for HTTP request.
9093
void StartSpan(const HttpdStartSpanAttributes &attrs);
9194
void EndSpan(const HttpdEndSpanAttributes &attrs);
9295
// we are called from apache when apr_pool_t is being cleaned after request

instrumentation/httpd/tests/01-create-root-span.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ check_results() {
2727
check 'span kind' Server
2828
[ "`getSpanField span_id`" != "`getSpanField parent_span_id`" ] || fail "Bad span: span.id same as parent span.id"
2929

30-
echo Checking span attribiutes
30+
echo Checking span attributes
3131
declare -A SPAN_ATTRS
3232
# transforms "http.method: GET, http.flavor: http, ..." into SPAN_ATTRS[http.method] = GET
3333
IFS=',' read -ra my_array <<< "`getSpanField attributes`"

0 commit comments

Comments
 (0)