mirror of https://github.com/hashicorp/consul
backport of commit 94998bec4b
(#17518)
Co-authored-by: Ashvitha Sridharan <ashvitha.sridharan@hashicorp.com>pull/17531/head
parent
1330cc0711
commit
fe5a963c62
|
@ -115,15 +115,7 @@ func (c *hcpClient) FetchTelemetryConfig(ctx context.Context) (*TelemetryConfig,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
payloadConfig := resp.Payload.TelemetryConfig
|
return convertTelemetryConfig(resp)
|
||||||
return &TelemetryConfig{
|
|
||||||
Endpoint: payloadConfig.Endpoint,
|
|
||||||
Labels: payloadConfig.Labels,
|
|
||||||
MetricsConfig: &MetricsConfig{
|
|
||||||
Filters: payloadConfig.Metrics.IncludeList,
|
|
||||||
Endpoint: payloadConfig.Metrics.Endpoint,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *hcpClient) FetchBootstrap(ctx context.Context) (*BootstrapConfig, error) {
|
func (c *hcpClient) FetchBootstrap(ctx context.Context) (*BootstrapConfig, error) {
|
||||||
|
@ -281,6 +273,29 @@ func (c *hcpClient) DiscoverServers(ctx context.Context) ([]string, error) {
|
||||||
return servers, nil
|
return servers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertTelemetryConfig validates the AgentTelemetryConfig payload and converts it into a TelemetryConfig object.
|
||||||
|
func convertTelemetryConfig(resp *hcptelemetry.AgentTelemetryConfigOK) (*TelemetryConfig, error) {
|
||||||
|
if resp.Payload == nil {
|
||||||
|
return nil, fmt.Errorf("missing payload")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.Payload.TelemetryConfig == nil {
|
||||||
|
return nil, fmt.Errorf("missing telemetry config")
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadConfig := resp.Payload.TelemetryConfig
|
||||||
|
var metricsConfig MetricsConfig
|
||||||
|
if payloadConfig.Metrics != nil {
|
||||||
|
metricsConfig.Endpoint = payloadConfig.Metrics.Endpoint
|
||||||
|
metricsConfig.Filters = payloadConfig.Metrics.IncludeList
|
||||||
|
}
|
||||||
|
return &TelemetryConfig{
|
||||||
|
Endpoint: payloadConfig.Endpoint,
|
||||||
|
Labels: payloadConfig.Labels,
|
||||||
|
MetricsConfig: &metricsConfig,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Enabled verifies if telemetry is enabled by ensuring a valid endpoint has been retrieved.
|
// Enabled verifies if telemetry is enabled by ensuring a valid endpoint has been retrieved.
|
||||||
// It returns full metrics endpoint and true if a valid endpoint was obtained.
|
// It returns full metrics endpoint and true if a valid endpoint was obtained.
|
||||||
func (t *TelemetryConfig) Enabled() (string, bool) {
|
func (t *TelemetryConfig) Enabled() (string, bool) {
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-telemetry-gateway/preview/2023-04-14/client/consul_telemetry_service"
|
||||||
|
"github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-telemetry-gateway/preview/2023-04-14/models"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -73,3 +75,75 @@ func TestFetchTelemetryConfig(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConvertTelemetryConfig(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
for name, test := range map[string]struct {
|
||||||
|
resp *consul_telemetry_service.AgentTelemetryConfigOK
|
||||||
|
expectedTelemetryCfg *TelemetryConfig
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
"success": {
|
||||||
|
resp: &consul_telemetry_service.AgentTelemetryConfigOK{
|
||||||
|
Payload: &models.HashicorpCloudConsulTelemetry20230414AgentTelemetryConfigResponse{
|
||||||
|
TelemetryConfig: &models.HashicorpCloudConsulTelemetry20230414TelemetryConfig{
|
||||||
|
Endpoint: "https://test.com",
|
||||||
|
Labels: map[string]string{"test": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedTelemetryCfg: &TelemetryConfig{
|
||||||
|
Endpoint: "https://test.com",
|
||||||
|
Labels: map[string]string{"test": "test"},
|
||||||
|
MetricsConfig: &MetricsConfig{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"successWithMetricsConfig": {
|
||||||
|
resp: &consul_telemetry_service.AgentTelemetryConfigOK{
|
||||||
|
Payload: &models.HashicorpCloudConsulTelemetry20230414AgentTelemetryConfigResponse{
|
||||||
|
TelemetryConfig: &models.HashicorpCloudConsulTelemetry20230414TelemetryConfig{
|
||||||
|
Endpoint: "https://test.com",
|
||||||
|
Labels: map[string]string{"test": "test"},
|
||||||
|
Metrics: &models.HashicorpCloudConsulTelemetry20230414TelemetryMetricsConfig{
|
||||||
|
Endpoint: "https://metrics-test.com",
|
||||||
|
IncludeList: []string{"consul.raft.apply"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedTelemetryCfg: &TelemetryConfig{
|
||||||
|
Endpoint: "https://test.com",
|
||||||
|
Labels: map[string]string{"test": "test"},
|
||||||
|
MetricsConfig: &MetricsConfig{
|
||||||
|
Endpoint: "https://metrics-test.com",
|
||||||
|
Filters: []string{"consul.raft.apply"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"errorsWithNilPayload": {
|
||||||
|
resp: &consul_telemetry_service.AgentTelemetryConfigOK{},
|
||||||
|
wantErr: "missing payload",
|
||||||
|
},
|
||||||
|
"errorsWithNilTelemetryConfig": {
|
||||||
|
resp: &consul_telemetry_service.AgentTelemetryConfigOK{
|
||||||
|
Payload: &models.HashicorpCloudConsulTelemetry20230414AgentTelemetryConfigResponse{},
|
||||||
|
},
|
||||||
|
wantErr: "missing telemetry config",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
test := test
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
telemetryCfg, err := convertTelemetryConfig(test.resp)
|
||||||
|
if test.wantErr != "" {
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Nil(t, telemetryCfg)
|
||||||
|
require.Contains(t, err.Error(), test.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, test.expectedTelemetryCfg, telemetryCfg)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue