NCBI C++ ToolKit
request_ctx.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___REQUEST_CTX__HPP
2 #define CORELIB___REQUEST_CTX__HPP
3 
4 /* $Id: request_ctx.hpp 103157 2024-09-16 18:33:36Z grichenk $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Aleksey Grichenko, Denis Vakatov
30  *
31  * File Description:
32  * Request context for diagnostic framework.
33  *
34  */
35 
36 /// @file request_ctx.hpp
37 ///
38 /// Defines CRequestContext class for NCBI C++ diagnostic API.
39 ///
40 
41 
42 #include <corelib/ncbiobj.hpp>
43 #include <corelib/ncbidiag.hpp>
44 #include <corelib/ncbitime.hpp>
45 #include <corelib/ncbistr.hpp>
47 
48 
49 /** @addtogroup Diagnostics
50  *
51  * @{
52  */
53 
54 
56 
57 
58 // Forward declarations
60 
61 
62 /// Helper class to hold hit id and sub-hit counter which can be shared
63 /// between multiple request contexts.
65 {
66 public:
67  /// Set new hit id, checks its validity
68  explicit CSharedHitId(const string& hit_id)
69  : m_SubHitId(0), m_AppState(GetDiagContext().GetAppState())
70  {
71  x_SetHitId(hit_id);
72  }
73 
74  CSharedHitId(void) : m_SubHitId(0) {}
75  ~CSharedHitId(void) {}
76 
77  bool Empty(void) const { return m_HitId.empty(); }
78 
79  /// Mark this hit id as a shared one and start using shared counter.
80  void SetShared(void) const
81  {
82  if ( IsShared() ) return; // Already shared.
83  m_SharedSubHitId.Reset(new TSharedCounter());
84  m_SharedSubHitId->GetData().Set(m_SubHitId);
85  }
86 
87  /// Check if shared counter is used.
88  bool IsShared(void) const { return !m_SharedSubHitId.Empty(); }
89 
90  /// Get hit id value.
91  const string& GetHitId(void) const { return m_HitId; }
92 
93  /// Set new hit id value. This resets sub-hit counter and makes it non-shared.
94  void SetHitId(const string& hit_id)
95  {
96  m_SharedSubHitId.Reset();
97  m_SubHitId = 0;
98  x_SetHitId(hit_id);
99  m_AppState = GetDiagContext().GetAppState();
100  }
101 
102  typedef unsigned int TSubHitId;
103 
104  /// Get current sub-hit id value.
106  {
107  return IsShared() ? (TSubHitId)m_SharedSubHitId->GetData().Get() : m_SubHitId;
108  }
109 
110  /// Get next sub-hit id value.
112  {
113  return IsShared() ? (TSubHitId)m_SharedSubHitId->GetData().Add(1) : ++m_SubHitId;
114  }
115 
116  /// Check if this hit ID was set at request level.
117  bool IsRequestLevel(void) const
118  {
119  return m_AppState == eDiagAppState_RequestBegin ||
120  m_AppState == eDiagAppState_Request ||
121  m_AppState == eDiagAppState_RequestEnd;
122  }
123 
124 private:
125  /// Set new hit id, checks its validity
126  void x_SetHitId(const string& hit_id);
127 
128 private:
130 
131  string m_HitId;
135 };
136 
137 
138 class CMask;
139 class CMaskFileName;
140 
142 {
143 public:
144  virtual ~IRequestTracer(void) {}
147 };
148 
149 
151 {
152 public:
153  virtual ~ITracerSpan(void) {}
154 
155  enum ESpanKind {
160  };
161 
174  };
175 
178  eResponse
179  };
180 
181  enum ESpanStatus {
183  eError
184  };
185 
186  virtual void SetName(const string& name) = 0;
187  virtual void SetAttribute(ESpanAttribute attr, const string& value) = 0;
188  virtual void SetCustomAttribute(const string& attr, const string& value) = 0;
189  virtual void SetHttpHeader(EHttpHeaderType header_type, const string& name, const string& value) = 0;
190  virtual void SetSpanStatus(ESpanStatus status) = 0;
191 
192  virtual void PostEvent(const SDiagMessage& message) = 0;
193 
194  virtual void EndSpan(void) = 0;
195 };
196 
197 
199 {
200 public:
201  /// Request context flags.
203  fResetOnStart = 1, ///< Reset values when printing request-start.
204 
205  fDefault = 0
206  };
207  typedef int TContextFlags;
208 
209  CRequestContext(TContextFlags flags = fDefault);
210  virtual ~CRequestContext(void);
211 
213 
214  /// Get request ID (or zero if not set).
215  TCount GetRequestID(void) const;
216  /// Set request ID.
217  void SetRequestID(TCount rid);
218  /// Check if request ID was assigned a value
219  bool IsSetRequestID(void) const;
220  /// Reset request ID
221  void UnsetRequestID(void);
222  /// Assign the next available request ID to this request.
223  TCount SetRequestID(void);
224 
225  /// Return the next available application-wide request ID.
226  static TCount GetNextRequestID(void);
227 
228  /// Application state
229  EDiagAppState GetAppState(void) const;
230  void SetAppState(EDiagAppState state);
231 
232  /// Client IP/hostname
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);
238 
239  /// Session ID
240  string GetSessionID(void) const;
241  void SetSessionID(const string& session);
242  bool IsSetSessionID(void) const;
243  bool IsSetExplicitSessionID(void) const; ///< Does not check default SID
244  void UnsetSessionID(void);
245  /// Create and set new session ID
246  const string& SetSessionID(void);
247  /// Get URL-encoded session ID
248  string GetEncodedSessionID(void) const;
249 
250  /// Hit ID
251  /// Allowed source of the current hit id
252  /// @sa IsSetHitID
254  eHitID_Any = 0, ///< Any hit id - always return true.
255  eHitID_Request = 0x01, ///< Check if per-request hit id is set.
256  eHitID_Default = 0x02, ///< Check if default hit id is set.
257 
258  /// Check if any hit is already available (will not be generated
259  /// on request).
260  eHidID_Existing = eHitID_Default | eHitID_Request
261  };
262 
263  /// Get explicit hit id or the default one (from HTTP_NCBI_PHID etc).
264  /// If none of the above is available, generate a new id for the current
265  /// request.
266  /// If using the default hit id, it is cached in the request context and
267  /// becomes local one.
268  string GetHitID(void) const
269  { return x_GetHitID(CDiagContext::eHitID_Create); }
270  /// Set explicit hit id. The id is reset on request end.
271  void SetHitID(const string& hit);
272  /// Check if there's an explicit hit id or the default one.
273  /// @param src
274  /// Allowed source(s) of hit id.
275  /// @return
276  /// If 'src' is eHitID_Any, always return 'true' because GetHitID
277  /// always returns a non-empty value. For other options return true
278  /// if the selected hit id source is already not empty.
279  bool IsSetHitID(EHitIDSource src = eHitID_Any) const;
280  /// Check if there's an explicit hit id.
281  /// @deprecated Use IsSetHitID(eHitID_Request) instead.
283  bool IsSetExplicitHitID(void) const
284  { return IsSetHitID(eHitID_Request); }
285  /// Reset explicit hit id.
286  void UnsetHitID(void);
287  /// Generate unique hit id, assign it to this request, return
288  /// the hit id value.
289  const string& SetHitID(void);
290  /// Get current hit id appended with auto-incremented sub-hit id.
291  const string& GetNextSubHitID(CTempString prefix = CTempString());
292  /// Get the last generated sub-hit id.
293  const string& GetCurrentSubHitID(CTempString prefix = CTempString());
294 
295  /// Dtab
296  bool IsSetDtab(void) const;
297  const string& GetDtab(void) const;
298  void SetDtab(const string& dtab);
299  void UnsetDtab(void);
300 
301  /// Request exit status
302  int GetRequestStatus(void) const;
303  void SetRequestStatus(int status);
304  void SetRequestStatus(CRequestStatus::ECode code);
305  bool IsSetRequestStatus(void) const;
306  void UnsetRequestStatus(void);
307 
308  /// Request execution timer
309  const CStopWatch& GetRequestTimer(void) const { return m_ReqTimer; }
310  CStopWatch& GetRequestTimer(void) { return m_ReqTimer; }
311 
312  /// Bytes read
313  Int8 GetBytesRd(void) const;
314  void SetBytesRd(Int8 bytes);
315  bool IsSetBytesRd(void) const;
316  void UnsetBytesRd(void);
317 
318  /// Bytes written
319  Int8 GetBytesWr(void) const;
320  void SetBytesWr(Int8 bytes);
321  bool IsSetBytesWr(void) const;
322  void UnsetBytesWr(void);
323 
324  /// Reset all properties to the initial state
325  void Reset(void);
326 
327  /// User-defined request properties
329 
330  /// Add/change property
331  void SetProperty(const string& name, const string& value);
332  /// Get property value or empty string
333  const string& GetProperty(const string& name) const;
334  /// Check if the property has a value (even if it's an empty string).
335  bool IsSetProperty(const string& name) const;
336  /// Remove property from the map
337  void UnsetProperty(const string& name);
338 
339  /// Get all properties (read only)
340  const TProperties& GetProperties(void) const { return m_Properties; }
341  /// Get all properties (non-const)
342  TProperties& GetProperties(void) { return m_Properties; }
343 
344  /// Auto-increment request ID with every posted message
345  void SetAutoIncRequestIDOnPost(bool enable) { m_AutoIncOnPost = enable; }
346  /// Get auto-increment state
347  bool GetAutoIncRequestIDOnPost(void) const { return m_AutoIncOnPost; }
348  /// Set default auto-increment flag used for each default request context.
349  /// Contexts created by users do not check this flag.
350  /// The flag is not MT-protected.
351  static void SetDefaultAutoIncRequestIDOnPost(bool enable);
352  /// Get default auto-increment flag.
353  static bool GetDefaultAutoIncRequestIDOnPost(void);
354 
355  /// Session ID format
357  eSID_Ncbi, ///< Strict NCBI format: (UID:16)_(RqID:4+)SID
358  eSID_Standard, ///< Alpanum, underscore, -.:@, (default)
359  eSID_Other ///< Any other format
360  };
361  /// Session ID error actions
363  eOnBadSID_Allow, ///< Don't validate session id.
364  eOnBadSID_AllowAndReport, ///< Accept but show warning (default).
365  eOnBadSID_Ignore, ///< Ignore bad session id.
366  eOnBadSID_IgnoreAndReport, ///< Ignore and show warning.
367  eOnBadSID_Throw ///< Throw on bad session id.
368  };
369 
370  /// Check if session id fits the allowed format.
371  static bool IsValidSessionID(const string& session_id);
372 
373  /// Get/set session id error action.
374  static EOnBadSessionID GetBadSessionIDAction(void);
375  static void SetBadSessionIDAction(EOnBadSessionID action);
376 
377  /// Get/set allowed session id format.
378  static ESessionIDFormat GetAllowedSessionIDFormat(void);
379  static void SetAllowedSessionIDFormat(ESessionIDFormat fmt);
380 
381  /// Copy current request context to a new one. The method can be used
382  /// to process a single request in several threads which share the same
383  /// information (request id, session id etc.).
384  /// NOTE: The new context is not linked to the parent one. No further
385  /// changes in a context are copied to its clones. It's the developer's
386  /// responsibility to track multiple clones and make sure that they are
387  /// used properly (e.g. status is set by just one thread; only one
388  /// request-stop is printed and no clones continue to log the same request
389  /// after it's stopped).
390  CRef<CRequestContext> Clone(void) const;
391 
392  /// Select the last hit id from the list of ids separated with commas
393  /// and optional spaces.
394  static string SelectLastHitID(const string& hit_ids);
395  /// Select the last session id from the list of ids separated with
396  /// commas and optional spaces, ignore UNK_SESSION value.
397  static string SelectLastSessionID(const string& session_ids);
398 
399  /// Add pass-through value if it matches a pattern from NCBI_CONTEXT_FIELDS.
400  void AddPassThroughProperty(const string& name, const string& value);
401 
402  /// Switch request context to read-only mode. A read-only context
403  /// can be attached to multiple threads. The mode must be disabled
404  /// before making any modifications (e.g. printing request-stop).
405  /// To avoid overhead CRequestContext does not check if there are
406  /// any threads currently using the same context in a different mode.
407  /// Any attempts to modify request context while in read-only mode
408  /// are ignored and reported as an error (once per process).
409  /// Note that some const methods may still need to modify the context
410  /// (e.g. GetHitID() generates a new hit id if it's not yet assigned).
411  /// To prevent the above error from being logged, the method should be
412  /// called at least once before switching to read-only mode.
413  void SetReadOnly(bool read_only) { m_IsReadOnly = read_only; }
414  /// Get current read-only flag.
415  bool GetReadOnly(void) const { return m_IsReadOnly; }
416 
418 
419  /// Return version increased on every context change (hit/subhit id, client ip,
420  /// session id).
421  TVersion GetVersion(void) const { return m_Version; }
422 
423  /// Set request tracer to be called on context events (start/stop etc.).
424  /// @sa IRequestTracer
425  static void SetRequestTracer(const shared_ptr<IRequestTracer>& tracer);
426 
427  /// At least Opentememetry tracer allows to set span kind only at span creation time.
428  /// These methods allow to pass span kind to the tracer.
429  void SetTracerSpanKind(ITracerSpan::ESpanKind kind) { m_SpanKind = kind; }
430  ITracerSpan::ESpanKind GetTracerSpanKind(void) const { return m_SpanKind; }
431 
432  void SetTracerSpan(const shared_ptr<ITracerSpan>& span) { m_TracerSpan = span; }
433  shared_ptr<ITracerSpan> GetTracerSpan(void) const { return m_TracerSpan; }
434 
435 private:
436  // Prohibit copying
439 
440  // Start/Stop methods must be visible in CDiagContext
441  friend class CDiagContext;
442 
443  // Mark the current request as running.
444  // Reset request status, bytes rd/wr, restart timer.
445  void StartRequest(void);
446 
447  // Mark the current request as finished.
448  // Reset all values (call Reset()).
449  void StopRequest(void);
450 
451  // Check if the request is running.
452  bool IsRunning(void) const { return m_IsRunning; }
453 
454  enum EProperty {
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,
462  eProp_Dtab = 1 << 7
463  };
464  typedef int TPropSet;
465 
466  bool x_IsSetProp(EProperty prop) const;
467  void x_SetProp(EProperty prop);
468  void x_UnsetProp(EProperty prop);
469 
470  // Log current hit id if not yet logged and if the application state is
471  // 'in request', otherwise postpone logging until StartRequest is executed.
472  // If 'ignore_app_state' is set, log any available hit id anyway.
473  void x_LogHitID(bool ignore_app_state = false) const;
474 
475  void x_SetHitID(const CSharedHitId& hit_id);
476  string x_GetHitID(CDiagContext::EDefaultHitIDFlags flag) const;
477 
478  void x_UpdateSubHitID(bool increment, CTempString prefix);
479 
480  static bool& sx_GetDefaultAutoIncRequestIDOnPost(void);
481 
482  // Methods used by CRequestContext_PassThrough
483  bool x_IsSetPassThroughProp(CTempString name, bool update) const;
484  void x_SetPassThroughProp(CTempString name, CTempString value, bool update) const;
485  const string& x_GetPassThroughProp(CTempString name, bool update) const;
486  void x_ResetPassThroughProp(CTempString name, bool update) const;
487  // Copy std properties from CRequestContext to pass-through data.
488  void x_UpdateStdPassThroughProp(CTempString name) const;
489  // Copy std properties from pass-through data to CRequestContext.
490  void x_UpdateStdContextProp(CTempString name) const;
491 
492  static const CMask& sx_GetContextFieldsMask(void);
493  static string sx_NormalizeContextPropertyName(const string& name);
494 
495  // Load environment values matching NCBI_CONTEXT_FIELDS.
496  void x_LoadEnvContextProperties(void);
497 
498  friend class CDiagBuffer;
499  bool x_LogHitIDOnError(void) const;
500 
501  bool x_CanModify(void) const;
502 
503  // Bumps context version.
504  void x_Modify(void);
505 
507  fLoggedOnRequest = 1, // Logged on creation or request start
508  fLoggedOnError = 2 // Logged on ERR_POST when applog messages are disabled
509  };
510 
513  string m_ClientIP;
516  string m_Dtab;
517  mutable int m_HitIDLoggedFlag;
528 
529  // For saving/checking owner TID.
531  // TID of the thread currently using this context or -1.
535 
536  // Name/value map for properties to be passed between requests.
537  // @sa CRequestContext_PassThrough
539 
540  // Access to passable properties.
543 
544  static shared_ptr<IRequestTracer> sm_Tracer;
545 
546  // Make sure the same tracer is used through the whole request.
547  shared_ptr<IRequestTracer> m_Tracer;
549  shared_ptr<ITracerSpan> m_TracerSpan;
550 
551  // Patterns from NCBI_CONTEXT_FIELDS variable.
552  static unique_ptr<CMaskFileName> sm_ContextFields;
553 
554  // Context values loaded from the global environment.
555  static unique_ptr<TPassThroughProperties> sm_EnvContextProperties;
556 
558 };
559 
560 
561 /// Request context properties passed between tasks.
563 {
564 public:
565  /// Get CRequestContext_PassThrough for the current request context.
567 
568  /// Get CRequestContext_PassThrough for the specific request context.
570 
571  /// Supported serialization/deserialization formats.
572  enum EFormat {
573  eFormat_UrlEncoded ///< name=value pairs URL-encoded and separated with '&'
574  };
575 
576  /// Check if the property is set.
577  bool IsSet(CTempString name) const;
578 
579  /// Set or update property value.
580  void Set(CTempString name, CTempString value);
581 
582  /// Get current property value or empty string if it's not set;
583  const string& Get(CTempString name) const;
584 
585  /// Reset property.
586  void Reset(CTempString name);
587 
588  /// Serialize current values using the specified format.
589  string Serialize(EFormat format) const;
590 
591  /// Deserialize values using the specified format.
593 
594  /// Enumerate all properties. The callback must have the following signarure:
595  /// bool F(const string& name, const string& value);
596  /// The function should return true to continue enumaration, false to stop.
597  template<class TCallback>
598  void Enumerate(TCallback callback)
599  {
600  m_Context->x_UpdateStdPassThroughProp("");
601  ITERATE(TProperties, it, m_Context->m_PassThroughProperties) {
602  if ( !callback(it->first, it->second) ) break;
603  }
604  }
605 
606 private:
607  string x_SerializeUrlEncoded(void) const;
608  void x_DeserializeUrlEncoded(CTempString data);
609 
611 
613 };
614 
615 
616 /// Take guard of the current CRequestContext, handle app-state, start/stop
617 /// logging and request status in the dtor.
619 {
620 public:
621  enum EFlags {
622  fPrintRequestStart = 1 << 0 ///< Print request-start automatically in the
623  ///< constructor. By default request-start is
624  ///< not printed to allow the caller log request
625  ///< arguments.
626  };
627  typedef int TFlags;
628 
629  /// Initialize guard.
630  /// @param context
631  /// Request context to be used. If null, re-use current context.
632  /// @param flags
633  /// Optional flags, @sa FFlags.
635 
636  /// Destroy guard. If released do nothing. On exception set error status.
637  /// Print request-stop. Restore previous context.
639 
640  /// Set request context status.
641  void SetStatus(int status) { m_RequestContext->SetRequestStatus(status); }
642 
643  /// Set default error status, which will be used if an uncaught exception
644  /// is detected.
645  void SetDefaultErrorStatus(int status);
646 
647  /// Get the guarded request context.
648  CRequestContext& GetRequestContext() const { return *m_RequestContext; }
649 
650  /// Release the guarded context, restore the saved context if any, do not
651  /// perform any other actions (logging, setting status).
652  void Release(void);
653 
654 private:
655  TFlags m_Flags = 0;
656  int m_ErrorStatus = 500;
659  bool m_OriginatesFromThrow = false;
660 };
661 
662 
665 {
666 public:
667  /// Error types that CRequestContext can generate.
668  ///
669  /// These generic error conditions can occur for corelib applications.
670  enum EErrCode {
671  eBadSession, ///< Invalid session id
672  eBadHit ///< Invalid hit id
673  };
674 
675  /// Translate from the error code value to its string representation.
676  virtual const char* GetErrCodeString(void) const override;
677 
678  // Standard exception boilerplate code.
680 };
681 
682 
683 inline
685 {
687 }
688 
689 inline
691 {
692  if (!x_CanModify()) return;
694  m_RequestID = rid;
695  x_Modify();
696 }
697 
698 inline
700 {
701  if (!x_CanModify()) return m_RequestID;
703  return m_RequestID;
704 }
705 
706 inline
708 {
710 }
711 
712 inline
714 {
715  if (!x_CanModify()) return;
717  m_RequestID = 0;
718  x_Modify();
719 }
720 
721 
722 inline
724 {
725  return x_IsSetProp(eProp_ClientIP) ?
727 }
728 
729 inline
731 {
732  return IsSetExplicitClientIP() ||
733  !GetDiagContext().GetDefaultClientIP().empty();
734 }
735 
736 inline
738 {
739  return x_IsSetProp(eProp_ClientIP);
740 }
741 
742 inline
744 {
745  if (!x_CanModify()) return;
747  m_ClientIP.clear();
748  x_Modify();
749 }
750 
751 
752 inline
754 {
755  if ( x_IsSetProp(eProp_SessionID) ) {
757  }
758  string def_sid = GetDiagContext().GetDefaultSessionID();
759  if ( !def_sid.empty() ) return def_sid;
760  return const_cast<CRequestContext*>(this)->SetSessionID();
761 }
762 
763 inline
765 {
766  return x_IsSetProp(eProp_SessionID) ?
769 }
770 
771 inline
773 {
774  return IsSetExplicitSessionID() ||
775  !GetDiagContext().GetDefaultSessionID().empty();
776 }
777 
778 inline
780 {
782 }
783 
784 inline
786 {
787  if (!x_CanModify()) return;
790  x_Modify();
791 }
792 
793 
794 inline
796 {
797  if (src == eHitID_Any) {
798  // Local, default or auto-created hit id is always available.
799  return true;
800  }
801  if ((src & eHitID_Request) && x_IsSetProp(eProp_HitID)) {
802  return m_HitID.IsRequestLevel();
803  }
804  if ((src & eHitID_Default) && GetDiagContext().x_IsSetDefaultHitID()) {
805  return true;
806  }
807  return false;
808 }
809 
810 
811 inline
813 {
814  if (!x_CanModify()) return;
817  x_Modify();
818  m_HitIDLoggedFlag = 0;
819  m_SubHitIDCache.clear();
820 }
821 
822 
823 inline
825 {
826  if (!x_CanModify()) return m_SubHitIDCache;
827  x_UpdateSubHitID(true, prefix);
828  return m_SubHitIDCache;
829 }
830 
831 
832 inline
834 {
835  x_UpdateSubHitID(false, prefix);
836  return m_SubHitIDCache;
837 }
838 
839 
840 inline
842 {
843  return x_IsSetProp(eProp_Dtab);
844 }
845 
846 
847 inline
848 const string& CRequestContext::GetDtab(void) const
849 {
851 }
852 
853 
854 inline
855 void CRequestContext::SetDtab(const string& dtab)
856 {
857  if (!x_CanModify()) return;
859  m_Dtab = dtab;
860 }
861 
862 
863 inline
865 {
866  if (!x_CanModify()) return;
868 }
869 
870 
871 inline
873 {
875 }
876 
877 inline
879 {
880  if (!x_CanModify()) return;
882  m_ReqStatus = status;
883 }
884 
885 inline
887 {
888  if (!x_CanModify()) return;
889  SetRequestStatus((int)code);
890 }
891 
892 inline
894 {
896 }
897 
898 inline
900 {
901  if (!x_CanModify()) return;
903  m_ReqStatus = 0;
904 }
905 
906 
907 inline
909 {
910  return x_IsSetProp(eProp_BytesRd) ? m_BytesRd : 0;
911 }
912 
913 inline
915 {
916  if (!x_CanModify()) return;
918  m_BytesRd = bytes;
919 }
920 
921 inline
923 {
924  return x_IsSetProp(eProp_BytesRd);
925 }
926 
927 inline
929 {
930  if (!x_CanModify()) return;
932  m_BytesRd = 0;
933 }
934 
935 
936 inline
938 {
939  return x_IsSetProp(eProp_BytesWr) ? m_BytesWr : 0;
940 }
941 
942 inline
944 {
945  if (!x_CanModify()) return;
947  m_BytesWr = bytes;
948 }
949 
950 inline
952 {
953  return x_IsSetProp(eProp_BytesWr);
954 }
955 
956 inline
958 {
959  if (!x_CanModify()) return;
961  m_BytesWr = 0;
962 }
963 
964 
965 inline
967 {
968  return m_PropSet & prop ? true : false;
969 }
970 
971 
972 inline
974 {
975  m_PropSet |= prop;
976 }
977 
978 inline
980 {
981  m_PropSet &= ~prop;
982 }
983 
984 
985 inline
987 {
989  return false;
990  }
992  return true;
993 }
994 
995 
996 inline
998 {
999  if ( m_IsReadOnly ) {
1000  ERR_POST_ONCE("Attempt to modify a read-only request context.");
1001  return false;
1002  }
1003  return true;
1004 }
1005 
1006 
1007 inline
1009 {
1011 }
1012 
1013 
1014 inline
1016 {
1017  m_Context.Reset(&GetDiagContext().GetRequestContext());
1018 }
1019 
1020 
1021 inline
1023  : m_Context(&ctx)
1024 {
1025 }
1026 
1027 
1028 inline
1030 {
1031  return m_Context->x_IsSetPassThroughProp(name, true);
1032 }
1033 
1034 
1035 inline
1037 {
1038  m_Context->x_SetPassThroughProp(name, value, true);
1039 }
1040 
1041 
1042 inline
1044 {
1045  return m_Context->x_GetPassThroughProp(name, true);
1046 }
1047 
1048 
1049 inline
1051 {
1052  m_Context->x_ResetPassThroughProp(name, true);
1053 }
1054 
1055 
1057 
1058 
1059 #endif /* CORELIB___REQUEST_CTX__HPP */
CAtomicCounter –.
Definition: ncbicntr.hpp:71
Thread local context data stored in TLS.
Definition: ncbidiag.cpp:447
CEncodedString –.
Definition: ncbistr.hpp:4817
EFormat
The formats are checked in the same order as declared here.
CMaskFileName –.
Definition: ncbi_mask.hpp:107
CMask –.
Definition: ncbi_mask.hpp:59
CObjectFor –.
Definition: ncbiobj.hpp:2335
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
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...
Definition: request_ctx.hpp:65
CStopWatch –.
Definition: ncbitime.hpp:1937
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
The NCBI C++ standard methods for dealing with std::string.
static uch flags
CS_CONTEXT * ctx
Definition: t0006.c:12
#define true
Definition: bool.h:35
char data[12]
Definition: iconv.c:80
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
TNCBIAtomicValue TValue
Alias TValue for TNCBIAtomicValue.
Definition: ncbicntr.hpp:73
TValue Add(int delta) THROWS_NONE
Atomically add value (=delta), and return new counter value.
Definition: ncbicntr.hpp:278
EContextFlags
Request context flags.
bool IsSetRequestID(void) const
Check if request ID was assigned a value.
void x_Modify(void)
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.
EDiagAppState m_AppState
int GetRequestStatus(void) const
Request exit status.
#define ERR_POST_ONCE(message)
Error posting only once during program execution.
Definition: ncbidiag.hpp:602
Uint8 TCount
Generic type for counters (posts, requests etc.)
Definition: ncbidiag.hpp:1605
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.
Definition: logging.cpp:874
CDiagContext & GetDiagContext(void)
Get diag context instance.
Definition: logging.cpp:818
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
CSharedHitId(void)
Definition: request_ctx.hpp:74
void SetTracerSpan(const shared_ptr< ITracerSpan > &span)
map< string, string, PNocase > TPassThroughProperties
void x_SetProp(EProperty prop)
void Reset(CTempString name)
Reset property.
TVersion m_Version
void UnsetBytesWr(void)
string GetSessionID(void) const
Session ID.
CAtomicCounter::TValue TVersion
void UnsetClientIP(void)
bool IsSetRequestStatus(void) const
TSubHitId m_SubHitId
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.
Definition: request_ctx.hpp:68
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.
TPropSet m_PropSet
string GetClientIP(void) const
Client IP/hostname.
virtual ~IRequestTracer(void)
void x_LogHitID(void) const
Definition: ncbidiag.cpp:2968
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.
Definition: request_ctx.hpp:94
static shared_ptr< IRequestTracer > sm_Tracer
static string GetDefaultClientIP(void)
Get default client ip.
Definition: ncbidiag.cpp:2932
void Enumerate(TCallback callback)
Enumerate all properties.
void SetAutoIncRequestIDOnPost(bool enable)
Auto-increment request ID with every posted message.
void SetRequestStatus(int status)
EDiagAppState m_AppState
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.
CStopWatch m_ReqTimer
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.
void UnsetDtab(void)
bool IsSetDtab(void) const
Dtab.
ITracerSpan::ESpanKind m_SpanKind
void UnsetSessionID(void)
bool IsSetExplicitClientIP(void) const
string m_SubHitIDCache
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.
Definition: ncbidiag.hpp:789
bool IsSetClientIP(void) const
shared_ptr< ITracerSpan > m_TracerSpan
void UnsetBytesRd(void)
TProperties m_Properties
bool IsShared(void) const
Check if shared counter is used.
Definition: request_ctx.hpp:88
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.
Definition: ncbidiag.cpp:2802
bool Empty(void) const
Definition: request_ctx.hpp:77
void UnsetHitID(void)
Reset explicit hit id.
unsigned int TSubHitId
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)
~CSharedHitId(void)
Definition: request_ctx.hpp:75
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.
Definition: request_ctx.hpp:80
CSharedHitId m_HitID
void SetReadOnly(bool read_only)
Switch request context to read-only mode.
CRequestContext::TPassThroughProperties TProperties
TContextFlags m_Flags
shared_ptr< IRequestTracer > m_Tracer
const string & GetHitId(void) const
Get hit id value.
Definition: request_ctx.hpp:91
string GetDefaultSessionID(void) const
Get default session id.
Definition: logging.cpp:868
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.
Definition: ncbidiag.hpp:796
@ eDiagAppState_RequestBegin
RB.
Definition: ncbidiag.hpp:794
@ eDiagAppState_Request
R.
Definition: ncbidiag.hpp:795
@ 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...
Definition: ncbiexpt.hpp:1388
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define NCBI_DEPRECATED
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
const string & GetEncodedString(void) const
Get encoded string.
Definition: ncbistr.hpp:4832
const string & GetOriginalString(void) const
Get the original unencoded string.
Definition: ncbistr.hpp:4830
void SetString(const CTempString s, NStr::EUrlEncode flag=NStr::eUrlEnc_SkipMarkChars)
Set new original string.
Definition: ncbistr.cpp:7298
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
const TYPE & Get(const CNamedParameterList *param)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
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.
static Format format
Definition: njn_ioutil.cpp:53
Defines CRequestStatus class for NCBI C++ diagnostic API.
static CNamedPipeClient * client
SDiagMessage –.
Definition: ncbidiag.hpp:1599
Definition: inftrees.h:24
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
Definition: will_convert.c:21
Modified on Fri Sep 20 14:58:14 2024 by modify_doxy.py rev. 669887