42 # include <grpc++/server_context.h>
43 # include <grpc/support/alloc.h>
44 # include <grpc/support/log.h>
50 #define NCBI_USE_ERRCODE_X Serial_GRPCIntegration
60 if (name ==
"ncbi_dtab") {
62 }
else if (name ==
"ncbi_phid") {
64 }
else if (name ==
"ncbi_sid") {
80 if (name ==
"l5d-dtab") {
82 }
else if (name ==
"ncbi-phid") {
84 }
else if (name ==
"ncbi-sid") {
121 switch (args->severity) {
122 case GPR_LOG_SEVERITY_DEBUG: sev =
eDiag_Trace;
break;
123 case GPR_LOG_SEVERITY_INFO: sev =
eDiag_Info;
break;
124 case GPR_LOG_SEVERITY_ERROR: sev =
eDiag_Error;
break;
143 unique_ptr<TGRPCBaseClientContext>
148 unique_ptr<TGRPCBaseClientContext>
result
149 (TGRPCBaseClientContext::FromServerContext(sctx, options));
155 return make_unique<CGRPCClientContext>();
164 cctx.set_initial_metadata_corked(
true);
173 cctx.AddMetadata(
"dtab", rctx.
GetDtab());
175 cctx.AddMetadata(
"client", dctx.
GetAppName());
178 bool need_filter = pass_through.
IsSet(
"PATH");
181 <<
"No NCBI_CONTEXT_FIELDS or [Context] Fields"
182 " setting; performing ad-hoc filtering.");
185 ([&](
const string& name,
const string&
value) {
186 if ( !need_filter || name ==
"http_proxy"
198 cctx.AddMetadata(
"ncbi_client_ip",
buf);
218 #define TSCP(x, y) { CRequestStatus::x, grpc::y }
220 TSCP(e400_BadRequest, INVALID_ARGUMENT),
221 TSCP(e401_Unauthorized, PERMISSION_DENIED),
222 TSCP(e402_PaymentRequired, PERMISSION_DENIED),
223 TSCP(e403_Forbidden, PERMISSION_DENIED),
224 TSCP(e404_NotFound, NOT_FOUND),
225 TSCP(e405_MethodNotAllowed, UNIMPLEMENTED),
226 TSCP(e406_NotAcceptable, UNIMPLEMENTED),
227 TSCP(e407_ProxyAuthRequired, PERMISSION_DENIED),
228 TSCP(e408_RequestTimeout, DEADLINE_EXCEEDED),
229 TSCP(e409_Conflict, ABORTED),
230 TSCP(e410_Gone, NOT_FOUND),
231 TSCP(e411_LengthRequired, UNIMPLEMENTED),
232 TSCP(e412_PreconditionFailed, FAILED_PRECONDITION),
233 TSCP(e413_RequestEntityTooLarge, RESOURCE_EXHAUSTED),
234 TSCP(e414_RequestURITooLong, RESOURCE_EXHAUSTED),
235 TSCP(e415_UnsupportedMediaType, UNIMPLEMENTED),
236 TSCP(e416_RangeNotSatisfiable, OUT_OF_RANGE),
237 TSCP(e417_ExpectationFailed, FAILED_PRECONDITION),
238 TSCP(e422_UnprocessableEntity, UNIMPLEMENTED),
239 TSCP(e499_BrokenConnection, CANCELLED),
240 TSCP(e500_InternalServerError, INTERNAL),
241 TSCP(e501_NotImplemented, UNIMPLEMENTED),
242 TSCP(e502_BadGateway, INTERNAL),
243 TSCP(e503_ServiceUnavailable, UNAVAILABLE),
244 TSCP(e504_GatewayTimeout, DEADLINE_EXCEEDED),
245 TSCP(e505_HTTPVerNotSupported, UNIMPLEMENTED)
256 if (it != sc_ErrorCodeMap.end()) {
321 string client_name, peer_ip, port;
328 string peer = sctx->peer().substr(pos + 1);
330 pos = peer.
rfind(
':');
331 if (pos ==
NPOS || peer[peer.size() - 1] ==
']') {
334 port = peer.
substr(pos + 1);
335 host.
assign(peer, 0, pos);
336 if (host[0] ==
'[' && host[pos - 1] ==
']') {
337 host = host.
substr(1, pos - 2);
340 if ( !host.
empty() ) {
345 for (
const auto& metadata : sctx->client_metadata()) {
346 CTempString nm(metadata.first .data(), metadata.first .size());
347 CTempString vl(metadata.second.data(), metadata.second.size());
355 grpc_fields[name2] =
value;
358 = sc_RCSetterMap.find(name.c_str());
359 if (it != sc_RCSetterMap.end()) {
364 rcsettings[it->second][name] =
value;
365 }
else if (metadata.first ==
"client") {
369 for (
const auto& it : rcsettings) {
370 const string*
value =
nullptr;
371 if (it.second.size() == 1) {
372 value = &it.second.begin()->second;
374 _ASSERT(it.second.size() == 2);
375 const string *old_name =
nullptr, *new_name =
nullptr;
376 for (
const auto& it2 : it.second) {
378 new_name = &it2.first;
381 old_name = &it2.first;
387 <<
"Ignoring deprecated metadata field "
388 << *old_name <<
" in favor of " << *new_name);
390 (*(it.first))(rctx, *
value);
395 if ( !client_name.empty() ) {
396 extra.
Print(
"client_name", client_name);
398 if ( !peer_ip.empty() ) {
399 extra.
Print(
"peer_ip", peer_ip);
401 if ( !port.empty() ) {
402 extra.
Print(
"client_port", port);
408 if (app ==
nullptr) {
414 list<string> env_names;
415 env->Enumerate(env_names);
416 for (
const auto & it : env_names) {
420 env_fields[name] =
env->Get(it);
440 auto peer = sctx->peer();
445 for (
const auto& it : sctx->client_metadata()) {
446 CTempString name(it.first.data(), it.first.size());
464 const char* variable,
465 const char* env_var_name,
469 if (
addr.empty() ) {
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
static grpc::Server::GlobalCallbacks *volatile sm_ServerCallbacks
static CNcbiApplication * Instance(void)
Singleton method.
Request context properties passed between tasks.
class CStaticArrayMap<> provides access to a static array in much the same way as CStaticArraySet<>,...
TBase::const_iterator const_iterator
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
const CNcbiEnvironment & GetEnvironment(void) const
Get the application's cached environment.
@ eNoOwnership
No ownership is assumed.
void PrintRequestStop(void)
Print request stop message (for request-driven applications)
CDiagContext_Extra & Print(const string &name, const string &value)
The method does not print the argument, but adds it to the string.
CDiagContext & GetDiagContext(void)
Get diag context instance.
void PrintRequestStart(const string &message)
Print request start message (for request-driven applications)
string GetSessionID(void) const
Session ID.
void SetRequestID(TCount rid)
Set request ID.
const string & GetNextSubHitID(CTempString prefix=CTempString())
Get current hit id appended with auto-incremented sub-hit id.
#define ERR_POST_X_ONCE(err_subcode, message)
Error posting only once during program execution with default error code and given error subcode.
void SetClientIP(const string &client)
bool IsSetSessionID(void) const
static CRequestContext & GetRequestContext(void)
Shortcut to CDiagContextThreadData::GetThreadData().GetRequestContext()
string GetClientIP(void) const
Client IP/hostname.
void Enumerate(TCallback callback)
Enumerate all properties.
bool IsSet(CTempString name) const
Check if the property is set.
const string & GetDtab(void) const
void Flush(void)
Print the message and reset object.
bool IsSetClientIP(void) const
void AddPassThroughProperty(const string &name, const string &value)
Add pass-through value if it matches a pattern from NCBI_CONTEXT_FIELDS.
void LogFields(const TEntries &entries) const
Log (as an extra) all names/values matching fields in NCBI_LOG_FIELDS.
const string & GetAppName(void) const
Get application name.
EDiagSev
Severity level for the posted diagnostics.
const char * g_DiagUnknownFunction(void)
@ eDiag_Trace
Trace message.
@ eDiag_Info
Informational message.
@ eDiag_Error
Error message.
void Warning(CExceptionArgs_Base &args)
void Info(CExceptionArgs_Base &args)
static void AddStandardNCBIMetadata(TParent &cctx)
grpc::ClientContext TGRPCBaseClientContext
grpc::StatusCode g_AsGRPCStatusCode(CRequestStatus::ECode status_code)
static bool x_IsRealRequest(const TGRPCServerContext *sctx)
grpc::PropagationOptions TGRPCPropagationOptions
static void EndRequest(grpc::ServerContext *sctx)
grpc::ServerContext TGRPCServerContext
static unique_ptr< TParent > FromServerContext(const TGRPCServerContext &sc, TGRPCPropagationOptions opts=TGRPCPropagationOptions())
EInvocationType
How was BeginRequest or EndRequest called?
static bool IsImplemented(void)
string g_NCBI_GRPC_GetAddress(const char *section, const char *variable, const char *env_var_name, CParamBase::EParamSource *value_source)
Get "hostport" for the likes of "grpc::CreateChannel(hostport, ...)" trying (in order of priority):
static void BeginRequest(grpc::ServerContext *sctx)
@ eImplicit
From {Pre,Post}SynchronousRequest.
string g_GetConfigString(const char *section, const char *variable, const char *env_var_name, const char *default_value, CParamBase::EParamSource *src=nullptr)
Helper functions for getting values from registry/environment.
EParamSource
Source of the value returned by CParam::GetDefault().
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
NCBI_NS_STD::string::size_type SIZE_TYPE
size_type rfind(const CTempString match, size_type pos=npos) const
Find the first instance of the entire matching string within the current string in a backward directi...
CTempString & assign(const char *src_str, size_type len)
Assign new values to the content of the a string.
bool empty(void) const
Return true if the represented string is empty (i.e., the length is zero)
static string CParse(const CTempString str, EQuoted quoted=eQuoted)
Discard C-style backslash escapes.
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
CTempString substr(size_type pos) const
Obtain a substring from this string, beginning at a given offset.
size_type find(const CTempString match, size_type pos=0) const
Find the first instance of the entire matching string within the current string, beginning at an opti...
static string CEncode(const CTempString str, EQuoted quoted=eQuoted)
Encode a string for C/C++.
static string & ReplaceInPlace(string &src, const string &search, const string &replace, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
static string & ToLower(string &str)
Convert string to lower case – string& version.
@ eNotQuoted
String is not quoted.
@ eNocase
Case insensitive compare.
static void s_SetHitID(CRequestContext &r, const string &s)
static void s_SetSessionID(CRequestContext &r, const string &s)
SStaticPair< CRequestStatus::ECode, grpc::StatusCode > TStatusCodePair
SStaticPair< const char *, FRCSetter > TRCSetterPair
static void s_SetClientIP(CRequestContext &r, const string &s)
void(* FRCSetter)(CRequestContext &, const string &)
static string s_EncodeMetadataName(const string &name)
static string s_DecodeMetadataValue(const CTempString &value)
static CGRPCInitializer s_GRPCInitializer
static const TRCSetterPair sc_RCSetters[]
static void s_SetDtab(CRequestContext &r, const string &s)
CStaticArrayMap< CRequestStatus::ECode, grpc::StatusCode > TStatusCodeMap
static string s_EncodeMetadataValue(const string &value)
CStaticArrayMap< const char *, FRCSetter, PCase_CStr > TRCSetterMap
DEFINE_STATIC_ARRAY_MAP(TStatusCodeMap, sc_ErrorCodeMap, sc_ErrorCodes)
static const TStatusCodePair sc_ErrorCodes[]
static void s_NCBI_GPR_Log_Function(gpr_log_func_args *args)
static string s_DecodeMetadataName(const CTempString &name)
Definition of all error codes used in serial libraries (xser.lib, xcser.lib).
const GenericPointer< typename T::ValueType > T2 value
char * NcbiGetHostIP(char *buf, size_t bufsize)
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
Glue for integrating gRPC into the C++ toolkit, to ensure logging and metadata propagation (sid,...
Template structure SStaticPair is simlified replacement of STL pair<> Main reason of introducing this...