Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to add Nginx variables related to tracing context #416

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading