@@ -11,137 +11,102 @@ import (
11
11
"time"
12
12
13
13
"go.opentelemetry.io/collector/component"
14
- "go.opentelemetry.io/collector/confmap"
15
14
"go.opentelemetry.io/collector/pdata/pcommon"
16
15
"go.opentelemetry.io/collector/pdata/pmetric"
17
16
"go.opentelemetry.io/collector/receiver"
18
17
)
19
18
20
19
const metricsPrefix = "azure_"
21
20
22
- // ResourceAttributeSettings provides common settings for a particular metric.
23
- type ResourceAttributeSettings struct {
24
- Enabled bool `mapstructure:"enabled"`
25
-
26
- enabledProvidedByUser bool
21
+ // MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations
22
+ // required to produce metric representation defined in metadata and user config.
23
+ type MetricsBuilder struct {
24
+ config MetricsBuilderConfig // config of the metrics builder.
25
+ startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
26
+ metricsCapacity int // maximum observed number of metrics per resource.
27
+ metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
28
+ buildInfo component.BuildInfo // contains version information
29
+ metrics map [string ]* metricAzureAbstract
27
30
}
28
31
29
- func (ras * ResourceAttributeSettings ) Unmarshal (parser * confmap.Conf ) error {
30
- if parser == nil {
31
- return nil
32
- }
33
- err := parser .Unmarshal (ras )
34
- if err != nil {
35
- return err
36
- }
37
- ras .enabledProvidedByUser = parser .IsSet ("enabled" )
38
- return nil
32
+ // MetricBuilderOption applies changes to default metrics builder.
33
+ type MetricBuilderOption interface {
34
+ apply (* MetricsBuilder )
39
35
}
40
36
41
- // ResourceAttributesSettings provides settings for azuremonitorreceiver metrics.
42
- type ResourceAttributesSettings struct {
43
- AzureMonitorSubscriptionID ResourceAttributeSettings `mapstructure:"azuremonitor.subscription_id"`
44
- AzureMonitorTenantID ResourceAttributeSettings `mapstructure:"azuremonitor.tenant_id"`
37
+ type metricBuilderOptionFunc func ( mb * MetricsBuilder )
38
+
39
+ func ( mbof metricBuilderOptionFunc ) apply ( mb * MetricsBuilder ) {
40
+ mbof ( mb )
45
41
}
46
42
47
- func DefaultResourceAttributesSettings () ResourceAttributesSettings {
48
- return ResourceAttributesSettings {
49
- AzureMonitorSubscriptionID : ResourceAttributeSettings {
50
- Enabled : true ,
51
- },
52
- AzureMonitorTenantID : ResourceAttributeSettings {
53
- Enabled : true ,
54
- },
55
- }
43
+ // WithStartTime sets startTime on the metrics builder.
44
+ func WithStartTime (startTime pcommon.Timestamp ) MetricBuilderOption {
45
+ return metricBuilderOptionFunc (func (mb * MetricsBuilder ) {
46
+ mb .startTime = startTime
47
+ })
56
48
}
57
49
58
- func NewMetricsBuilder (mbc MetricsBuilderConfig , settings receiver.Settings , options ... metricBuilderOption ) * MetricsBuilder {
50
+ func NewMetricsBuilder (mbc MetricsBuilderConfig , settings receiver.Settings , options ... MetricBuilderOption ) * MetricsBuilder {
59
51
mb := & MetricsBuilder {
60
- startTime : pcommon . NewTimestampFromTime ( time . Now ()) ,
61
- metricsBuffer : pmetric . NewMetrics ( ),
62
- buildInfo : settings . BuildInfo ,
63
- resourceAttributesSettings : mbc . ResourceAttributes ,
64
- metrics : map [string ]* metricAzureAbstract {},
52
+ config : mbc ,
53
+ startTime : pcommon . NewTimestampFromTime ( time . Now () ),
54
+ metricsBuffer : pmetric . NewMetrics () ,
55
+ buildInfo : settings . BuildInfo ,
56
+ metrics : map [string ]* metricAzureAbstract {},
65
57
}
66
58
for _ , op := range options {
67
- op (mb )
59
+ op . apply (mb )
68
60
}
69
61
return mb
70
62
}
71
63
72
- func DefaultMetricsBuilderConfig () MetricsBuilderConfig {
73
- return MetricsBuilderConfig {
74
- ResourceAttributes : DefaultResourceAttributesSettings (),
75
- }
76
- }
77
-
78
- // MetricsBuilderConfig is a structural subset of an otherwise 1-1 copy of metadata.yaml
79
- type MetricsBuilderConfig struct {
80
- ResourceAttributes ResourceAttributesSettings `mapstructure:"resource_attributes"`
64
+ // NewResourceBuilder returns a new resource builder that should be used to build a resource associated with for the emitted metrics.
65
+ func (mb * MetricsBuilder ) NewResourceBuilder () * ResourceBuilder {
66
+ return NewResourceBuilder (mb .config .ResourceAttributes )
81
67
}
82
68
83
- // MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations
84
- // required to produce metric representation defined in metadata and user settings.
85
- type MetricsBuilder struct {
86
- startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
87
- metricsCapacity int // maximum observed number of metrics per resource.
88
- resourceCapacity int // maximum observed number of resource attributes.
89
- metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
90
- buildInfo component.BuildInfo // contains version information
91
- resourceAttributesSettings ResourceAttributesSettings
92
- metrics map [string ]* metricAzureAbstract
93
- }
94
-
95
- // metricBuilderOption applies changes to default metrics builder.
96
- type metricBuilderOption func (* MetricsBuilder )
97
-
98
69
// updateCapacity updates max length of metrics and resource attributes that will be used for the slice capacity.
99
70
func (mb * MetricsBuilder ) updateCapacity (rm pmetric.ResourceMetrics ) {
100
71
if mb .metricsCapacity < rm .ScopeMetrics ().At (0 ).Metrics ().Len () {
101
72
mb .metricsCapacity = rm .ScopeMetrics ().At (0 ).Metrics ().Len ()
102
73
}
103
- if mb .resourceCapacity < rm .Resource ().Attributes ().Len () {
104
- mb .resourceCapacity = rm .Resource ().Attributes ().Len ()
105
- }
106
74
}
107
75
108
76
// ResourceMetricsOption applies changes to provided resource metrics.
109
- type ResourceMetricsOption func (ResourceAttributesSettings , pmetric.ResourceMetrics )
77
+ type ResourceMetricsOption interface {
78
+ apply (pmetric.ResourceMetrics )
79
+ }
110
80
111
- // WithAzureMonitorSubscriptionID sets provided value as "azuremonitor.subscription_id" attribute for current resource.
112
- func WithAzureMonitorSubscriptionID (val string ) ResourceMetricsOption {
113
- return func (ras ResourceAttributesSettings , rm pmetric.ResourceMetrics ) {
114
- if ras .AzureMonitorSubscriptionID .Enabled {
115
- rm .Resource ().Attributes ().PutStr ("azuremonitor.subscription_id" , val )
116
- }
117
- }
81
+ type resourceMetricsOptionFunc func (pmetric.ResourceMetrics )
82
+
83
+ func (rmof resourceMetricsOptionFunc ) apply (rm pmetric.ResourceMetrics ) {
84
+ rmof (rm )
118
85
}
119
86
120
- // WithAzuremonitorTenantID sets provided value as "azuremonitor.tenant_id" attribute for current resource.
121
- func WithAzureMonitorTenantID (val string ) ResourceMetricsOption {
122
- return func (ras ResourceAttributesSettings , rm pmetric.ResourceMetrics ) {
123
- if ras .AzureMonitorTenantID .Enabled {
124
- rm .Resource ().Attributes ().PutStr ("azuremonitor.tenant_id" , val )
125
- }
126
- }
87
+ // WithResource sets the provided resource on the emitted ResourceMetrics.
88
+ // It's recommended to use ResourceBuilder to create the resource.
89
+ func WithResource (res pcommon.Resource ) ResourceMetricsOption {
90
+ return resourceMetricsOptionFunc (func (rm pmetric.ResourceMetrics ) {
91
+ res .CopyTo (rm .Resource ())
92
+ })
127
93
}
128
94
129
95
// EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for
130
96
// recording another set of data points as part of another resource. This function can be helpful when one scraper
131
97
// needs to emit metrics from several resources. Otherwise calling this function is not required,
132
98
// just `Emit` function can be called instead.
133
99
// Resource attributes should be provided as ResourceMetricsOption arguments.
134
- func (mb * MetricsBuilder ) EmitForResource (rmo ... ResourceMetricsOption ) {
100
+ func (mb * MetricsBuilder ) EmitForResource (options ... ResourceMetricsOption ) {
135
101
rm := pmetric .NewResourceMetrics ()
136
- rm .Resource ().Attributes ().EnsureCapacity (mb .resourceCapacity )
137
102
ils := rm .ScopeMetrics ().AppendEmpty ()
138
103
ils .Scope ().SetName (ScopeName )
139
104
ils .Scope ().SetVersion (mb .buildInfo .Version )
140
105
ils .Metrics ().EnsureCapacity (mb .metricsCapacity )
141
106
mb .EmitAllMetrics (ils )
142
107
143
- for _ , op := range rmo {
144
- op ( mb . resourceAttributesSettings , rm )
108
+ for _ , op := range options {
109
+ op . apply ( rm )
145
110
}
146
111
if ils .Metrics ().Len () > 0 {
147
112
mb .updateCapacity (rm )
@@ -161,10 +126,10 @@ func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics {
161
126
162
127
// Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted,
163
128
// and metrics builder should update its startTime and reset it's internal state accordingly.
164
- func (mb * MetricsBuilder ) Reset (options ... metricBuilderOption ) {
129
+ func (mb * MetricsBuilder ) Reset (options ... MetricBuilderOption ) {
165
130
mb .startTime = pcommon .NewTimestampFromTime (time .Now ())
166
131
for _ , op := range options {
167
- op (mb )
132
+ op . apply (mb )
168
133
}
169
134
}
170
135
0 commit comments