-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from qonto/fix-invalid-prometheus-label
Replace unsupported characters by underscore in labels
- Loading branch information
Showing
14 changed files
with
534 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Package mocks contains mock for Cloudwatch client | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
aws_cloudwatch "github.com/aws/aws-sdk-go-v2/service/cloudwatch" | ||
aws_cloudwatch_types "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" | ||
) | ||
|
||
type CloudwatchClient struct { | ||
Metrics []aws_cloudwatch_types.MetricDataResult | ||
} | ||
|
||
// GetMetricData returns custom metrics | ||
func (m CloudwatchClient) GetMetricData(ctx context.Context, input *aws_cloudwatch.GetMetricDataInput, fn ...func(*aws_cloudwatch.Options)) (*aws_cloudwatch.GetMetricDataOutput, error) { | ||
response := &aws_cloudwatch.GetMetricDataOutput{} | ||
response.MetricDataResults = m.Metrics | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,30 @@ | ||
package ec2_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
aws_ec2 "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
aws_ec2_types "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/qonto/prometheus-rds-exporter/internal/app/ec2" | ||
mock "github.com/qonto/prometheus-rds-exporter/internal/app/ec2/mock" | ||
converter "github.com/qonto/prometheus-rds-exporter/internal/app/unit" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var t3Large = ec2.EC2InstanceMetrics{ | ||
MaximumIops: 15700, | ||
MaximumThroughput: 347.5, | ||
Memory: 8, | ||
Vcpu: 2, | ||
} | ||
|
||
var t3Small = ec2.EC2InstanceMetrics{ | ||
MaximumIops: 11800, | ||
MaximumThroughput: 260.62, | ||
Memory: 2, | ||
Vcpu: 2, | ||
} | ||
|
||
type mockEC2Client struct{} | ||
|
||
func (m mockEC2Client) DescribeInstanceTypes(ctx context.Context, input *aws_ec2.DescribeInstanceTypesInput, optFns ...func(*aws_ec2.Options)) (*aws_ec2.DescribeInstanceTypesOutput, error) { | ||
var instances []aws_ec2_types.InstanceTypeInfo | ||
|
||
for _, instanceType := range input.InstanceTypes { | ||
//nolint // Hide "missing cases in switch" alert because instanceType has many values. Mock with return empty result for unknown instances | ||
switch instanceType { | ||
case "t3.large": | ||
instances = append(instances, aws_ec2_types.InstanceTypeInfo{ | ||
InstanceType: instanceType, | ||
VCpuInfo: &aws_ec2_types.VCpuInfo{DefaultVCpus: &t3Large.Vcpu}, | ||
MemoryInfo: &aws_ec2_types.MemoryInfo{SizeInMiB: &t3Large.Memory}, | ||
EbsInfo: &aws_ec2_types.EbsInfo{EbsOptimizedInfo: &aws_ec2_types.EbsOptimizedInfo{ | ||
MaximumIops: &t3Large.MaximumIops, | ||
MaximumThroughputInMBps: &t3Large.MaximumThroughput, | ||
}}, | ||
}) | ||
case "t3.small": | ||
instances = append(instances, aws_ec2_types.InstanceTypeInfo{ | ||
InstanceType: instanceType, | ||
VCpuInfo: &aws_ec2_types.VCpuInfo{DefaultVCpus: &t3Small.Vcpu}, | ||
MemoryInfo: &aws_ec2_types.MemoryInfo{SizeInMiB: &t3Small.Memory}, | ||
EbsInfo: &aws_ec2_types.EbsInfo{EbsOptimizedInfo: &aws_ec2_types.EbsOptimizedInfo{ | ||
MaximumIops: &t3Small.MaximumIops, | ||
MaximumThroughputInMBps: &t3Small.MaximumThroughput, | ||
}}, | ||
}) | ||
} | ||
} | ||
|
||
return &aws_ec2.DescribeInstanceTypesOutput{InstanceTypes: instances}, nil | ||
} | ||
|
||
func TestGetDBInstanceTypeInformation(t *testing.T) { | ||
mock := mockEC2Client{} | ||
client := mock.EC2Client{} | ||
|
||
instanceTypes := []string{"db.t3.large", "db.t3.small"} | ||
client := ec2.NewFetcher(mock) | ||
result, err := client.GetDBInstanceTypeInformation(instanceTypes) | ||
fetcher := ec2.NewFetcher(client) | ||
result, err := fetcher.GetDBInstanceTypeInformation(instanceTypes) | ||
|
||
require.NoError(t, err, "GetDBInstanceTypeInformation must succeed") | ||
assert.Equal(t, t3Large.Vcpu, result.Instances["db.t3.large"].Vcpu, "vCPU don't match") | ||
assert.Equal(t, converter.MegaBytesToBytes(t3Large.Memory), result.Instances["db.t3.large"].Memory, "Memory don't match") | ||
assert.Equal(t, t3Large.MaximumIops, result.Instances["db.t3.large"].MaximumIops, "MaximumThroughput don't match") | ||
assert.Equal(t, converter.MegaBytesToBytes(t3Large.MaximumThroughput), result.Instances["db.t3.large"].MaximumThroughput, "MaximumThroughput don't match") | ||
assert.Equal(t, mock.InstanceT3Large.Vcpu, result.Instances["db.t3.large"].Vcpu, "vCPU don't match") | ||
assert.Equal(t, converter.MegaBytesToBytes(mock.InstanceT3Large.Memory), result.Instances["db.t3.large"].Memory, "Memory don't match") | ||
assert.Equal(t, mock.InstanceT3Large.MaximumIops, result.Instances["db.t3.large"].MaximumIops, "MaximumThroughput don't match") | ||
assert.Equal(t, converter.MegaBytesToBytes(mock.InstanceT3Large.MaximumThroughput), result.Instances["db.t3.large"].MaximumThroughput, "MaximumThroughput don't match") | ||
|
||
assert.Equal(t, t3Small.Vcpu, result.Instances["db.t3.small"].Vcpu, "vCPU don't match") | ||
assert.Equal(t, converter.MegaBytesToBytes(t3Small.Memory), result.Instances["db.t3.small"].Memory, "Memory don't match") | ||
assert.Equal(t, mock.InstanceT3Small.Vcpu, result.Instances["db.t3.small"].Vcpu, "vCPU don't match") | ||
assert.Equal(t, converter.MegaBytesToBytes(mock.InstanceT3Small.Memory), result.Instances["db.t3.small"].Memory, "Memory don't match") | ||
|
||
assert.Equal(t, float64(1), client.GetStatistics().EC2ApiCall, "EC2 API call don't match") | ||
assert.Equal(t, float64(1), fetcher.GetStatistics().EC2ApiCall, "EC2 API call don't match") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Package mocks contains mock for EC2 client | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
aws_ec2 "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
aws_ec2_types "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/qonto/prometheus-rds-exporter/internal/app/ec2" | ||
) | ||
|
||
//nolint:golint,gomnd | ||
var InstanceT3Large = ec2.EC2InstanceMetrics{ | ||
MaximumIops: 15700, | ||
MaximumThroughput: 347.5, | ||
Memory: 8, | ||
Vcpu: 2, | ||
} | ||
|
||
//nolint:golint,gomnd | ||
var InstanceT3Small = ec2.EC2InstanceMetrics{ | ||
MaximumIops: 11800, | ||
MaximumThroughput: 260.62, | ||
Memory: 2, | ||
Vcpu: 2, | ||
} | ||
|
||
type EC2Client struct{} | ||
|
||
func (m EC2Client) DescribeInstanceTypes(ctx context.Context, input *aws_ec2.DescribeInstanceTypesInput, optFns ...func(*aws_ec2.Options)) (*aws_ec2.DescribeInstanceTypesOutput, error) { | ||
var instances []aws_ec2_types.InstanceTypeInfo | ||
|
||
for _, instanceType := range input.InstanceTypes { | ||
//nolint // Hide "missing cases in switch" alert because instanceType has many values. Mock with return empty result for unknown instances | ||
switch instanceType { | ||
case "t3.large": | ||
instances = append(instances, aws_ec2_types.InstanceTypeInfo{ | ||
InstanceType: instanceType, | ||
VCpuInfo: &aws_ec2_types.VCpuInfo{DefaultVCpus: &InstanceT3Large.Vcpu}, | ||
MemoryInfo: &aws_ec2_types.MemoryInfo{SizeInMiB: &InstanceT3Large.Memory}, | ||
EbsInfo: &aws_ec2_types.EbsInfo{EbsOptimizedInfo: &aws_ec2_types.EbsOptimizedInfo{ | ||
MaximumIops: &InstanceT3Large.MaximumIops, | ||
MaximumThroughputInMBps: &InstanceT3Large.MaximumThroughput, | ||
}}, | ||
}) | ||
case "t3.small": | ||
instances = append(instances, aws_ec2_types.InstanceTypeInfo{ | ||
InstanceType: instanceType, | ||
VCpuInfo: &aws_ec2_types.VCpuInfo{DefaultVCpus: &InstanceT3Small.Vcpu}, | ||
MemoryInfo: &aws_ec2_types.MemoryInfo{SizeInMiB: &InstanceT3Small.Memory}, | ||
EbsInfo: &aws_ec2_types.EbsInfo{EbsOptimizedInfo: &aws_ec2_types.EbsOptimizedInfo{ | ||
MaximumIops: &InstanceT3Small.MaximumIops, | ||
MaximumThroughputInMBps: &InstanceT3Small.MaximumThroughput, | ||
}}, | ||
}) | ||
} | ||
} | ||
|
||
return &aws_ec2.DescribeInstanceTypesOutput{InstanceTypes: instances}, nil | ||
} |
Oops, something went wrong.