metrics.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package couchdbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver"
  4. import (
  5. "fmt"
  6. "go.opentelemetry.io/collector/pdata/pcommon"
  7. "go.opentelemetry.io/collector/receiver/scrapererror"
  8. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver/internal/metadata"
  9. )
  10. func (c *couchdbScraper) recordCouchdbAverageRequestTimeDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  11. averageRequestTimeMetricKey := []string{"request_time", "value", "arithmetic_mean"}
  12. averageRequestTimeValue, err := getValueFromBody(averageRequestTimeMetricKey, stats)
  13. if err != nil {
  14. errs.AddPartial(1, err)
  15. return
  16. }
  17. parsedValue, err := c.parseFloat(averageRequestTimeValue)
  18. if err != nil {
  19. errs.AddPartial(1, err)
  20. return
  21. }
  22. c.mb.RecordCouchdbAverageRequestTimeDataPoint(now, parsedValue)
  23. }
  24. func (c *couchdbScraper) recordCouchdbHttpdBulkRequestsDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  25. httpdBulkRequestsMetricKey := []string{"httpd", "bulk_requests", "value"}
  26. httpdBulkRequestsMetricValue, err := getValueFromBody(httpdBulkRequestsMetricKey, stats)
  27. if err != nil {
  28. errs.AddPartial(1, err)
  29. return
  30. }
  31. parsedValue, err := c.parseInt(httpdBulkRequestsMetricValue)
  32. if err != nil {
  33. errs.AddPartial(1, err)
  34. return
  35. }
  36. c.mb.RecordCouchdbHttpdBulkRequestsDataPoint(now, parsedValue)
  37. }
  38. func (c *couchdbScraper) recordCouchdbHttpdRequestsDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  39. for methodVal, method := range metadata.MapAttributeHTTPMethod {
  40. httpdRequestMethodKey := []string{"httpd_request_methods", methodVal, "value"}
  41. httpdRequestMethodValue, err := getValueFromBody(httpdRequestMethodKey, stats)
  42. if err != nil {
  43. errs.AddPartial(1, err)
  44. continue
  45. }
  46. parsedValue, err := c.parseInt(httpdRequestMethodValue)
  47. if err != nil {
  48. errs.AddPartial(1, err)
  49. continue
  50. }
  51. c.mb.RecordCouchdbHttpdRequestsDataPoint(now, parsedValue, method)
  52. }
  53. }
  54. func (c *couchdbScraper) recordCouchdbHttpdResponsesDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  55. codes := []string{"200", "201", "202", "204", "206", "301", "302", "304", "400", "401", "403", "404", "405", "406", "409", "412", "413", "414", "415", "416", "417", "500", "501", "503"}
  56. for _, code := range codes {
  57. httpdResponsetCodeKey := []string{"httpd_status_codes", code, "value"}
  58. httpdResponsetCodeValue, err := getValueFromBody(httpdResponsetCodeKey, stats)
  59. if err != nil {
  60. errs.AddPartial(1, err)
  61. continue
  62. }
  63. parsedValue, err := c.parseInt(httpdResponsetCodeValue)
  64. if err != nil {
  65. errs.AddPartial(1, err)
  66. continue
  67. }
  68. c.mb.RecordCouchdbHttpdResponsesDataPoint(now, parsedValue, code)
  69. }
  70. }
  71. func (c *couchdbScraper) recordCouchdbHttpdViewsDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  72. for viewVal, view := range metadata.MapAttributeView {
  73. viewKey := []string{"httpd", viewVal, "value"}
  74. viewValue, err := getValueFromBody(viewKey, stats)
  75. if err != nil {
  76. errs.AddPartial(1, err)
  77. continue
  78. }
  79. parsedValue, err := c.parseInt(viewValue)
  80. if err != nil {
  81. errs.AddPartial(1, err)
  82. continue
  83. }
  84. c.mb.RecordCouchdbHttpdViewsDataPoint(now, parsedValue, view)
  85. }
  86. }
  87. func (c *couchdbScraper) recordCouchdbDatabaseOpenDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  88. openDatabaseKey := []string{"open_databases", "value"}
  89. openDatabaseMetricValue, err := getValueFromBody(openDatabaseKey, stats)
  90. if err != nil {
  91. errs.AddPartial(1, err)
  92. return
  93. }
  94. parsedValue, err := c.parseInt(openDatabaseMetricValue)
  95. if err != nil {
  96. errs.AddPartial(1, err)
  97. return
  98. }
  99. c.mb.RecordCouchdbDatabaseOpenDataPoint(now, parsedValue)
  100. }
  101. func (c *couchdbScraper) recordCouchdbFileDescriptorOpenDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  102. fileDescriptorKey := []string{"open_os_files", "value"}
  103. fileDescriptorMetricValue, err := getValueFromBody(fileDescriptorKey, stats)
  104. if err != nil {
  105. errs.AddPartial(1, err)
  106. return
  107. }
  108. parsedValue, err := c.parseInt(fileDescriptorMetricValue)
  109. if err != nil {
  110. errs.AddPartial(1, err)
  111. return
  112. }
  113. c.mb.RecordCouchdbFileDescriptorOpenDataPoint(now, parsedValue)
  114. }
  115. func (c *couchdbScraper) recordCouchdbDatabaseOperationsDataPoint(now pcommon.Timestamp, stats map[string]any, errs *scrapererror.ScrapeErrors) {
  116. operations := []metadata.AttributeOperation{metadata.AttributeOperationReads, metadata.AttributeOperationWrites}
  117. keyPaths := [][]string{{"database_reads", "value"}, {"database_writes", "value"}}
  118. for i := 0; i < len(operations); i++ {
  119. key := keyPaths[i]
  120. value, err := getValueFromBody(key, stats)
  121. if err != nil {
  122. errs.AddPartial(1, err)
  123. continue
  124. }
  125. parsedValue, err := c.parseInt(value)
  126. if err != nil {
  127. errs.AddPartial(1, err)
  128. continue
  129. }
  130. c.mb.RecordCouchdbDatabaseOperationsDataPoint(now, parsedValue, operations[i])
  131. }
  132. }
  133. func getValueFromBody(keys []string, body map[string]any) (any, error) {
  134. var currentValue any = body
  135. for _, key := range keys {
  136. currentBody, ok := currentValue.(map[string]any)
  137. if !ok {
  138. return nil, fmt.Errorf("could not find key in body")
  139. }
  140. currentValue, ok = currentBody[key]
  141. if !ok {
  142. return nil, fmt.Errorf("could not find key in body")
  143. }
  144. }
  145. return currentValue, nil
  146. }
  147. func (c *couchdbScraper) parseInt(value any) (int64, error) {
  148. switch i := value.(type) {
  149. case int64:
  150. return i, nil
  151. case float64:
  152. return int64(i), nil
  153. }
  154. return 0, fmt.Errorf("could not parse value as int")
  155. }
  156. func (c *couchdbScraper) parseFloat(value any) (float64, error) {
  157. if f, ok := value.(float64); ok {
  158. return f, nil
  159. }
  160. return 0, fmt.Errorf("could not parse value as float")
  161. }