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

Support to capture http headers and set as Span attributes. #224

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class RequestPayload
std::string http_request_method; /* Request method (eg. GET, HEAD, POST, etc.) */

std::unordered_map<std::string, std::string> http_headers; /* HTTP Request headers: Cookie, Referer, SM_USER*/
std::unordered_map<std::string, std::string> request_headers;

std::string server_name;
std::string scheme;
Expand All @@ -53,6 +54,10 @@ class RequestPayload
{
http_headers[key] = value;
}
void set_request_headers(const std::string& key, const std::string& value)
{
request_headers[key] = value;
}
void set_uri(const char* URI) { uri = URI; }
void set_request_protocol(const char* requestProtocol) {request_protocol = requestProtocol; }
void set_http_get_parameter(const char* httpGetParameter) {http_get_parameter = httpGetParameter; }
Expand Down Expand Up @@ -82,6 +87,14 @@ class RequestPayload
std::string get_client_ip() {return client_ip; }
long get_port() {return port; }
long get_status_code() {return status_code; }
std::unordered_map<std::string, std::string>& get_request_headers() {
return request_headers;
}
};

struct ResponsePayload
{
std::unordered_map<std::string, std::string> response_headers;
};

struct InteractionPayload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace core {

class TenantConfig;
class RequestPayload;
class ResponsePayload;
class InteractionPayload;
class EndInteractionPayload;

Expand All @@ -50,7 +51,8 @@ class IRequestProcessingEngine {
APPD_SDK_HANDLE_REQ* reqHandle) = 0;
virtual APPD_SDK_STATUS_CODE endRequest(
APPD_SDK_HANDLE_REQ reqHandle,
const char* error) = 0;
const char* error,
const ResponsePayload* payload = nullptr) = 0;
virtual APPD_SDK_STATUS_CODE startInteraction(
APPD_SDK_HANDLE_REQ reqHandle,
const InteractionPayload* payload,
Expand All @@ -75,7 +77,8 @@ class RequestProcessingEngine : public IRequestProcessingEngine {
APPD_SDK_HANDLE_REQ* reqHandle) override;
APPD_SDK_STATUS_CODE endRequest(
APPD_SDK_HANDLE_REQ reqHandle,
const char* error) override;
const char* error,
const ResponsePayload* payload = nullptr) override;
APPD_SDK_STATUS_CODE startInteraction(
APPD_SDK_HANDLE_REQ reqHandle,
const InteractionPayload* payload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class WSAgent

APPD_SDK_STATUS_CODE endRequest(
APPD_SDK_HANDLE_REQ reqHandle,
const char* error);
const char* error,
const ResponsePayload* payload = nullptr );

