loggingagent.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef __ESPSERVERLOGGINGAGENT__HPP__
  14. #define __ESPSERVERLOGGINGAGENT__HPP__
  15. #include "loggingcommon.hpp"
  16. #include "datafieldmap.hpp"
  17. #ifdef ESPSERVERLOGGINGAGENT_EXPORTS
  18. #define ESPSERVERLOGGINGAGENT_API DECL_EXPORT
  19. #else
  20. #define ESPSERVERLOGGINGAGENT_API DECL_IMPORT
  21. #endif
  22. enum ESPLogContentGroup
  23. {
  24. ESPLCGESPContext = 0,
  25. ESPLCGUserContext = 1,
  26. ESPLCGUserReq = 2,
  27. ESPLCGUserResp = 3,
  28. ESPLCGLogDatasets = 4,
  29. ESPLCGBackEndResp = 5,
  30. ESPLCGAll = 6
  31. };
  32. static const char * const espLogContentGroupNames[] = { "ESPContext", "UserContext", "UserRequest", "UserResponse",
  33. "LogDatasets", "BackEndResponse", "", NULL };
  34. class CTransIDBuilder : public CInterface, implements IInterface
  35. {
  36. StringAttr seed;
  37. bool localSeed;
  38. unsigned __int64 seq = 0;
  39. unsigned maxLength = 0;
  40. unsigned maxSeq = 0;
  41. unsigned seedExpiredSeconds = 0;
  42. time_t createTime;
  43. void add(StringAttrMapping* transIDFields, const char* key, StringBuffer& id)
  44. {
  45. StringAttr* value = transIDFields->getValue(key);
  46. if (value)
  47. id.append(value->get()).append('-');
  48. else
  49. {
  50. const char* ptr = key;
  51. if (strlen(key) > 11) //skip the "transaction" prefix of the key
  52. ptr += 11;
  53. id.append('?').append(ptr).append('-');
  54. }
  55. }
  56. public:
  57. IMPLEMENT_IINTERFACE;
  58. CTransIDBuilder(const char* _seed, bool _localSeed, unsigned _maxLength, unsigned _maxSeq, unsigned _seedExpiredSeconds)
  59. : seed(_seed), localSeed(_localSeed), maxLength(_maxLength), maxSeq(_maxSeq), seedExpiredSeconds(_seedExpiredSeconds)
  60. {
  61. CDateTime now;
  62. now.setNow();
  63. createTime = now.getSimple();
  64. };
  65. virtual ~CTransIDBuilder() {};
  66. bool checkMaxSequenceNumber() { return (maxSeq == 0) || (seq < maxSeq); };
  67. bool checkMaxLength(unsigned length) { return (maxLength == 0) || (length <= maxLength); };
  68. bool checkTimeout()
  69. {
  70. if (seedExpiredSeconds ==0)
  71. return true;
  72. CDateTime now;
  73. now.setNow();
  74. return now.getSimple() < createTime + seedExpiredSeconds;
  75. };
  76. bool isLocalSeed() { return localSeed; };
  77. void resetTransSeed(const char* newSeed)
  78. {
  79. if (isEmptyString(newSeed))
  80. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "TransactionSeed cannot be empty.");
  81. seed.set(newSeed);
  82. seq = 0;
  83. CDateTime now;
  84. now.setNow();
  85. createTime = now.getSimple();
  86. };
  87. virtual const char* getTransSeed() { return seed.get(); };
  88. virtual void getTransID(StringAttrMapping* transIDFields, StringBuffer& id)
  89. {
  90. id.clear();
  91. if (transIDFields)
  92. {
  93. add(transIDFields, sTransactionDateTime, id);
  94. add(transIDFields, sTransactionMethod, id);
  95. add(transIDFields, sTransactionIdentifier, id);
  96. }
  97. if (localSeed)
  98. id.append(seed.get()).append("-X").append(++seq);
  99. else
  100. id.append(seed.get()).append('-').append(++seq);
  101. };
  102. };
  103. class CESPLogContentGroupFilters : public CInterface, implements IInterface
  104. {
  105. ESPLogContentGroup group;
  106. StringArray filters;
  107. public:
  108. IMPLEMENT_IINTERFACE;
  109. CESPLogContentGroupFilters(ESPLogContentGroup _group) : group(_group) {};
  110. ESPLogContentGroup getGroup() { return group; };
  111. StringArray& getFilters() { return filters; };
  112. void clearFilters() { filters.clear(); };
  113. unsigned getFilterCount() { return filters.length(); };
  114. void addFilter(const char* filter)
  115. {
  116. if (filter && *filter)
  117. filters.append(filter);
  118. };
  119. };
  120. class CESPServerLoggingAgent : public CInterface, implements IEspLogAgent
  121. {
  122. StringBuffer serviceName, loggingAgentName, defaultGroup;
  123. StringBuffer serverUrl, serverUserID, serverPassword;
  124. unsigned maxServerWaitingSeconds; //time out value for HTTP connection to logging server
  125. unsigned maxGTSRetries;
  126. StringArray logContentFilters;
  127. CIArrayOf<CESPLogContentGroupFilters> groupFilters;
  128. bool logBackEndResp;
  129. MapStringToMyClass<CTransIDBuilder> transIDMap;
  130. MapStringToMyClass<CLogSource> logSources;
  131. void readAllLogFilters(IPropertyTree* cfg);
  132. bool readLogFilters(IPropertyTree* cfg, unsigned groupID);
  133. void filterLogContentTree(StringArray& filters, IPropertyTree* originalContentTree, IPropertyTree* newLogContentTree, bool& logContentEmpty);
  134. void filterAndAddLogContentBranch(StringArray& branchNamesInFilter, unsigned idx, StringArray& branchNamesInLogContent,
  135. IPropertyTree* in, IPropertyTree* updateLogRequestTree, bool& logContentEmpty);
  136. void addLogContentBranch(StringArray& branchNames, IPropertyTree* contentToLogBranch, IPropertyTree* updateLogRequestTree);
  137. bool sendHTTPRequest(StringBuffer& req, StringBuffer& resp, StringBuffer& status);
  138. int getTransactionSeed(const char* source, StringBuffer& transactionSeed, StringBuffer& statusMessage);
  139. bool getTransactionSeed(StringBuffer& soapreq, int& statusCode, StringBuffer& statusMessage, StringBuffer& seedID);
  140. void resetTransSeed(CTransIDBuilder *builder, const char* groupName);
  141. virtual void createLocalTransactionSeed(StringBuffer& transactionSeed);
  142. public:
  143. IMPLEMENT_IINTERFACE;
  144. CESPServerLoggingAgent() {};
  145. virtual ~CESPServerLoggingAgent() {};
  146. bool init(const char* name, const char* type, IPropertyTree* cfg, const char* process);
  147. virtual bool getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp);
  148. virtual void getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID);
  149. virtual bool updateLog(IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp);
  150. virtual void filterLogContent(IEspUpdateLogRequestWrap* req);
  151. };
  152. #endif //__ESPSERVERLOGGINGAGENT__HPP__