OperationPerformanceCountersBase.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Diagnostics
  5. {
  6. using System.Diagnostics;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Administration;
  10. using System.Diagnostics.PerformanceData;
  11. abstract class OperationPerformanceCountersBase : PerformanceCountersBase
  12. {
  13. protected string instanceName;
  14. protected string operationName;
  15. protected enum PerfCounters : int
  16. {
  17. Calls = 0,
  18. CallsPerSecond,
  19. CallsOutstanding,
  20. CallsFailed,
  21. CallsFailedPerSecond,
  22. CallsFaulted,
  23. CallsFaultedPerSecond,
  24. CallDuration,
  25. CallDurationBase,
  26. SecurityValidationAuthenticationFailures,
  27. SecurityValidationAuthenticationFailuresPerSecond,
  28. CallsNotAuthorized,
  29. CallsNotAuthorizedPerSecond,
  30. TxFlowed,
  31. TxFlowedPerSecond,
  32. TotalCounters = TxFlowedPerSecond + 1
  33. }
  34. protected static readonly string[] perfCounterNames =
  35. {
  36. PerformanceCounterStrings.SERVICEMODELOPERATION.Calls,
  37. PerformanceCounterStrings.SERVICEMODELOPERATION.CallsPerSecond,
  38. PerformanceCounterStrings.SERVICEMODELOPERATION.CallsOutstanding,
  39. PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFailed,
  40. PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFailedPerSecond,
  41. PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFaulted,
  42. PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFaultedPerSecond,
  43. PerformanceCounterStrings.SERVICEMODELOPERATION.CallDuration,
  44. PerformanceCounterStrings.SERVICEMODELOPERATION.CallDurationBase,
  45. PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityValidationAuthenticationFailures,
  46. PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityValidationAuthenticationFailuresPerSecond,
  47. PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityCallsNotAuthorized,
  48. PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityCallsNotAuthorizedPerSecond,
  49. PerformanceCounterStrings.SERVICEMODELOPERATION.TxFlowed,
  50. PerformanceCounterStrings.SERVICEMODELOPERATION.TxFlowedPerSecond,
  51. };
  52. const int maxCounterLength = 64;
  53. const int hashLength = 2;
  54. [Flags]
  55. enum truncOptions : uint
  56. {
  57. NoBits = 0,
  58. service7 = 0x01,
  59. contract7 = 0x02,
  60. operation15 = 0x04,
  61. uri32 = 0x08
  62. }
  63. internal OperationPerformanceCountersBase(string service, string contract, string operationName, string uri)
  64. {
  65. this.operationName = operationName;
  66. this.instanceName = CreateFriendlyInstanceName(service, contract, operationName, uri);
  67. }
  68. static internal string CreateFriendlyInstanceName(string service, string contract, string operation, string uri)
  69. {
  70. // instance name is: serviceName.interfaceName.operationName@uri
  71. int length = service.Length + contract.Length + operation.Length + uri.Length + 3;
  72. if (length > maxCounterLength)
  73. {
  74. int count = 0;
  75. truncOptions tasks = OperationPerformanceCounters.GetCompressionTasks(
  76. length, service.Length, contract.Length, operation.Length, uri.Length);
  77. //if necessary, compress service name to 5 chars with a 2 char hash code
  78. if ((tasks & truncOptions.service7) > 0)
  79. {
  80. count = 7;
  81. service = GetHashedString(service, count - hashLength, service.Length - count + hashLength, true);
  82. }
  83. //if necessary, compress contract name to 5 chars with a 2 char hash code
  84. if ((tasks & truncOptions.contract7) > 0)
  85. {
  86. count = 7;
  87. contract = GetHashedString(contract, count - hashLength, contract.Length - count + hashLength, true);
  88. }
  89. //if necessary, compress operation name to 13 chars with a 2 char hash code
  90. if ((tasks & truncOptions.operation15) > 0)
  91. {
  92. count = 15;
  93. operation = GetHashedString(operation, count - hashLength, operation.Length - count + hashLength, true);
  94. }
  95. //if necessary, compress uri to 30 chars with a 2 char hash code
  96. if ((tasks & truncOptions.uri32) > 0)
  97. {
  98. count = 32;
  99. uri = GetHashedString(uri, 0, uri.Length - count + hashLength, false);
  100. }
  101. }
  102. // replace '/' with '|' because perfmon fails when '/' is in perfcounter instance name
  103. return service + "." + contract + "." + operation + "@" + uri.Replace('/', '|');
  104. }
  105. static truncOptions GetCompressionTasks(int totalLen, int serviceLen, int contractLen, int operationLen, int uriLen)
  106. {
  107. truncOptions bitmask = 0;
  108. if (totalLen > maxCounterLength)
  109. {
  110. int workingLen = totalLen;
  111. //note: order of if statements important (see spec)!
  112. if (workingLen > maxCounterLength && serviceLen > 8)
  113. {
  114. bitmask |= truncOptions.service7; //compress service name to 8 chars
  115. workingLen -= serviceLen - 7;
  116. }
  117. if (workingLen > maxCounterLength && contractLen > 7)
  118. {
  119. bitmask |= truncOptions.contract7; //compress contract name to 8 chars
  120. workingLen -= contractLen - 7;
  121. }
  122. if (workingLen > maxCounterLength && operationLen > 15)
  123. {
  124. bitmask |= truncOptions.operation15; //compress operation name to 16 chars
  125. workingLen -= operationLen - 15;
  126. }
  127. if (workingLen > maxCounterLength && uriLen > 32)
  128. {
  129. bitmask |= truncOptions.uri32; //compress uri to 32 chars
  130. }
  131. }
  132. return bitmask;
  133. }
  134. internal override string InstanceName
  135. {
  136. get
  137. {
  138. return this.instanceName;
  139. }
  140. }
  141. internal string OperationName
  142. {
  143. get { return this.operationName; }
  144. }
  145. internal override string[] CounterNames
  146. {
  147. get
  148. {
  149. return perfCounterNames;
  150. }
  151. }
  152. internal override int PerfCounterStart
  153. {
  154. get { return (int)PerfCounters.Calls; }
  155. }
  156. internal override int PerfCounterEnd
  157. {
  158. get { return (int)PerfCounters.TotalCounters; }
  159. }
  160. internal abstract void MethodCalled();
  161. internal abstract void MethodReturnedSuccess();
  162. internal abstract void MethodReturnedError();
  163. internal abstract void MethodReturnedFault();
  164. internal abstract void SaveCallDuration(long time);
  165. internal abstract void AuthenticationFailed();
  166. internal abstract void AuthorizationFailed();
  167. internal abstract void TxFlowed();
  168. }
  169. }