Skip to content

Commit

Permalink
fix linters
Browse files Browse the repository at this point in the history
  • Loading branch information
narcis.gemene committed Aug 14, 2024
1 parent 9bf4e14 commit 58a9764
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 110 deletions.
26 changes: 5 additions & 21 deletions receiver/huaweicloudcesreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package huaweicloudcesreceiver // import "github.com/open-telemetry/opentelemetr
import (
"errors"
"fmt"
"regexp"
"slices"
"strings"

Expand All @@ -16,21 +15,6 @@ import (
"go.uber.org/multierr"
)

const (
maxDimensions = 4
maxMetricRetention = 604800
)

var (
namespaceRegex = regexp.MustCompile(`[A-Za-z][a-zA-Z_0-9]{0,29}\.[a-zA-Z_0-9]{1,30}`)
namespaceForbiddenServices = map[string]any{
"SYS": struct{}{},
"AGT": struct{}{},
"SRE": struct{}{},
}
forbiddenNamespace = "SERVICE.BMS"
)

var (
// Predefined error responses for configuration validation failures
errMissingRegionName = errors.New(`"region_name" is not specified in config`)
Expand All @@ -57,9 +41,9 @@ type Config struct {
// Set of attributes used to configure huawei's CES SDK connection
HuaweiSessionConfig `mapstructure:",squash"`

// ProjectId is a string to reference project where metrics should be associated with.
// If ProjectId is not filled in, the SDK will automatically call the IAM service to query the project id corresponding to the region.
ProjectId string `mapstructure:"project_id"`
// ProjectID is a string to reference project where metrics should be associated with.
// If ProjectID is not filled in, the SDK will automatically call the IAM service to query the project id corresponding to the region.
ProjectID string `mapstructure:"project_id"`
// How retrieved data from Cloud Eye is aggregated.
// Possible values are 1, 300, 1200, 3600, 14400, and 86400.
// 1: Cloud Eye performs no aggregation and displays raw data.
Expand Down Expand Up @@ -94,12 +78,12 @@ var validFilters = []string{"max", "min", "average", "sum", "variance"}

// Validate config
func (config *Config) Validate() error {
var err error = nil
var err error
if config.RegionName == "" {
err = multierr.Append(err, errMissingRegionName)
}

if config.ProjectId == "" {
if config.ProjectID == "" {
err = multierr.Append(err, errMissingProjectID)
}
if index := slices.Index(validPeriods, config.Period); index == -1 {
Expand Down
14 changes: 7 additions & 7 deletions receiver/huaweicloudcesreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestConfig_Validate(t *testing.T) {
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
Expand All @@ -41,7 +41,7 @@ func TestConfig_Validate(t *testing.T) {
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 100,
Filter: "min",
},
Expand All @@ -53,7 +53,7 @@ func TestConfig_Validate(t *testing.T) {
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 300,
Filter: "invalid",
},
Expand All @@ -65,7 +65,7 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
Expand All @@ -111,7 +111,7 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
Expand All @@ -129,7 +129,7 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectId: "my_project",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
Expand Down
10 changes: 5 additions & 5 deletions receiver/huaweicloudcesreceiver/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions receiver/huaweicloudcesreceiver/internal/ces_to_otlp.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal

import (
Expand Down
114 changes: 59 additions & 55 deletions receiver/huaweicloudcesreceiver/internal/ces_to_otlp_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal

import (
Expand All @@ -17,11 +20,61 @@ func stringPtr(s string) *string {
func float64Ptr(f float64) *float64 {
return &f
}

func expectedMetrics() pmetric.Metrics {
metrics := pmetric.NewMetrics()
resourceMetric := metrics.ResourceMetrics().AppendEmpty()

resource := resourceMetric.Resource()
resource.Attributes().PutStr("cloud.provider", "huawei_cloud")
resource.Attributes().PutStr("project.id", "project_1")
resource.Attributes().PutStr("region", "eu-west-101")

scopedMetric := resourceMetric.ScopeMetrics().AppendEmpty()
scopedMetric.Scope().SetName("huawei_cloud_ces")
scopedMetric.Scope().SetVersion("v1")

metric := scopedMetric.Metrics().AppendEmpty()
metric.SetName("cpu_util")
metric.SetUnit("%")
metric.Metadata().PutStr("instance_id", "faea5b75-e390-4e2b-8733-9226a9026070")
metric.Metadata().PutStr("service.namespace", "SYS.ECS")

dataPoints := metric.SetEmptyGauge().DataPoints()
dp := dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625610000)))
dp.SetDoubleValue(0.5)

dp = dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625715000)))
dp.SetDoubleValue(0.7)

scopedMetric = resourceMetric.ScopeMetrics().AppendEmpty()
scopedMetric.Scope().SetName("huawei_cloud_ces")
scopedMetric.Scope().SetVersion("v1")

metric = scopedMetric.Metrics().AppendEmpty()
metric.SetName("network_vm_connections")
metric.SetUnit("count")
metric.Metadata().PutStr("instance_id", "06b4020f-461a-4a52-84da-53fa71c2f42b")
metric.Metadata().PutStr("service.namespace", "SYS.ECS")

dataPoints = metric.SetEmptyGauge().DataPoints()
dp = dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625612000)))
dp.SetDoubleValue(1)

dp = dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625717000)))
dp.SetDoubleValue(3)
return metrics
}

