config_helper.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
  4. import (
  5. "sort"
  6. "strings"
  7. )
  8. // configHelper contains many of the functions required to get various info from the SNMP config
  9. type configHelper struct {
  10. cfg *Config
  11. metricScalarOIDs []string
  12. metricColumnOIDs []string
  13. attributeColumnOIDs []string
  14. resourceAttributeScalarOIDs []string
  15. resourceAttributeColumnOIDs []string
  16. metricNamesByOID map[string]string
  17. metricAttributesByOID map[string][]Attribute
  18. resourceAttributesByOID map[string][]string
  19. }
  20. // newConfigHelper returns a new configHelper with various pieces of static info saved for easy access
  21. func newConfigHelper(cfg *Config) *configHelper {
  22. ch := configHelper{
  23. cfg: cfg,
  24. metricScalarOIDs: []string{},
  25. metricColumnOIDs: []string{},
  26. attributeColumnOIDs: []string{},
  27. resourceAttributeScalarOIDs: []string{},
  28. resourceAttributeColumnOIDs: []string{},
  29. metricNamesByOID: map[string]string{},
  30. metricAttributesByOID: map[string][]Attribute{},
  31. resourceAttributesByOID: map[string][]string{},
  32. }
  33. // Group all metric scalar OIDs and metric column OIDs
  34. // Also create a map of metric names with OID as key so the metric config will be easy to
  35. // matchup later with returned SNMP data
  36. for name, metricCfg := range cfg.Metrics {
  37. for i, oid := range metricCfg.ScalarOIDs {
  38. // Data is returned by the client with '.' prefix on the OIDs.
  39. // Making sure the prefix exists here in the configs so we can match it up with returned data later
  40. if !strings.HasPrefix(oid.OID, ".") {
  41. oid.OID = "." + oid.OID
  42. cfg.Metrics[name].ScalarOIDs[i].OID = oid.OID
  43. }
  44. ch.metricScalarOIDs = append(ch.metricScalarOIDs, oid.OID)
  45. ch.metricNamesByOID[oid.OID] = name
  46. ch.metricAttributesByOID[oid.OID] = oid.Attributes
  47. ch.resourceAttributesByOID[oid.OID] = oid.ResourceAttributes
  48. }
  49. for i, oid := range metricCfg.ColumnOIDs {
  50. // Data is returned by the client with '.' prefix on the OIDs.
  51. // Making sure the prefix exists here in the configs so we can match it up with returned data later
  52. if !strings.HasPrefix(oid.OID, ".") {
  53. oid.OID = "." + oid.OID
  54. cfg.Metrics[name].ColumnOIDs[i].OID = oid.OID
  55. }
  56. ch.metricColumnOIDs = append(ch.metricColumnOIDs, oid.OID)
  57. ch.metricNamesByOID[oid.OID] = name
  58. ch.metricAttributesByOID[oid.OID] = oid.Attributes
  59. ch.resourceAttributesByOID[oid.OID] = oid.ResourceAttributes
  60. }
  61. }
  62. // Find all attribute column OIDs
  63. for name, attributeCfg := range cfg.Attributes {
  64. if attributeCfg.OID == "" {
  65. continue
  66. }
  67. // Data is returned by the client with '.' prefix on the OIDs.
  68. // Making sure the prefix exists here in the configs so we can match it up with returned data later
  69. if !strings.HasPrefix(attributeCfg.OID, ".") {
  70. attributeCfg.OID = "." + attributeCfg.OID
  71. cfg.Attributes[name] = attributeCfg
  72. }
  73. ch.attributeColumnOIDs = append(ch.attributeColumnOIDs, attributeCfg.OID)
  74. }
  75. // Find all resource attribute scalar and column OIDs
  76. for name, resourceAttributeCfg := range cfg.ResourceAttributes {
  77. if resourceAttributeCfg.ScalarOID != "" {
  78. // Data is returned by the client with '.' prefix on the OIDs.
  79. // Making sure the prefix exists here in the configs so we can match it up with returned data later
  80. if !strings.HasPrefix(resourceAttributeCfg.ScalarOID, ".") {
  81. resourceAttributeCfg.ScalarOID = "." + resourceAttributeCfg.ScalarOID
  82. cfg.ResourceAttributes[name] = resourceAttributeCfg
  83. }
  84. ch.resourceAttributeScalarOIDs = append(ch.resourceAttributeScalarOIDs, resourceAttributeCfg.ScalarOID)
  85. continue
  86. }
  87. if resourceAttributeCfg.OID != "" {
  88. // Data is returned by the client with '.' prefix on the OIDs.
  89. // Making sure the prefix exists here in the configs so we can match it up with returned data later
  90. if !strings.HasPrefix(resourceAttributeCfg.OID, ".") {
  91. resourceAttributeCfg.OID = "." + resourceAttributeCfg.OID
  92. cfg.ResourceAttributes[name] = resourceAttributeCfg
  93. }
  94. ch.resourceAttributeColumnOIDs = append(ch.resourceAttributeColumnOIDs, resourceAttributeCfg.OID)
  95. }
  96. }
  97. // We expect these []string to be sorted later (i.e. mocks and resourceKey)
  98. sort.Strings(ch.metricScalarOIDs)
  99. sort.Strings(ch.metricColumnOIDs)
  100. sort.Strings(ch.attributeColumnOIDs)
  101. sort.Strings(ch.resourceAttributeScalarOIDs)
  102. sort.Strings(ch.resourceAttributeColumnOIDs)
  103. return &ch
  104. }
  105. // getMetricScalarOIDs returns all of the scalar OIDs in the metric configs
  106. func (h configHelper) getMetricScalarOIDs() []string {
  107. return h.metricScalarOIDs
  108. }
  109. // getMetricColumnOIDs returns all of the column OIDs in the metric configs
  110. func (h configHelper) getMetricColumnOIDs() []string {
  111. return h.metricColumnOIDs
  112. }
  113. // getAttributeColumnOIDs returns all of the attribute column OIDs in the attribute configs
  114. func (h configHelper) getAttributeColumnOIDs() []string {
  115. return h.attributeColumnOIDs
  116. }
  117. // getResourceAttributeScalarOIDs returns all of the resource attribute scalar OIDs in the resource attribute configs
  118. func (h configHelper) getResourceAttributeScalarOIDs() []string {
  119. return h.resourceAttributeScalarOIDs
  120. }
  121. // getResourceAttributeColumnOIDs returns all of the resource attribute column OIDs in the resource attribute configs
  122. func (h configHelper) getResourceAttributeColumnOIDs() []string {
  123. return h.resourceAttributeColumnOIDs
  124. }
  125. // getMetricName a metric names based on a given OID
  126. func (h configHelper) getMetricName(oid string) string {
  127. return h.metricNamesByOID[oid]
  128. }
  129. // getMetricConfig returns a metric config based on a given name
  130. func (h configHelper) getMetricConfig(name string) *MetricConfig {
  131. return h.cfg.Metrics[name]
  132. }
  133. // getAttributeConfigValue returns the value of an attribute config
  134. func (h configHelper) getAttributeConfigValue(name string) string {
  135. attrConfig := h.cfg.Attributes[name]
  136. if attrConfig == nil {
  137. return ""
  138. }
  139. return attrConfig.Value
  140. }
  141. // getAttributeConfigIndexedValuePrefix returns the indexed value prefix of an attribute config
  142. func (h configHelper) getAttributeConfigIndexedValuePrefix(name string) string {
  143. attrConfig := h.cfg.Attributes[name]
  144. if attrConfig == nil {
  145. return ""
  146. }
  147. return attrConfig.IndexedValuePrefix
  148. }
  149. // getAttributeConfigOID returns the column OID of an attribute config
  150. func (h configHelper) getAttributeConfigOID(name string) string {
  151. attrConfig := h.cfg.Attributes[name]
  152. if attrConfig == nil {
  153. return ""
  154. }
  155. return attrConfig.OID
  156. }
  157. // getResourceAttributeConfigIndexedValuePrefix returns the indexed value prefix of a resource attribute config
  158. func (h configHelper) getResourceAttributeConfigIndexedValuePrefix(name string) string {
  159. attrConfig := h.cfg.ResourceAttributes[name]
  160. if attrConfig == nil {
  161. return ""
  162. }
  163. return attrConfig.IndexedValuePrefix
  164. }
  165. // getResourceAttributeConfigOID returns the column OID of a resource attribute config
  166. func (h configHelper) getResourceAttributeConfigOID(name string) string {
  167. attrConfig := h.cfg.ResourceAttributes[name]
  168. if attrConfig == nil {
  169. return ""
  170. }
  171. return attrConfig.OID
  172. }
  173. // getResourceAttributeConfigScalarOID returns the scalar OID of a resource attribute config
  174. func (h configHelper) getResourceAttributeConfigScalarOID(name string) string {
  175. attrConfig := h.cfg.ResourceAttributes[name]
  176. if attrConfig == nil {
  177. return ""
  178. }
  179. return attrConfig.ScalarOID
  180. }
  181. // getMetricConfigAttributes returns the metric config attributes for a given OID
  182. func (h configHelper) getMetricConfigAttributes(oid string) []Attribute {
  183. return h.metricAttributesByOID[oid]
  184. }
  185. // getResourceAttributeNames returns the metric config resource attributes for a given OID
  186. func (h configHelper) getResourceAttributeNames(oid string) []string {
  187. return h.resourceAttributesByOID[oid]
  188. }