Skip to content

Commit

Permalink
Feature to add Nginx variables related to tracing context (#416)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aryanishan1001 authored Apr 29, 2024
1 parent f0dcbe9 commit b756753
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 29 deletions.
3 changes: 2 additions & 1 deletion instrumentation/otel-webserver-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ Currently, Nginx Webserver module monitores some fixed set of modules, which get
|*NginxModulePropagatorType* | w3c | OPTIONAL: Specify the Propagator used by the instrumentation (W3C and B3 propagators available). e.g.```NginxModulePropagatorType b3;```|
|*NginxModuleOperationName* | | OPTIONAL: Specify the operation name (span name) for any specific endpoint. e.g.```NginxModuleOperationName My_Backend;```|

#### Other Configurations


- Nginx variables related to traceing info - $opentelemetry_trace_id , $opentelemetry_span_id $opentelemetry_context_traceparent , $opentelemetry_context_b3

### Build and Installation
#### Prerequisites
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ISdkWrapper {
const SpanKind& kind,
const OtelKeyValueMap& attributes,
const std::unordered_map<std::string, std::string>& carrier = {}) = 0;

virtual std::string ReturnCurrentSpanId() = 0;

virtual void PopulatePropagationHeaders(
std::unordered_map<std::string, std::string>& carrier) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class SdkWrapper : public ISdkWrapper{
void PopulatePropagationHeaders(
std::unordered_map<std::string, std::string>& carrier) override;

std::string ReturnCurrentSpanId() override;

private:
trace::SpanKind GetTraceSpanKind(const SpanKind& kind);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ OTEL_SDK_STATUS_CODE RequestProcessingEngine::startRequest(
}

auto span = m_sdkWrapper->CreateSpan(spanName, sdkwrapper::SpanKind::SERVER, keyValueMap, payload->get_http_headers());

LOG4CXX_TRACE(mLogger, "Span started for context: [" << wscontext
<<"] SpanName: " << spanName << ", RequestProtocol: " << payload->get_request_protocol()
<<" SpanId: " << span.get());
Expand Down Expand Up @@ -200,10 +200,12 @@ OTEL_SDK_STATUS_CODE RequestProcessingEngine::startInteraction(
// TODO : confirm and update name later
std::string spanName = payload->moduleName + "_" + payload->phaseName;
keyValueMap["interactionType"] = "EXIT_CALL";
std::string parentSpanId = m_sdkWrapper->ReturnCurrentSpanId();
auto interactionSpan = m_sdkWrapper->CreateSpan(spanName, SpanKind::CLIENT, keyValueMap);
LOG4CXX_TRACE(mLogger, "Client Span started with SpanName: " << spanName
<< " Span Id: " << interactionSpan.get());
m_sdkWrapper->PopulatePropagationHeaders(propagationHeaders);
propagationHeaders["Parent_Span_Id"] = parentSpanId;

// Add the interaction to the request context.
requestContext->addInteraction(interactionSpan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ OTEL_SDK_STATUS_CODE startModuleInteraction(OTEL_SDK_HANDLE_REQ req_handle_key,
propagationHeaders[*ix].name = temp_key;
char *temp_value= (char*)malloc(itr->second.size() + 1);
std::strcpy(temp_value, itr->second.c_str());
propagationHeaders[*ix].value = temp_value;
propagationHeaders[*ix].value = temp_value;
++(*ix);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ ScopedSpan::ScopedSpan(
{
trace::StartSpanOptions options{};
options.kind = kind;

mSpan = sdkHelperFactory->GetTracer()->StartSpan(name, attributes, options);

mScope.reset(new trace::Scope(mSpan));
mSpanKind = kind;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,37 @@ std::shared_ptr<IScopedSpan> SdkWrapper::CreateSpan(
mLogger));
}
}

std::string SdkWrapper::ReturnCurrentSpanId(){

auto context = context::RuntimeContext::GetCurrent();
auto currentSpan = trace::GetSpan(context);
trace::SpanContext spanContext = currentSpan->GetContext();
trace::SpanId spanId = spanContext.span_id();
constexpr int len = 2 * trace::SpanId::kSize;
char* data = new char[len];
spanId.ToLowerBase16(nostd::span<char, len>{data, len});
std::string currentSpanId(data, len);
delete[] data;
return currentSpanId;
}
void SdkWrapper::PopulatePropagationHeaders(
std::unordered_map<std::string, std::string>& carrier) {

// TODO : This is inefficient change as we are copying otel carrier data
// into unordered map and sending it back to agent.
// Ideally agent should keep otelCarrier data structure on its side.
auto otelCarrier = OtelCarrier();
auto otelCarrier = OtelCarrier();
auto context = context::RuntimeContext::GetCurrent();
for (auto &propagators : mSdkHelperFactory->GetPropagators()) {
propagators->Inject(otelCarrier, context);
}

// copy all relevant kv pairs into carrier
for (int i = 0; i < CARRIER_HEADER_LEN; i++) {
auto carrier_header = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
if(carrier_header != ""){
carrier[CARRIER_HEADER_NAME[i]] = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
// copy all relevant kv pairs into carrier
for (int i = 0; i < CARRIER_HEADER_LEN; i++) {
auto carrier_header = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
if(carrier_header != ""){
carrier[CARRIER_HEADER_NAME[i]] = otelCarrier.Get(CARRIER_HEADER_NAME[i]).data();
}
}
}
}

trace::SpanKind SdkWrapper::GetTraceSpanKind(const SpanKind& kind)
Expand Down
Loading

0 comments on commit b756753

Please sign in to comment.