func TestConvertCESMetricsToOTLP(t *testing.T) {
tests := []struct {
name string
input []model.BatchMetricData
expected func() pmetric.Metrics
expected pmetric.Metrics
}{
{
name: "Valid Metric Conversion",
Expand Down Expand Up @@ -69,67 +122,18 @@ func TestConvertCESMetricsToOTLP(t *testing.T) {
Unit: stringPtr("count"),
},
},
expected: func() pmetric.Metrics {
metrics := pmetric.NewMetrics()
resourceMetric := metrics.ResourceMetrics().AppendEmpty()

resource := resourceMetric.Resource()
resource.Attributes().PutStr("cloud.provider", "huawei_cloud")
resource.Attributes().PutStr("project.id", "project_1")
resource.Attributes().PutStr("region", "eu-west-101")

scopedMetric := resourceMetric.ScopeMetrics().AppendEmpty()
scopedMetric.Scope().SetName("huawei_cloud_ces")
scopedMetric.Scope().SetVersion("v1")

metric := scopedMetric.Metrics().AppendEmpty()
metric.SetName("cpu_util")
metric.SetUnit("%")
metric.Metadata().PutStr("instance_id", "faea5b75-e390-4e2b-8733-9226a9026070")
metric.Metadata().PutStr("service.namespace", "SYS.ECS")

dataPoints := metric.SetEmptyGauge().DataPoints()
dp := dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625610000)))
dp.SetDoubleValue(0.5)

dp = dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625715000)))
dp.SetDoubleValue(0.7)

scopedMetric = resourceMetric.ScopeMetrics().AppendEmpty()
scopedMetric.Scope().SetName("huawei_cloud_ces")
scopedMetric.Scope().SetVersion("v1")

metric = scopedMetric.Metrics().AppendEmpty()
metric.SetName("network_vm_connections")
metric.SetUnit("count")
metric.Metadata().PutStr("instance_id", "06b4020f-461a-4a52-84da-53fa71c2f42b")
metric.Metadata().PutStr("service.namespace", "SYS.ECS")

dataPoints = metric.SetEmptyGauge().DataPoints()
dp = dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625612000)))
dp.SetDoubleValue(1)

dp = dataPoints.AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(1556625717000)))
dp.SetDoubleValue(3)
return metrics
},
expected: expectedMetrics(),
},
{
name: "Empty Metric Data",
input: []model.BatchMetricData{},
expected: func() pmetric.Metrics {
return pmetric.NewMetrics()
},
name: "Empty Metric Data",
input: []model.BatchMetricData{},
expected: pmetric.NewMetrics(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected(), ConvertCESMetricsToOTLP("project_1", "eu-west-101", "average", tt.input))
assert.Equal(t, tt.expected, ConvertCESMetricsToOTLP("project_1", "eu-west-101", "average", tt.input))
})
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 58a9764

Please sign in to comment.