resource_to_telemetry.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package resourcetotelemetry // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
  4. import (
  5. "context"
  6. "go.opentelemetry.io/collector/consumer"
  7. "go.opentelemetry.io/collector/exporter"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. "go.opentelemetry.io/collector/pdata/pmetric"
  10. )
  11. // Settings defines configuration for converting resource attributes to telemetry attributes.
  12. // When used, it must be embedded in the exporter configuration:
  13. //
  14. // type Config struct {
  15. // // ...
  16. // resourcetotelemetry.Settings `mapstructure:"resource_to_telemetry_conversion"`
  17. // }
  18. type Settings struct {
  19. // Enabled indicates whether to convert resource attributes to telemetry attributes. Default is `false`.
  20. Enabled bool `mapstructure:"enabled"`
  21. }
  22. type wrapperMetricsExporter struct {
  23. exporter.Metrics
  24. }
  25. func (wme *wrapperMetricsExporter) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
  26. return wme.Metrics.ConsumeMetrics(ctx, convertToMetricsAttributes(md))
  27. }
  28. func (wme *wrapperMetricsExporter) Capabilities() consumer.Capabilities {
  29. // Always return true since this wrapper modifies data inplace.
  30. return consumer.Capabilities{MutatesData: true}
  31. }
  32. // WrapMetricsExporter wraps a given exporter.Metrics and based on the given settings
  33. // converts incoming resource attributes to metrics attributes.
  34. func WrapMetricsExporter(set Settings, exporter exporter.Metrics) exporter.Metrics {
  35. if !set.Enabled {
  36. return exporter
  37. }
  38. return &wrapperMetricsExporter{Metrics: exporter}
  39. }
  40. func convertToMetricsAttributes(md pmetric.Metrics) pmetric.Metrics {
  41. rms := md.ResourceMetrics()
  42. for i := 0; i < rms.Len(); i++ {
  43. resource := rms.At(i).Resource()
  44. ilms := rms.At(i).ScopeMetrics()
  45. for j := 0; j < ilms.Len(); j++ {
  46. ilm := ilms.At(j)
  47. metricSlice := ilm.Metrics()
  48. for k := 0; k < metricSlice.Len(); k++ {
  49. addAttributesToMetric(metricSlice.At(k), resource.Attributes())
  50. }
  51. }
  52. }
  53. return md
  54. }
  55. // addAttributesToMetric adds additional labels to the given metric
  56. func addAttributesToMetric(metric pmetric.Metric, labelMap pcommon.Map) {
  57. //exhaustive:enforce
  58. switch metric.Type() {
  59. case pmetric.MetricTypeGauge:
  60. addAttributesToNumberDataPoints(metric.Gauge().DataPoints(), labelMap)
  61. case pmetric.MetricTypeSum:
  62. addAttributesToNumberDataPoints(metric.Sum().DataPoints(), labelMap)
  63. case pmetric.MetricTypeHistogram:
  64. addAttributesToHistogramDataPoints(metric.Histogram().DataPoints(), labelMap)
  65. case pmetric.MetricTypeSummary:
  66. addAttributesToSummaryDataPoints(metric.Summary().DataPoints(), labelMap)
  67. case pmetric.MetricTypeExponentialHistogram:
  68. addAttributesToExponentialHistogramDataPoints(metric.ExponentialHistogram().DataPoints(), labelMap)
  69. }
  70. }
  71. func addAttributesToNumberDataPoints(ps pmetric.NumberDataPointSlice, newAttributeMap pcommon.Map) {
  72. for i := 0; i < ps.Len(); i++ {
  73. joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
  74. }
  75. }
  76. func addAttributesToHistogramDataPoints(ps pmetric.HistogramDataPointSlice, newAttributeMap pcommon.Map) {
  77. for i := 0; i < ps.Len(); i++ {
  78. joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
  79. }
  80. }
  81. func addAttributesToSummaryDataPoints(ps pmetric.SummaryDataPointSlice, newAttributeMap pcommon.Map) {
  82. for i := 0; i < ps.Len(); i++ {
  83. joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
  84. }
  85. }
  86. func addAttributesToExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice, newAttributeMap pcommon.Map) {
  87. for i := 0; i < ps.Len(); i++ {
  88. joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
  89. }
  90. }
  91. func joinAttributeMaps(from, to pcommon.Map) {
  92. to.EnsureCapacity(from.Len() + to.Len())
  93. from.Range(func(k string, v pcommon.Value) bool {
  94. v.CopyTo(to.PutEmpty(k))
  95. return true
  96. })
  97. }