-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathRequestProcessingEngine.cpp
242 lines (196 loc) · 8.72 KB
/
RequestProcessingEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright 2021 AppDynamics LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "api/RequestProcessingEngine.h"
#include "AgentLogger.h"
#include "api/ApiUtils.h"
#include "api/Payload.h"
#include "sdkwrapper/SdkWrapper.h"
#include "sdkwrapper/IScopedSpan.h"
#include "sdkwrapper/SdkConstants.h"
#include <sys/types.h>
#include <unistd.h>
#include <boost/filesystem.hpp>
#include <boost/thread/locks.hpp>
#include <log4cxx/logger.h>
#include <unordered_map>
#include <sstream>
namespace appd {
namespace core {
using namespace sdkwrapper;
RequestProcessingEngine::RequestProcessingEngine()
: mLogger(getLogger(std::string(LogContext::AGENT) + ".RequestProcessingEngine"))
{
}
void RequestProcessingEngine::init(std::shared_ptr<TenantConfig>& config,
std::shared_ptr<SpanNamer> spanNamer) {
m_sdkWrapper.reset(new sdkwrapper::SdkWrapper());
m_sdkWrapper->Init(config);
m_spanNamer = spanNamer;
}
APPD_SDK_STATUS_CODE RequestProcessingEngine::startRequest(
const std::string& wscontext,
RequestPayload* payload,
APPD_SDK_HANDLE_REQ* reqHandle) {
if (!reqHandle) {
LOG4CXX_ERROR(mLogger, __FUNCTION__ << " " << APPD_STATUS(handle_pointer_is_null));
return APPD_STATUS(handle_pointer_is_null);
}
if (!payload) {
LOG4CXX_ERROR(mLogger, __FUNCTION__ << " " << APPD_STATUS(payload_reflector_is_null));
return APPD_STATUS(payload_reflector_is_null);
}
std::string spanName = m_spanNamer->getSpanName(payload->get_uri());
appd::core::sdkwrapper::OtelKeyValueMap keyValueMap;
keyValueMap["request_protocol"] = payload->get_request_protocol();
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());
RequestContext* requestContext = new RequestContext(span);
requestContext->setContextName(wscontext);
// Fill the requestHandle
*reqHandle = (void*)requestContext;
return APPD_SUCCESS;
}
APPD_SDK_STATUS_CODE RequestProcessingEngine::endRequest(
APPD_SDK_HANDLE_REQ reqHandle,
const char* error) {
if (!reqHandle) {
LOG4CXX_ERROR(mLogger, "Invalid request, can't end request");
return APPD_STATUS(fail);
}
RequestContext* requestContext = (RequestContext*)reqHandle;
auto rootSpan = requestContext->rootSpan();
// End all the active interactions one by one if they aren't closed.
// Even if we end parent span, child spans might remain active. We need
// to ensure that all client/internal spans are closed (in case of error in request
// they might not be explicitely closed)
while (requestContext->hasActiveInteraction()) {
auto interactionSpan = requestContext->lastActiveInteraction();
LOG4CXX_TRACE(mLogger, "Ending Span with id: " << interactionSpan.get());
interactionSpan->End();
requestContext->removeInteraction();
}
// check for error and set attribute in the scopedSpan.
if (error) {
std::stringstream strValue;
unsigned int errorValue;
strValue << error;
strValue >> errorValue;
std::string errorStatus (kHttpErrorCode + error); // This is status message eg: HTTP ERROR CODE:403
if (errorValue >= HTTP_ERROR_1XX && errorValue < HTTP_ERROR_4XX ) {
rootSpan->SetStatus(StatusCode::Unset);
}
else if (errorValue >= HTTP_ERROR_4XX && errorValue < HTTP_ERROR_5XX ) {
if (rootSpan->GetSpanKind() == SpanKind::SERVER)
rootSpan->SetStatus(StatusCode::Unset);
else
rootSpan->SetStatus(StatusCode::Error, errorStatus);
} else {
rootSpan->SetStatus(StatusCode::Error, errorStatus);
}
LOG4CXX_TRACE(mLogger, "Setting status as error[" << errorStatus <<"] on root Span");
} else {
rootSpan->SetStatus(StatusCode::Ok);
}
LOG4CXX_TRACE(mLogger, "Ending root span with id: " << rootSpan.get());
rootSpan->End();
delete requestContext;
return APPD_SUCCESS;
}
APPD_SDK_STATUS_CODE RequestProcessingEngine::startInteraction(
APPD_SDK_HANDLE_REQ reqHandle,
const InteractionPayload* payload,
std::unordered_map<std::string, std::string>& propagationHeaders) {
if (!reqHandle) {
LOG4CXX_ERROR(mLogger, __FUNCTION__ << " " << APPD_STATUS(handle_pointer_is_null));
return APPD_STATUS(handle_pointer_is_null);
}
if (!payload) {
LOG4CXX_ERROR(mLogger, __FUNCTION__ << " " << APPD_STATUS(payload_reflector_is_null));
return APPD_STATUS(payload_reflector_is_null);
}
RequestContext* requestContext = (RequestContext*)reqHandle;
// TODO : Add internal spans for virtual hosts post MVP
// Create client span for this interaction. And set it in RequestContext object.
appd::core::sdkwrapper::OtelKeyValueMap keyValueMap;
// TODO : confirm and update name later
std::string spanName = payload->moduleName + "_" + payload->phaseName;
keyValueMap["interactionType"] = "EXIT_CALL";
auto interactionSpan = m_sdkWrapper->CreateSpan(spanName, SpanKind::CLIENT, keyValueMap);
interactionSpan->AddAttribute("http.target", payload->target);
interactionSpan->AddAttribute("http.scheme", payload->scheme);
interactionSpan->AddAttribute("http.host", payload->host);
LOG4CXX_TRACE(mLogger, "Client Span started with SpanName: " << spanName
<< " Span Id: " << interactionSpan.get());
m_sdkWrapper->PopulatePropagationHeaders(propagationHeaders);
// Add the interaction to the request context.
requestContext->addInteraction(interactionSpan);
return APPD_SUCCESS;
}
APPD_SDK_API APPD_SDK_STATUS_CODE RequestProcessingEngine::endInteraction(
APPD_SDK_HANDLE_REQ reqHandle,
bool ignoreBackend,
EndInteractionPayload *payload) {
if (!reqHandle) {
LOG4CXX_ERROR(mLogger, __FUNCTION__ << " " << APPD_STATUS(handle_pointer_is_null));
return APPD_STATUS(handle_pointer_is_null);
}
if (!payload) {
LOG4CXX_ERROR(mLogger, __FUNCTION__ << " " << APPD_STATUS(payload_reflector_is_null));
return APPD_STATUS(payload_reflector_is_null);
}
// TODO : incorporate ignore backend logic here.
RequestContext* rContext = (RequestContext*)reqHandle;
if (!rContext->hasActiveInteraction()) {
// error : requestContext has no corresponding interaction.
LOG4CXX_TRACE(mLogger, __FUNCTION__ << " " << APPD_STATUS(invalid_context));
return APPD_STATUS(invalid_context);
}
auto interactionSpan = rContext->lastActiveInteraction();
// If errorCode is 0 or errMsg is empty, there is no error.
bool isError = payload->errorCode != 0 && !payload->errorMsg.empty();
if (isError) {
if (payload->errorCode >= HTTP_ERROR_1XX && payload->errorCode < HTTP_ERROR_4XX ) {
interactionSpan->SetStatus(StatusCode::Unset);
}
else if (payload->errorCode >= HTTP_ERROR_4XX && payload->errorCode < HTTP_ERROR_5XX ) {
if (interactionSpan->GetSpanKind() == SpanKind::SERVER)
interactionSpan->SetStatus(StatusCode::Unset);
else
interactionSpan->SetStatus(StatusCode::Error, payload->errorMsg);
} else {
interactionSpan->SetStatus(StatusCode::Error, payload->errorMsg);
}
interactionSpan->AddAttribute("error_code", payload->errorCode);
LOG4CXX_TRACE(mLogger, "Span updated with error Code: " << payload->errorCode);
} else {
interactionSpan->SetStatus(StatusCode::Ok);
}
if (!payload->backendName.empty()) {
// TODO : update span name when api is added.
interactionSpan->AddAttribute("backend_name", payload->backendName);
interactionSpan->AddAttribute("backend_type", payload->backendType);
LOG4CXX_TRACE(mLogger, "Span updated with BackendName: " << payload->backendName
<< " BackendType: " << payload->backendType);
}
LOG4CXX_TRACE(mLogger, "Ending Span with id: " << interactionSpan.get());
interactionSpan->End();
rContext->removeInteraction();
return APPD_SUCCESS;
}
} // core
} // appd