APPD_SDK_STATUS_CODE startInteraction(
APPD_SDK_HANDLE_REQ reqHandle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ typedef struct {
const char* http_get_param;
const char* http_post_param;
const char* request_method;
http_headers* headers;
http_headers* propagation_headers;
http_headers* request_headers;

int propagation_count;
int request_headers_count;
}request_payload;

typedef struct {
http_headers* response_headers;
int response_headers_count;
}response_payload;

typedef struct{
const char* cName;
const char* sNamespace;
Expand All @@ -56,12 +65,12 @@ struct cNode{
};

void initDependency();
void populatePayload(request_payload* req_payload, void* payload, int count);
void populatePayload(request_payload* req_payload, void* payload);
APPD_SDK_STATUS_CODE opentelemetry_core_init(APPD_SDK_ENV_RECORD* env, unsigned numberOfRecords, struct cNode *rootCN);
APPD_SDK_STATUS_CODE startRequest(const char* wscontext, request_payload* req_payload, APPD_SDK_HANDLE_REQ* reqHandle, int count);
APPD_SDK_STATUS_CODE startRequest(const char* wscontext, request_payload* req_payload, APPD_SDK_HANDLE_REQ* reqHandle);
APPD_SDK_STATUS_CODE startModuleInteraction(APPD_SDK_HANDLE_REQ req_handle_key, const char* module_name, const char* stage, bool resolveBackends, APPD_SDK_ENV_RECORD* propagationHeaders, int* ix);
APPD_SDK_STATUS_CODE stopModuleInteraction(APPD_SDK_HANDLE_REQ req_handle_key, const char* backendName, const char* backendType, unsigned int err_code, const char* msg);
APPD_SDK_STATUS_CODE endRequest(APPD_SDK_HANDLE_REQ req_handle_key, const char* errMsg);
APPD_SDK_STATUS_CODE endRequest(APPD_SDK_HANDLE_REQ req_handle_key, const char* errMsg, response_payload* payload);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ APPD_SDK_STATUS_CODE RequestProcessingEngine::startRequest(
keyValueMap[kAttrHTTPFlavor] = payload->get_flavor();
keyValueMap[kAttrHTTPStatusCode] = payload->get_status_code();
keyValueMap[kAttrHTTPClientIP] = payload->get_client_ip();

auto& request_headers = payload->get_request_headers();
for (auto itr = request_headers.begin(); itr != request_headers.end(); itr++) {
DebajitDas marked this conversation as resolved.
Show resolved Hide resolved
std::string key = "http.request.header." +
std::string(itr->first);
keyValueMap[key] = itr->second;
}
auto span = m_sdkWrapper->CreateSpan(spanName, sdkwrapper::SpanKind::SERVER, keyValueMap, payload->get_http_headers());

LOG4CXX_TRACE(mLogger, "Span started for context: [" << wscontext
Expand All @@ -89,7 +96,8 @@ APPD_SDK_STATUS_CODE RequestProcessingEngine::startRequest(

APPD_SDK_STATUS_CODE RequestProcessingEngine::endRequest(
APPD_SDK_HANDLE_REQ reqHandle,
const char* error) {
const char* error,
const ResponsePayload* payload) {

if (!reqHandle) {
LOG4CXX_ERROR(mLogger, "Invalid request, can't end request");
Expand Down Expand Up @@ -139,6 +147,14 @@ APPD_SDK_STATUS_CODE RequestProcessingEngine::endRequest(
} else {
rootSpan->SetStatus(StatusCode::Ok);
}
if (payload != nullptr) {
for (auto itr = payload->response_headers.begin();
itr != payload->response_headers.end(); itr++) {
std::string key = "http.response.header." +
DebajitDas marked this conversation as resolved.
Show resolved Hide resolved
std::string(itr->first);
rootSpan->AddAttribute(key, itr->second);
}
}

LOG4CXX_TRACE(mLogger, "Ending root span with id: " << rootSpan.get());
rootSpan->End();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ WSAgent::startRequest(
APPD_SDK_STATUS_CODE
WSAgent::endRequest(
APPD_SDK_HANDLE_REQ reqHandle,
const char* error)
const char* error, const ResponsePayload* payload)
{
// TODO: How to get the context here?
// one solution is get it from reqHandle.
Expand All @@ -121,7 +121,7 @@ WSAgent::endRequest(
auto *engine = mAgentCore->getRequestProcessor(context);
if (nullptr != engine)
{
return engine->endRequest(reqHandle, error);
return engine->endRequest(reqHandle, error, payload);
}
return APPD_STATUS(fail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

appd::core::WSAgent wsAgent; // global variable for interface between Hooks and Core Logic

void populatePayload(request_payload* req_payload, void* load, int count)
void populatePayload(request_payload* req_payload, void* load)
{
appd::core::RequestPayload* payload = (appd::core::RequestPayload*)load;
payload->set_uri(req_payload->uri);
Expand All @@ -31,8 +31,13 @@ void populatePayload(request_payload* req_payload, void* load, int count)
payload->set_http_get_parameter(req_payload->http_get_param);
payload->set_http_request_method(req_payload->request_method);

for(int i=0; i<count; i++){
payload->set_http_headers(req_payload->headers[i].name, req_payload->headers[i].value);
for(int i=0; i<req_payload->propagation_count; i++){
payload->set_http_headers(req_payload->propagation_headers[i].name, req_payload->propagation_headers[i].value);
}

for (int i = 0; i < req_payload->request_headers_count; i++) {
payload->set_request_headers(req_payload->request_headers[i].name,
req_payload->request_headers[i].value);
}
}

Expand Down Expand Up @@ -60,21 +65,32 @@ APPD_SDK_STATUS_CODE opentelemetry_core_init(APPD_SDK_ENV_RECORD* env, unsigned
return res;
}

APPD_SDK_STATUS_CODE startRequest(const char* wscontext, request_payload* req_payload, APPD_SDK_HANDLE_REQ* reqHandle, int count)
APPD_SDK_STATUS_CODE startRequest(const char* wscontext, request_payload* req_payload, APPD_SDK_HANDLE_REQ* reqHandle)
{
APPD_SDK_STATUS_CODE res = APPD_SUCCESS;

std::unique_ptr<appd::core::RequestPayload> requestPayload(new appd::core::RequestPayload);
populatePayload(req_payload, requestPayload.get(), count);
populatePayload(req_payload, requestPayload.get());
res = wsAgent.startRequest(wscontext, requestPayload.get(), reqHandle);

return res;
}

APPD_SDK_STATUS_CODE endRequest(APPD_SDK_HANDLE_REQ req_handle_key, const char* errMsg)
APPD_SDK_STATUS_CODE endRequest(APPD_SDK_HANDLE_REQ req_handle_key, const char* errMsg,
response_payload* payload)
{
APPD_SDK_STATUS_CODE res = APPD_SUCCESS;
res = wsAgent.endRequest(req_handle_key, errMsg);

std::unique_ptr<appd::core::ResponsePayload>
responsePayload(new appd::core::ResponsePayload);
if (payload != NULL) {
for (int i = 0; i < payload->response_headers_count; i++) {
responsePayload->response_headers[payload->response_headers[i].name]
= payload->response_headers[i].value;
}
}

res = wsAgent.endRequest(req_handle_key, errMsg, responsePayload.get());

return res;
}
Expand Down
Loading