|
4 | 4 | package hpa // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/hpa"
|
5 | 5 |
|
6 | 6 | import (
|
7 |
| - agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1" |
8 |
| - metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" |
9 |
| - resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" |
10 |
| - conventions "go.opentelemetry.io/collector/semconv/v1.6.1" |
| 7 | + "time" |
| 8 | + |
| 9 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 10 | + "go.opentelemetry.io/collector/pdata/pmetric" |
| 11 | + "go.opentelemetry.io/collector/receiver" |
11 | 12 | autoscalingv2 "k8s.io/api/autoscaling/v2"
|
12 | 13 | autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
|
13 |
| - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
14 | 14 |
|
15 | 15 | "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
|
16 |
| - "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants" |
| 16 | + imetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/hpa/internal/metadata" |
17 | 17 | "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
|
18 |
| - "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/utils" |
19 | 18 | )
|
20 | 19 |
|
21 |
| -var hpaMaxReplicasMetric = &metricspb.MetricDescriptor{ |
22 |
| - Name: "k8s.hpa.max_replicas", |
23 |
| - Description: "Maximum number of replicas to which the autoscaler can scale up", |
24 |
| - Unit: "1", |
25 |
| - Type: metricspb.MetricDescriptor_GAUGE_INT64, |
26 |
| -} |
27 |
| - |
28 |
| -var hpaMinReplicasMetric = &metricspb.MetricDescriptor{ |
29 |
| - Name: "k8s.hpa.min_replicas", |
30 |
| - Description: "Minimum number of replicas to which the autoscaler can scale down", |
31 |
| - Unit: "1", |
32 |
| - Type: metricspb.MetricDescriptor_GAUGE_INT64, |
33 |
| -} |
34 |
| - |
35 |
| -var hpaCurrentReplicasMetric = &metricspb.MetricDescriptor{ |
36 |
| - Name: "k8s.hpa.current_replicas", |
37 |
| - Description: "Current number of pod replicas managed by this autoscaler", |
38 |
| - Unit: "1", |
39 |
| - Type: metricspb.MetricDescriptor_GAUGE_INT64, |
40 |
| -} |
41 |
| - |
42 |
| -var hpaDesiredReplicasMetric = &metricspb.MetricDescriptor{ |
43 |
| - Name: "k8s.hpa.desired_replicas", |
44 |
| - Description: "Desired number of pod replicas managed by this autoscaler", |
45 |
| - Unit: "1", |
46 |
| - Type: metricspb.MetricDescriptor_GAUGE_INT64, |
47 |
| -} |
48 |
| - |
49 |
| -func GetMetrics(hpa *autoscalingv2.HorizontalPodAutoscaler) []*agentmetricspb.ExportMetricsServiceRequest { |
50 |
| - metrics := []*metricspb.Metric{ |
51 |
| - { |
52 |
| - MetricDescriptor: hpaMaxReplicasMetric, |
53 |
| - Timeseries: []*metricspb.TimeSeries{ |
54 |
| - utils.GetInt64TimeSeries(int64(hpa.Spec.MaxReplicas)), |
55 |
| - }, |
56 |
| - }, |
57 |
| - { |
58 |
| - MetricDescriptor: hpaMinReplicasMetric, |
59 |
| - Timeseries: []*metricspb.TimeSeries{ |
60 |
| - utils.GetInt64TimeSeries(int64(*hpa.Spec.MinReplicas)), |
61 |
| - }, |
62 |
| - }, |
63 |
| - { |
64 |
| - MetricDescriptor: hpaCurrentReplicasMetric, |
65 |
| - Timeseries: []*metricspb.TimeSeries{ |
66 |
| - utils.GetInt64TimeSeries(int64(hpa.Status.CurrentReplicas)), |
67 |
| - }, |
68 |
| - }, |
69 |
| - { |
70 |
| - MetricDescriptor: hpaDesiredReplicasMetric, |
71 |
| - Timeseries: []*metricspb.TimeSeries{ |
72 |
| - utils.GetInt64TimeSeries(int64(hpa.Status.DesiredReplicas)), |
73 |
| - }, |
74 |
| - }, |
75 |
| - } |
76 |
| - |
77 |
| - return []*agentmetricspb.ExportMetricsServiceRequest{ |
78 |
| - { |
79 |
| - Resource: getResourceForHPA(&hpa.ObjectMeta), |
80 |
| - Metrics: metrics, |
81 |
| - }, |
82 |
| - } |
83 |
| -} |
84 |
| - |
85 |
| -func GetMetricsBeta(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) []*agentmetricspb.ExportMetricsServiceRequest { |
86 |
| - metrics := []*metricspb.Metric{ |
87 |
| - { |
88 |
| - MetricDescriptor: hpaMaxReplicasMetric, |
89 |
| - Timeseries: []*metricspb.TimeSeries{ |
90 |
| - utils.GetInt64TimeSeries(int64(hpa.Spec.MaxReplicas)), |
91 |
| - }, |
92 |
| - }, |
93 |
| - { |
94 |
| - MetricDescriptor: hpaMinReplicasMetric, |
95 |
| - Timeseries: []*metricspb.TimeSeries{ |
96 |
| - utils.GetInt64TimeSeries(int64(*hpa.Spec.MinReplicas)), |
97 |
| - }, |
98 |
| - }, |
99 |
| - { |
100 |
| - MetricDescriptor: hpaCurrentReplicasMetric, |
101 |
| - Timeseries: []*metricspb.TimeSeries{ |
102 |
| - utils.GetInt64TimeSeries(int64(hpa.Status.CurrentReplicas)), |
103 |
| - }, |
104 |
| - }, |
105 |
| - { |
106 |
| - MetricDescriptor: hpaDesiredReplicasMetric, |
107 |
| - Timeseries: []*metricspb.TimeSeries{ |
108 |
| - utils.GetInt64TimeSeries(int64(hpa.Status.DesiredReplicas)), |
109 |
| - }, |
110 |
| - }, |
111 |
| - } |
112 |
| - |
113 |
| - return []*agentmetricspb.ExportMetricsServiceRequest{ |
114 |
| - { |
115 |
| - Resource: getResourceForHPA(&hpa.ObjectMeta), |
116 |
| - Metrics: metrics, |
117 |
| - }, |
118 |
| - } |
119 |
| -} |
120 |
| - |
121 |
| -func getResourceForHPA(om *v1.ObjectMeta) *resourcepb.Resource { |
122 |
| - return &resourcepb.Resource{ |
123 |
| - Type: constants.K8sType, |
124 |
| - Labels: map[string]string{ |
125 |
| - constants.K8sKeyHPAUID: string(om.UID), |
126 |
| - constants.K8sKeyHPAName: om.Name, |
127 |
| - conventions.AttributeK8SNamespaceName: om.Namespace, |
128 |
| - }, |
129 |
| - } |
| 20 | +func GetMetricsBeta(set receiver.CreateSettings, hpa *autoscalingv2beta2.HorizontalPodAutoscaler) pmetric.Metrics { |
| 21 | + mb := imetadata.NewMetricsBuilder(imetadata.DefaultMetricsBuilderConfig(), set) |
| 22 | + ts := pcommon.NewTimestampFromTime(time.Now()) |
| 23 | + mb.RecordK8sHpaMaxReplicasDataPoint(ts, int64(hpa.Spec.MaxReplicas)) |
| 24 | + mb.RecordK8sHpaMinReplicasDataPoint(ts, int64(*hpa.Spec.MinReplicas)) |
| 25 | + mb.RecordK8sHpaCurrentReplicasDataPoint(ts, int64(hpa.Status.CurrentReplicas)) |
| 26 | + mb.RecordK8sHpaDesiredReplicasDataPoint(ts, int64(hpa.Status.DesiredReplicas)) |
| 27 | + return mb.Emit(imetadata.WithK8sHpaUID(string(hpa.UID)), imetadata.WithK8sHpaName(hpa.Name), imetadata.WithK8sNamespaceName(hpa.Namespace)) |
| 28 | +} |
| 29 | + |
| 30 | +func GetMetrics(set receiver.CreateSettings, hpa *autoscalingv2.HorizontalPodAutoscaler) pmetric.Metrics { |
| 31 | + mb := imetadata.NewMetricsBuilder(imetadata.DefaultMetricsBuilderConfig(), set) |
| 32 | + ts := pcommon.NewTimestampFromTime(time.Now()) |
| 33 | + mb.RecordK8sHpaMaxReplicasDataPoint(ts, int64(hpa.Spec.MaxReplicas)) |
| 34 | + mb.RecordK8sHpaMinReplicasDataPoint(ts, int64(*hpa.Spec.MinReplicas)) |
| 35 | + mb.RecordK8sHpaCurrentReplicasDataPoint(ts, int64(hpa.Status.CurrentReplicas)) |
| 36 | + mb.RecordK8sHpaDesiredReplicasDataPoint(ts, int64(hpa.Status.DesiredReplicas)) |
| 37 | + return mb.Emit(imetadata.WithK8sHpaUID(string(hpa.UID)), imetadata.WithK8sHpaName(hpa.Name), imetadata.WithK8sNamespaceName(hpa.Namespace)) |
130 | 38 | }
|
131 | 39 |
|
132 | 40 | func GetMetadata(hpa *autoscalingv2.HorizontalPodAutoscaler) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
|
|
0 commit comments