1 #ifndef CORELIB___REQUEST_CTX__HPP
2 #define CORELIB___REQUEST_CTX__HPP
77 bool Empty(
void)
const {
return m_HitId.empty(); }
82 if ( IsShared() )
return;
84 m_SharedSubHitId->GetData().Set(m_SubHitId);
88 bool IsShared(
void)
const {
return !m_SharedSubHitId.Empty(); }
91 const string&
GetHitId(
void)
const {
return m_HitId; }
96 m_SharedSubHitId.Reset();
107 return IsShared() ? (
TSubHitId)m_SharedSubHitId->GetData().Get() : m_SubHitId;
113 return IsShared() ? (
TSubHitId)m_SharedSubHitId->GetData().Add(1) : ++m_SubHitId;
126 void x_SetHitId(
const string& hit_id);
215 TCount GetRequestID(
void)
const;
217 void SetRequestID(
TCount rid);
219 bool IsSetRequestID(
void)
const;
221 void UnsetRequestID(
void);
223 TCount SetRequestID(
void);
226 static TCount GetNextRequestID(
void);
233 string GetClientIP(
void)
const;
234 void SetClientIP(
const string&
client);
235 bool IsSetClientIP(
void)
const;
236 bool IsSetExplicitClientIP(
void)
const;
237 void UnsetClientIP(
void);
240 string GetSessionID(
void)
const;
241 void SetSessionID(
const string& session);
242 bool IsSetSessionID(
void)
const;
243 bool IsSetExplicitSessionID(
void)
const;
244 void UnsetSessionID(
void);
246 const string& SetSessionID(
void);
248 string GetEncodedSessionID(
void)
const;
255 eHitID_Request = 0x01,
256 eHitID_Default = 0x02,
260 eHidID_Existing = eHitID_Default | eHitID_Request
271 void SetHitID(
const string& hit);
279 bool IsSetHitID(EHitIDSource src = eHitID_Any)
const;
284 {
return IsSetHitID(eHitID_Request); }
286 void UnsetHitID(
void);
289 const string& SetHitID(
void);
296 bool IsSetDtab(
void)
const;
297 const string& GetDtab(
void)
const;
298 void SetDtab(
const string& dtab);
299 void UnsetDtab(
void);
302 int GetRequestStatus(
void)
const;
303 void SetRequestStatus(
int status);
305 bool IsSetRequestStatus(
void)
const;
306 void UnsetRequestStatus(
void);
313 Int8 GetBytesRd(
void)
const;
314 void SetBytesRd(
Int8 bytes);
315 bool IsSetBytesRd(
void)
const;
316 void UnsetBytesRd(
void);
319 Int8 GetBytesWr(
void)
const;
320 void SetBytesWr(
Int8 bytes);
321 bool IsSetBytesWr(
void)
const;
322 void UnsetBytesWr(
void);
331 void SetProperty(
const string& name,
const string&
value);
333 const string& GetProperty(
const string& name)
const;
335 bool IsSetProperty(
const string& name)
const;
337 void UnsetProperty(
const string& name);
351 static void SetDefaultAutoIncRequestIDOnPost(
bool enable);
353 static bool GetDefaultAutoIncRequestIDOnPost(
void);
371 static bool IsValidSessionID(
const string& session_id);
374 static EOnBadSessionID GetBadSessionIDAction(
void);
375 static void SetBadSessionIDAction(EOnBadSessionID action);
378 static ESessionIDFormat GetAllowedSessionIDFormat(
void);
379 static void SetAllowedSessionIDFormat(ESessionIDFormat fmt);
394 static string SelectLastHitID(
const string& hit_ids);
397 static string SelectLastSessionID(
const string& session_ids);
400 void AddPassThroughProperty(
const string& name,
const string&
value);
425 static void SetRequestTracer(
const shared_ptr<IRequestTracer>& tracer);
432 void SetTracerSpan(
const shared_ptr<ITracerSpan>& span) { m_TracerSpan = span; }
445 void StartRequest(
void);
449 void StopRequest(
void);
455 eProp_RequestID = 1 << 0,
456 eProp_ClientIP = 1 << 1,
457 eProp_SessionID = 1 << 2,
458 eProp_HitID = 1 << 3,
459 eProp_ReqStatus = 1 << 4,
460 eProp_BytesRd = 1 << 5,
461 eProp_BytesWr = 1 << 6,
473 void x_LogHitID(
bool ignore_app_state =
false)
const;
478 void x_UpdateSubHitID(
bool increment,
CTempString prefix);
480 static bool& sx_GetDefaultAutoIncRequestIDOnPost(
void);
483 bool x_IsSetPassThroughProp(
CTempString name,
bool update)
const;
485 const string& x_GetPassThroughProp(
CTempString name,
bool update)
const;
486 void x_ResetPassThroughProp(
CTempString name,
bool update)
const;
488 void x_UpdateStdPassThroughProp(
CTempString name)
const;
490 void x_UpdateStdContextProp(
CTempString name)
const;
492 static const CMask& sx_GetContextFieldsMask(
void);
493 static string sx_NormalizeContextPropertyName(
const string& name);
496 void x_LoadEnvContextProperties(
void);
498 friend class CDiagBuffer;
499 bool x_LogHitIDOnError(
void)
const;
501 bool x_CanModify(
void)
const;
507 fLoggedOnRequest = 1,
597 template<
class TCallback>
600 m_Context->x_UpdateStdPassThroughProp(
"");
602 if ( !callback(it->first, it->second) )
break;
607 string x_SerializeUrlEncoded(
void)
const;
622 fPrintRequestStart = 1 << 0
641 void SetStatus(
int status) { m_RequestContext->SetRequestStatus(status); }
645 void SetDefaultErrorStatus(
int status);
656 int m_ErrorStatus = 500;
659 bool m_OriginatesFromThrow =
false;
676 virtual const char* GetErrCodeString(
void)
const override;
759 if ( !def_sid.empty() )
return def_sid;
1000 ERR_POST_ONCE(
"Attempt to modify a read-only request context.");
Thread local context data stored in TLS.
Take guard of the current CRequestContext, handle app-state, start/stop logging and request status in...
Request context properties passed between tasks.
Helper class to hold hit id and sub-hit counter which can be shared between multiple request contexts...
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
The NCBI C++ standard methods for dealing with std::string.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
TNCBIAtomicValue TValue
Alias TValue for TNCBIAtomicValue.
TValue Add(int delta) THROWS_NONE
Atomically add value (=delta), and return new counter value.
EContextFlags
Request context flags.
bool IsSetRequestID(void) const
Check if request ID was assigned a value.
CStopWatch & GetRequestTimer(void)
CRequestContext & GetRequestContext() const
Get the guarded request context.
TSubHitId GetNextSubHitId(void)
Get next sub-hit id value.
void UnsetRequestStatus(void)
ESessionIDFormat
Session ID format.
bool IsSetBytesRd(void) const
TCount SetRequestID(void)
Assign the next available request ID to this request.
void SetBytesWr(Int8 bytes)
CEncodedString m_SessionID
EErrCode
Error types that CRequestContext can generate.
int GetRequestStatus(void) const
Request exit status.
#define ERR_POST_ONCE(message)
Error posting only once during program execution.
Uint8 TCount
Generic type for counters (posts, requests etc.)
Int8 GetBytesWr(void) const
Bytes written.
CObjectFor< CAtomicCounter > TSharedCounter
virtual void SetCustomAttribute(const string &attr, const string &value)=0
string GetEncodedSessionID(void) const
Get url-encoded session id.
CDiagContext & GetDiagContext(void)
Get diag context instance.
void SetTracerSpanKind(ITracerSpan::ESpanKind kind)
At least Opentememetry tracer allows to set span kind only at span creation time.
TProperties & GetProperties(void)
Get all properties (non-const)
bool GetAutoIncRequestIDOnPost(void) const
Get auto-increment state.
TSubHitId GetCurrentSubHitId(void) const
Get current sub-hit id value.
CRef< CRequestContext > m_SavedContext
void SetTracerSpan(const shared_ptr< ITracerSpan > &span)
map< string, string, PNocase > TPassThroughProperties
void x_SetProp(EProperty prop)
void Reset(CTempString name)
Reset property.
string GetSessionID(void) const
Session ID.
CAtomicCounter::TValue TVersion
bool IsSetRequestStatus(void) const
bool x_IsSetProp(EProperty prop) const
const string & GetNextSubHitID(CTempString prefix=CTempString())
Get current hit id appended with auto-incremented sub-hit id.
bool IsSetBytesWr(void) const
void SetDtab(const string &dtab)
Int8 GetBytesRd(void) const
Bytes read.
static unique_ptr< TPassThroughProperties > sm_EnvContextProperties
static TCount GetNextRequestID(void)
Return the next available application-wide request ID.
CSharedHitId(const string &hit_id)
Set new hit id, checks its validity.
const string & Get(CTempString name) const
Get current property value or empty string if it's not set;.
map< string, string > TProperties
User-defined request properties.
shared_ptr< ITracerSpan > GetTracerSpan(void) const
virtual void PostEvent(const SDiagMessage &message)=0
CRef< TSharedCounter > m_SharedSubHitId
bool IsSetSessionID(void) const
void SetStatus(int status)
Set request context status.
TCount GetRequestID(void) const
Get request ID (or zero if not set).
CRequestContext_PassThrough(void)
Get CRequestContext_PassThrough for the current request context.
string GetClientIP(void) const
Client IP/hostname.
virtual ~IRequestTracer(void)
void x_LogHitID(void) const
void x_SetPassThroughProp(CTempString name, CTempString value, bool update) const
void SetHitId(const string &hit_id)
Set new hit id value. This resets sub-hit counter and makes it non-shared.
static shared_ptr< IRequestTracer > sm_Tracer
static string GetDefaultClientIP(void)
Get default client ip.
void Enumerate(TCallback callback)
Enumerate all properties.
void SetAutoIncRequestIDOnPost(bool enable)
Auto-increment request ID with every posted message.
void SetRequestStatus(int status)
bool IsSetHitID(EHitIDSource src=eHitID_Any) const
Check if there's an explicit hit id or the default one.
virtual void EndSpan(void)=0
bool IsSet(CTempString name) const
Check if the property is set.
void UnsetRequestID(void)
Reset request ID.
const string & GetDtab(void) const
virtual void SetName(const string &name)=0
static CAtomicCounter sm_VersionCounter
bool x_IsSetPassThroughProp(CTempString name, bool update) const
bool x_LogHitIDOnError(void) const
EHitIDSource
Hit ID Allowed source of the current hit id.
const CStopWatch & GetRequestTimer(void) const
Request execution timer.
EFormat
Supported serialization/deserialization formats.
CRef< CRequestContext > m_Context
static unique_ptr< CMaskFileName > sm_ContextFields
bool GetReadOnly(void) const
Get current read-only flag.
bool IsSetDtab(void) const
Dtab.
ITracerSpan::ESpanKind m_SpanKind
void UnsetSessionID(void)
bool IsSetExplicitClientIP(void) const
const string & x_GetPassThroughProp(CTempString name, bool update) const
void x_ResetPassThroughProp(CTempString name, bool update) const
bool IsRunning(void) const
void Set(CTempString name, CTempString value)
Set or update property value.
const string & SetSessionID(void)
Create and set new session ID.
CRequestContext & operator=(const CRequestContext &)
bool IsSetExplicitHitID(void) const
Check if there's an explicit hit id.
void SetBytesRd(Int8 bytes)
ITracerSpan::ESpanKind GetTracerSpanKind(void) const
EDiagAppState
Application execution states shown in the std prefix.
bool IsSetClientIP(void) const
shared_ptr< ITracerSpan > m_TracerSpan
bool IsShared(void) const
Check if shared counter is used.
SDiagMessage::TCount TCount
EOnBadSessionID
Session ID error actions.
virtual void SetSpanStatus(ESpanStatus status)=0
EDiagAppState GetAppState(void) const
Return application state for the current thread if it's set.
void UnsetHitID(void)
Reset explicit hit id.
string GetHitID(void) const
Get explicit hit id or the default one (from HTTP_NCBI_PHID etc).
virtual void OnRequestStart(CRequestContext &context)=0
virtual void SetHttpHeader(EHttpHeaderType header_type, const string &name, const string &value)=0
const TProperties & GetProperties(void) const
Get all properties (read only)
virtual void OnRequestStop(CRequestContext &context)=0
NCBI_EXCEPTION_DEFAULT(CRequestContextException, CException)
bool IsSetExplicitSessionID(void) const
Does not check default SID.
TPassThroughProperties m_PassThroughProperties
TVersion GetVersion(void) const
Return version increased on every context change (hit/subhit id, client ip, session id).
virtual ~ITracerSpan(void)
void SetShared(void) const
Mark this hit id as a shared one and start using shared counter.
void SetReadOnly(bool read_only)
Switch request context to read-only mode.
CRequestContext::TPassThroughProperties TProperties
shared_ptr< IRequestTracer > m_Tracer
const string & GetHitId(void) const
Get hit id value.
string GetDefaultSessionID(void) const
Get default session id.
virtual void SetAttribute(ESpanAttribute attr, const string &value)=0
CRequestContext(const CRequestContext &)
bool IsRequestLevel(void) const
Check if this hit ID was set at request level.
void x_UnsetProp(EProperty prop)
const string & GetCurrentSubHitID(CTempString prefix=CTempString())
Get the last generated sub-hit id.
void x_UpdateSubHitID(bool increment, CTempString prefix)
CRef< CRequestContext > m_RequestContext
string GetEncodedSessionID(void) const
Get URL-encoded session ID.
bool x_CanModify(void) const
@ eSID_Ncbi
Strict NCBI format: (UID:16)_(RqID:4+)SID.
@ eSID_Standard
Alpanum, underscore, -.:@, (default)
@ eBadSession
Invalid session id.
@ eHitID_Request
Check if per-request hit id is set.
@ eHitID_Default
Check if default hit id is set.
@ eHitID_Any
Any hit id - always return true.
@ eDiagAppState_RequestEnd
RE.
@ eDiagAppState_RequestBegin
RB.
@ eDiagAppState_Request
R.
@ eOnBadSID_Allow
Don't validate session id.
@ eOnBadSID_IgnoreAndReport
Ignore and show warning.
@ eOnBadSID_AllowAndReport
Accept but show warning (default).
@ eOnBadSID_Ignore
Ignore bad session id.
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
void Reset(void)
Reset reference object.
int64_t Int8
8-byte (64-bit) signed integer
uint64_t Uint8
8-byte (64-bit) unsigned integer
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
const string & GetEncodedString(void) const
Get encoded string.
const string & GetOriginalString(void) const
Get the original unencoded string.
void SetString(const CTempString s, NStr::EUrlEncode flag=NStr::eUrlEnc_SkipMarkChars)
Set new original string.
#define NCBI_XNCBI_EXPORT
const TYPE & Get(const CNamedParameterList *param)
const GenericPointer< typename T::ValueType > T2 value
Defines NCBI C++ diagnostic APIs, classes, and macros.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Defines: CTimeFormat - storage class for time format.
Defines CRequestStatus class for NCBI C++ diagnostic API.
static CNamedPipeClient * client
void Serialize(CNcbiOstream &, const CRawScoreVector< Key, Score > &)
Generics These throw an exception; we must implement serialization for each type.
void Deserialize(CNcbiIstream &istr, CRawScoreVector< Key, Score > &)
static CS_CONTEXT * context