scraper.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build windows
  4. // +build windows
  5. package activedirectorydsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/activedirectorydsreceiver"
  6. import (
  7. "context"
  8. "fmt"
  9. "time"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/pdata/pcommon"
  12. "go.opentelemetry.io/collector/pdata/pmetric"
  13. "go.opentelemetry.io/collector/receiver"
  14. "go.opentelemetry.io/collector/receiver/scrapererror"
  15. "go.uber.org/multierr"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/activedirectorydsreceiver/internal/metadata"
  17. )
  18. type activeDirectoryDSScraper struct {
  19. mb *metadata.MetricsBuilder
  20. w *watchers
  21. }
  22. func newActiveDirectoryDSScraper(mbc metadata.MetricsBuilderConfig, params receiver.CreateSettings) *activeDirectoryDSScraper {
  23. return &activeDirectoryDSScraper{
  24. mb: metadata.NewMetricsBuilder(mbc, params),
  25. }
  26. }
  27. func (a *activeDirectoryDSScraper) start(ctx context.Context, host component.Host) error {
  28. watchers, err := getWatchers(defaultWatcherCreater{})
  29. if err != nil {
  30. return fmt.Errorf("failed to create performance counter watchers: %w", err)
  31. }
  32. a.w = watchers
  33. a.mb.Reset()
  34. return nil
  35. }
  36. func (a *activeDirectoryDSScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
  37. var multiErr error
  38. now := pcommon.NewTimestampFromTime(time.Now())
  39. draInboundBytesCompressed, err := a.w.Scrape(draInboundBytesCompressed)
  40. multiErr = multierr.Append(multiErr, err)
  41. if err == nil {
  42. a.mb.RecordActiveDirectoryDsReplicationNetworkIoDataPoint(now, int64(draInboundBytesCompressed), metadata.AttributeDirectionReceived, metadata.AttributeNetworkDataTypeCompressed)
  43. }
  44. draInboundBytesNotCompressed, err := a.w.Scrape(draInboundBytesNotCompressed)
  45. multiErr = multierr.Append(multiErr, err)
  46. if err == nil {
  47. a.mb.RecordActiveDirectoryDsReplicationNetworkIoDataPoint(now, int64(draInboundBytesNotCompressed), metadata.AttributeDirectionReceived, metadata.AttributeNetworkDataTypeUncompressed)
  48. }
  49. draOutboundBytesCompressed, err := a.w.Scrape(draOutboundBytesCompressed)
  50. multiErr = multierr.Append(multiErr, err)
  51. if err == nil {
  52. a.mb.RecordActiveDirectoryDsReplicationNetworkIoDataPoint(now, int64(draOutboundBytesCompressed), metadata.AttributeDirectionSent, metadata.AttributeNetworkDataTypeCompressed)
  53. }
  54. draOutboundBytesNotCompressed, err := a.w.Scrape(draOutboundBytesNotCompressed)
  55. multiErr = multierr.Append(multiErr, err)
  56. if err == nil {
  57. a.mb.RecordActiveDirectoryDsReplicationNetworkIoDataPoint(now, int64(draOutboundBytesNotCompressed), metadata.AttributeDirectionSent, metadata.AttributeNetworkDataTypeUncompressed)
  58. }
  59. draInboundFullSyncObjectsRemaining, err := a.w.Scrape(draInboundFullSyncObjectsRemaining)
  60. multiErr = multierr.Append(multiErr, err)
  61. if err == nil {
  62. a.mb.RecordActiveDirectoryDsReplicationSyncObjectPendingDataPoint(now, int64(draInboundFullSyncObjectsRemaining))
  63. }
  64. draInboundObjects, err := a.w.Scrape(draInboundObjects)
  65. multiErr = multierr.Append(multiErr, err)
  66. if err == nil {
  67. a.mb.RecordActiveDirectoryDsReplicationObjectRateDataPoint(now, draInboundObjects, metadata.AttributeDirectionReceived)
  68. }
  69. draOutboundObjects, err := a.w.Scrape(draOutboundObjects)
  70. multiErr = multierr.Append(multiErr, err)
  71. if err == nil {
  72. a.mb.RecordActiveDirectoryDsReplicationObjectRateDataPoint(now, draOutboundObjects, metadata.AttributeDirectionSent)
  73. }
  74. draInboundProperties, err := a.w.Scrape(draInboundProperties)
  75. multiErr = multierr.Append(multiErr, err)
  76. if err == nil {
  77. a.mb.RecordActiveDirectoryDsReplicationPropertyRateDataPoint(now, draInboundProperties, metadata.AttributeDirectionReceived)
  78. }
  79. draOutboundProperties, err := a.w.Scrape(draOutboundProperties)
  80. multiErr = multierr.Append(multiErr, err)
  81. if err == nil {
  82. a.mb.RecordActiveDirectoryDsReplicationPropertyRateDataPoint(now, draOutboundProperties, metadata.AttributeDirectionSent)
  83. }
  84. draInboundValuesDNs, dnsErr := a.w.Scrape(draInboundValuesDNs)
  85. multiErr = multierr.Append(multiErr, dnsErr)
  86. if dnsErr == nil {
  87. a.mb.RecordActiveDirectoryDsReplicationValueRateDataPoint(now, draInboundValuesDNs, metadata.AttributeDirectionReceived, metadata.AttributeValueTypeDistingushedNames)
  88. }
  89. draInboundValuesTotal, totalErr := a.w.Scrape(draInboundValuesTotal)
  90. multiErr = multierr.Append(multiErr, totalErr)
  91. if dnsErr == nil && totalErr == nil {
  92. otherValuesInbound := draInboundValuesTotal - draInboundValuesDNs
  93. a.mb.RecordActiveDirectoryDsReplicationValueRateDataPoint(now, otherValuesInbound, metadata.AttributeDirectionReceived, metadata.AttributeValueTypeOther)
  94. }
  95. draOutboundValuesDNs, dnsErr := a.w.Scrape(draOutboundValuesDNs)
  96. multiErr = multierr.Append(multiErr, dnsErr)
  97. if dnsErr == nil {
  98. a.mb.RecordActiveDirectoryDsReplicationValueRateDataPoint(now, draOutboundValuesDNs, metadata.AttributeDirectionSent, metadata.AttributeValueTypeDistingushedNames)
  99. }
  100. draOutboundValuesTotal, totalErr := a.w.Scrape(draOutboundValuesTotal)
  101. multiErr = multierr.Append(multiErr, totalErr)
  102. if dnsErr == nil && totalErr == nil {
  103. otherValuesOutbound := draOutboundValuesTotal - draOutboundValuesDNs
  104. a.mb.RecordActiveDirectoryDsReplicationValueRateDataPoint(now, otherValuesOutbound, metadata.AttributeDirectionSent, metadata.AttributeValueTypeOther)
  105. }
  106. draPendingReplicationOperations, err := a.w.Scrape(draPendingReplicationOperations)
  107. multiErr = multierr.Append(multiErr, err)
  108. if err == nil {
  109. a.mb.RecordActiveDirectoryDsReplicationOperationPendingDataPoint(now, int64(draPendingReplicationOperations))
  110. }
  111. draSyncFailuresSchemaMistmatch, schemaMismatchErr := a.w.Scrape(draSyncFailuresSchemaMismatch)
  112. multiErr = multierr.Append(multiErr, schemaMismatchErr)
  113. if schemaMismatchErr == nil {
  114. a.mb.RecordActiveDirectoryDsReplicationSyncRequestCountDataPoint(now, int64(draSyncFailuresSchemaMistmatch), metadata.AttributeSyncResultSchemaMismatch)
  115. }
  116. draSyncRequestsSuccessful, requestsSuccessfulErr := a.w.Scrape(draSyncRequestsSuccessful)
  117. multiErr = multierr.Append(multiErr, requestsSuccessfulErr)
  118. if requestsSuccessfulErr == nil {
  119. a.mb.RecordActiveDirectoryDsReplicationSyncRequestCountDataPoint(now, int64(draSyncRequestsSuccessful), metadata.AttributeSyncResultSuccess)
  120. }
  121. draSyncRequestsTotal, totalErr := a.w.Scrape(draSyncRequestsMade)
  122. multiErr = multierr.Append(multiErr, totalErr)
  123. if totalErr == nil && requestsSuccessfulErr == nil && schemaMismatchErr == nil {
  124. otherReplicationSyncRequests := draSyncRequestsTotal - draSyncRequestsSuccessful - draSyncFailuresSchemaMistmatch
  125. a.mb.RecordActiveDirectoryDsReplicationSyncRequestCountDataPoint(now, int64(otherReplicationSyncRequests), metadata.AttributeSyncResultOther)
  126. }
  127. dsDirectoryReads, err := a.w.Scrape(dsDirectoryReads)
  128. multiErr = multierr.Append(multiErr, err)
  129. if err == nil {
  130. a.mb.RecordActiveDirectoryDsOperationRateDataPoint(now, dsDirectoryReads, metadata.AttributeOperationTypeRead)
  131. }
  132. dsDirectoryWrites, err := a.w.Scrape(dsDirectoryWrites)
  133. multiErr = multierr.Append(multiErr, err)
  134. if err == nil {
  135. a.mb.RecordActiveDirectoryDsOperationRateDataPoint(now, dsDirectoryWrites, metadata.AttributeOperationTypeWrite)
  136. }
  137. dsDirectorySearches, err := a.w.Scrape(dsDirectorySearches)
  138. multiErr = multierr.Append(multiErr, err)
  139. if err == nil {
  140. a.mb.RecordActiveDirectoryDsOperationRateDataPoint(now, dsDirectorySearches, metadata.AttributeOperationTypeSearch)
  141. }
  142. dsClientBinds, err := a.w.Scrape(dsClientBinds)
  143. multiErr = multierr.Append(multiErr, err)
  144. if err == nil {
  145. a.mb.RecordActiveDirectoryDsBindRateDataPoint(now, dsClientBinds, metadata.AttributeBindTypeClient)
  146. }
  147. dsServerBinds, err := a.w.Scrape(dsServerBinds)
  148. multiErr = multierr.Append(multiErr, err)
  149. if err == nil {
  150. a.mb.RecordActiveDirectoryDsBindRateDataPoint(now, dsServerBinds, metadata.AttributeBindTypeServer)
  151. }
  152. dsCacheHitRate, err := a.w.Scrape(dsNameCacheHitRate)
  153. multiErr = multierr.Append(multiErr, err)
  154. if err == nil {
  155. a.mb.RecordActiveDirectoryDsNameCacheHitRateDataPoint(now, dsCacheHitRate)
  156. }
  157. dsNotifyQueueSize, err := a.w.Scrape(dsNotifyQueueSize)
  158. multiErr = multierr.Append(multiErr, err)
  159. if err == nil {
  160. a.mb.RecordActiveDirectoryDsNotificationQueuedDataPoint(now, int64(dsNotifyQueueSize))
  161. }
  162. securityPropEvents, err := a.w.Scrape(dsSecurityDescriptorPropagationsEvents)
  163. multiErr = multierr.Append(multiErr, err)
  164. if err == nil {
  165. a.mb.RecordActiveDirectoryDsSecurityDescriptorPropagationsEventQueuedDataPoint(now, int64(securityPropEvents))
  166. }
  167. securityDescSubops, err := a.w.Scrape(dsSecurityDescripterSubOperations)
  168. multiErr = multierr.Append(multiErr, err)
  169. if err == nil {
  170. a.mb.RecordActiveDirectoryDsSuboperationRateDataPoint(now, securityDescSubops, metadata.AttributeSuboperationTypeSecurityDescriptorPropagationsEvent)
  171. }
  172. searchSubops, err := a.w.Scrape(dsSearchSubOperations)
  173. multiErr = multierr.Append(multiErr, err)
  174. if err == nil {
  175. a.mb.RecordActiveDirectoryDsSuboperationRateDataPoint(now, searchSubops, metadata.AttributeSuboperationTypeSearch)
  176. }
  177. threadsInUse, err := a.w.Scrape(dsThreadsInUse)
  178. multiErr = multierr.Append(multiErr, err)
  179. if err == nil {
  180. a.mb.RecordActiveDirectoryDsThreadCountDataPoint(now, int64(threadsInUse))
  181. }
  182. ldapClientSessions, err := a.w.Scrape(ldapClientSessions)
  183. multiErr = multierr.Append(multiErr, err)
  184. if err == nil {
  185. a.mb.RecordActiveDirectoryDsLdapClientSessionCountDataPoint(now, int64(ldapClientSessions))
  186. }
  187. ldapBindTime, err := a.w.Scrape(ldapBindTime)
  188. multiErr = multierr.Append(multiErr, err)
  189. if err == nil {
  190. a.mb.RecordActiveDirectoryDsLdapBindLastSuccessfulTimeDataPoint(now, int64(ldapBindTime))
  191. }
  192. ldapSuccessfulBinds, err := a.w.Scrape(ldapSuccessfulBinds)
  193. multiErr = multierr.Append(multiErr, err)
  194. if err == nil {
  195. a.mb.RecordActiveDirectoryDsLdapBindRateDataPoint(now, ldapSuccessfulBinds)
  196. }
  197. ldapSearches, err := a.w.Scrape(ldapSearches)
  198. multiErr = multierr.Append(multiErr, err)
  199. if err == nil {
  200. a.mb.RecordActiveDirectoryDsLdapSearchRateDataPoint(now, ldapSearches)
  201. }
  202. if multiErr != nil {
  203. return pmetric.Metrics(a.mb.Emit()), scrapererror.NewPartialScrapeError(multiErr, len(multierr.Errors(multiErr)))
  204. }
  205. return pmetric.Metrics(a.mb.Emit()), nil
  206. }
  207. func (a *activeDirectoryDSScraper) shutdown(_ context.Context) error {
  208. return a.w.Close()
  209. }