Skip to content

Commit b756753

Browse files
Feature to add Nginx variables related to tracing context (#416)
* truezz * try2 * working * oka * dfs * Nginx tracing context variables added * changes * chamgess * merge with main * before merge * Feature to include tracing info as nginx variables * Improved code quality, added Readme * Improved code quality2 * code quality improvement 3
1 parent f0dcbe9 commit b756753

File tree

10 files changed

+329
-29
lines changed

10 files changed

+329
-29
lines changed

instrumentation/otel-webserver-module/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ Currently, Nginx Webserver module monitores some fixed set of modules, which get
182182
|*NginxModulePropagatorType* | w3c | OPTIONAL: Specify the Propagator used by the instrumentation (W3C and B3 propagators available). e.g.```NginxModulePropagatorType b3;```|
183183
|*NginxModuleOperationName* | | OPTIONAL: Specify the operation name (span name) for any specific endpoint. e.g.```NginxModuleOperationName My_Backend;```|
184184

185+
#### Other Configurations
185186

186-
187+
- Nginx variables related to traceing info - $opentelemetry_trace_id , $opentelemetry_span_id $opentelemetry_context_traceparent , $opentelemetry_context_b3
187188

188189
### Build and Installation
189190
#### Prerequisites

instrumentation/otel-webserver-module/include/core/sdkwrapper/ISdkWrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class ISdkWrapper {
3838
const SpanKind& kind,
3939
const OtelKeyValueMap& attributes,
4040
const std::unordered_map<std::string, std::string>& carrier = {}) = 0;
41+
42+
virtual std::string ReturnCurrentSpanId() = 0;
4143

4244
virtual void PopulatePropagationHeaders(
4345
std::unordered_map<std::string, std::string>& carrier) = 0;

instrumentation/otel-webserver-module/include/core/sdkwrapper/SdkWrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class SdkWrapper : public ISdkWrapper{
4141
void PopulatePropagationHeaders(
4242
std::unordered_map<std::string, std::string>& carrier) override;
4343

44+
std::string ReturnCurrentSpanId() override;
45+
4446
private:
4547
trace::SpanKind GetTraceSpanKind(const SpanKind& kind);
4648

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ OTEL_SDK_STATUS_CODE RequestProcessingEngine::startRequest(
8686
}
8787

8888
auto span = m_sdkWrapper->CreateSpan(spanName, sdkwrapper::SpanKind::SERVER, keyValueMap, payload->get_http_headers());
89-
89+
9090
LOG4CXX_TRACE(mLogger, "Span started for context: [" << wscontext
9191
<<"] SpanName: " << spanName << ", RequestProtocol: " << payload->get_request_protocol()
9292
<<" SpanId: " << span.get());
@@ -200,10 +200,12 @@ OTEL_SDK_STATUS_CODE RequestProcessingEngine::startInteraction(
200200
// TODO : confirm and update name later
201201
std::string spanName = payload->moduleName + "_" + payload->phaseName;
202202
keyValueMap["interactionType"] = "EXIT_CALL";
203+
std::string parentSpanId = m_sdkWrapper->ReturnCurrentSpanId();
203204
auto interactionSpan = m_sdkWrapper->CreateSpan(spanName, SpanKind::CLIENT, keyValueMap);
204205
LOG4CXX_TRACE(mLogger, "Client Span started with SpanName: " << spanName
205206
<< " Span Id: " << interactionSpan.get());
206207
m_sdkWrapper->PopulatePropagationHeaders(propagationHeaders);
208+
propagationHeaders["Parent_Span_Id"] = parentSpanId;
207209

208210
// Add the interaction to the request context.
209211
requestContext->addInteraction(interactionSpan);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ OTEL_SDK_STATUS_CODE startModuleInteraction(OTEL_SDK_HANDLE_REQ req_handle_key,
179179
propagationHeaders[*ix].name = temp_key;
180180
char *temp_value= (char*)malloc(itr->second.size() + 1);
181181
std::strcpy(temp_value, itr->second.c_str());
182-
propagationHeaders[*ix].value = temp_value;
182+
propagationHeaders[*ix].value = temp_value;
183183
++(*ix);
184184
}
185185
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ ScopedSpan::ScopedSpan(
3030
{
3131
trace::StartSpanOptions options{};
3232
options.kind = kind;
33+
3334
mSpan = sdkHelperFactory->GetTracer()->StartSpan(name, attributes, options);
35+
3436
mScope.reset(new trace::Scope(mSpan));
3537
mSpanKind = kind;
3638
}

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,37 @@ std::shared_ptr<IScopedSpan> SdkWrapper::CreateSpan(
6666
mLogger));
6767
}
6868
}
69-
69+
std::string SdkWrapper::ReturnCurrentSpanId(){
70+
71+
auto context = context::RuntimeContext::GetCurrent();
72+
auto currentSpan = trace::GetSpan(context);
73+
trace::SpanContext spanContext = currentSpan->GetContext();
74+
trace::SpanId spanId = spanContext.span_id();
75+
constexpr int len = 2 * trace::SpanId::kSize;
76+
char* data = new char[len];
77+
spanId.ToLowerBase16(nostd::span<char, len>{data, len});
78+
std::string currentSpanId(data, len);
79+
delete[] data;
80+
return currentSpanId;
81+
}
7082
void SdkWrapper::PopulatePropagationHeaders(
7183
std::unordered_map<std::string, std::string>& carrier) {
7284

7385
// TODO : This is inefficient change as we are copying otel carrier data
7486
// into unordered map and sending it back to agent.
7587
// Ideally agent should keep otelCarrier data structure on its side.
76-
auto otelCarrier = OtelCarrier();
88+
auto otelCarrier = OtelCarrier();
7789
auto context = context::RuntimeContext::GetCurrent();
7890
for (auto &propagators : mSdkHelperFactory->GetPropagators()) {
7991
propagators->Inject(otelCarrier, context);
8092
}
81-
82-
// copy all relevant kv pairs into carrier
83-
for (int i = 0; i < CARRIER_HEADER_LEN; i++) {
84-
auto carrier_header = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
85-
if(carrier_header != ""){
86-
carrier[CARRIER_HEADER_NAME[i]] = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
93+
// copy all relevant kv pairs into carrier
94+
for (int i = 0; i < CARRIER_HEADER_LEN; i++) {
95+
auto carrier_header = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
96+
if(carrier_header != ""){
97+
carrier[CARRIER_HEADER_NAME[i]] = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
98+
}
8799
}
88-
}
89100
}
90101

91102
trace::SpanKind SdkWrapper::GetTraceSpanKind(const SpanKind& kind)

0 commit comments

Comments
 (0)