mirror of
https://github.com/status-im/consul.git
synced 2025-02-27 21:00:44 +00:00
* OTElExporter now uses an EndpointProvider to discover the endpoint * OTELSink uses a ConfigProvider to obtain filters and labels configuration * improve tests for otel_sink * Regex logic is moved into client for a method on the TelemetryConfig object * Create a telemetry_config_provider and update deps to use it * Fix conversion * fix import newline * Add logger to hcp client and move telemetry_config out of the client.go file * Add a telemetry_config.go to refactor client.go * Update deps * update hcp deps test * Modify telemetry_config_providers * Check for nil filters * PR review updates * Fix comments and move around pieces * Fix comments * Remove context from client struct * Moved ctx out of sink struct and fixed filters, added a test * Remove named imports, use errors.New if not fformatting * Remove HCP dependencies in telemetry package * Add success metric and move lock only to grab the t.cfgHahs * Update hash * fix nits * Create an equals method and add tests * Improve telemetry_config_provider.go tests * Add race test * Add missing godoc * Remove mock for MetricsClient * Avoid goroutine test panics * trying to kick CI lint issues by upgrading mod * imprve test code and add hasher for testing * Use structure logging for filters, fix error constants, and default to allow all regex * removed hashin and modify logic to simplify * Improve race test and fix PR feedback by removing hash equals and avoid testing the timer.Ticker logic, and instead unit test * Ran make go-mod-tidy * Use errtypes in the test * Add changelog * add safety check for exporter endpoint * remove require.Contains by using error types, fix structure logging, and fix success metric typo in exporter * Fixed race test to have changing config values * Send success metric before modifying config * Avoid the defer and move the success metric under
124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-openapi/runtime"
|
|
hcptelemetry "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/require"
|
|
)
|
|
|
|
type mockTGW struct {
|
|
mockResponse *hcptelemetry.AgentTelemetryConfigOK
|
|
mockError error
|
|
}
|
|
|
|
func (m *mockTGW) AgentTelemetryConfig(params *hcptelemetry.AgentTelemetryConfigParams, authInfo runtime.ClientAuthInfoWriter, opts ...hcptelemetry.ClientOption) (*hcptelemetry.AgentTelemetryConfigOK, error) {
|
|
return m.mockResponse, m.mockError
|
|
}
|
|
func (m *mockTGW) GetLabelValues(params *hcptelemetry.GetLabelValuesParams, authInfo runtime.ClientAuthInfoWriter, opts ...hcptelemetry.ClientOption) (*hcptelemetry.GetLabelValuesOK, error) {
|
|
return hcptelemetry.NewGetLabelValuesOK(), nil
|
|
}
|
|
func (m *mockTGW) QueryRangeBatch(params *hcptelemetry.QueryRangeBatchParams, authInfo runtime.ClientAuthInfoWriter, opts ...hcptelemetry.ClientOption) (*hcptelemetry.QueryRangeBatchOK, error) {
|
|
return hcptelemetry.NewQueryRangeBatchOK(), nil
|
|
}
|
|
func (m *mockTGW) SetTransport(transport runtime.ClientTransport) {}
|
|
|
|
type expectedTelemetryCfg struct {
|
|
endpoint string
|
|
labels map[string]string
|
|
filters string
|
|
refreshInterval time.Duration
|
|
}
|
|
|
|
func TestFetchTelemetryConfig(t *testing.T) {
|
|
t.Parallel()
|
|
for name, tc := range map[string]struct {
|
|
mockResponse *hcptelemetry.AgentTelemetryConfigOK
|
|
mockError error
|
|
wantErr string
|
|
expected *expectedTelemetryCfg
|
|
}{
|
|
"errorsWithFetchFailure": {
|
|
mockError: fmt.Errorf("failed to fetch from HCP"),
|
|
mockResponse: nil,
|
|
wantErr: "failed to fetch from HCP",
|
|
},
|
|
"errorsWithInvalidPayload": {
|
|
mockResponse: &hcptelemetry.AgentTelemetryConfigOK{
|
|
Payload: &models.HashicorpCloudConsulTelemetry20230414AgentTelemetryConfigResponse{},
|
|
},
|
|
mockError: nil,
|
|
wantErr: "invalid response payload",
|
|
},
|
|
"success:": {
|
|
mockResponse: &hcptelemetry.AgentTelemetryConfigOK{
|
|
Payload: &models.HashicorpCloudConsulTelemetry20230414AgentTelemetryConfigResponse{
|
|
RefreshConfig: &models.HashicorpCloudConsulTelemetry20230414RefreshConfig{
|
|
RefreshInterval: "1s",
|
|
},
|
|
TelemetryConfig: &models.HashicorpCloudConsulTelemetry20230414TelemetryConfig{
|
|
Endpoint: "https://test.com",
|
|
Labels: map[string]string{"test": "123"},
|
|
Metrics: &models.HashicorpCloudConsulTelemetry20230414TelemetryMetricsConfig{
|
|
IncludeList: []string{"consul", "test"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expected: &expectedTelemetryCfg{
|
|
endpoint: "https://test.com/v1/metrics",
|
|
labels: map[string]string{"test": "123"},
|
|
filters: "consul|test",
|
|
refreshInterval: 1 * time.Second,
|
|
},
|
|
},
|
|
} {
|
|
tc := tc
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
c := &hcpClient{
|
|
tgw: &mockTGW{
|
|
mockError: tc.mockError,
|
|
mockResponse: tc.mockResponse,
|
|
},
|
|
}
|
|
|
|
telemetryCfg, err := c.FetchTelemetryConfig(context.Background())
|
|
|
|
if tc.wantErr != "" {
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), tc.wantErr)
|
|
require.Nil(t, telemetryCfg)
|
|
return
|
|
}
|
|
|
|
urlEndpoint, err := url.Parse(tc.expected.endpoint)
|
|
require.NoError(t, err)
|
|
|
|
regexFilters, err := regexp.Compile(tc.expected.filters)
|
|
require.NoError(t, err)
|
|
|
|
expectedCfg := &TelemetryConfig{
|
|
MetricsConfig: &MetricsConfig{
|
|
Endpoint: urlEndpoint,
|
|
Filters: regexFilters,
|
|
Labels: tc.expected.labels,
|
|
},
|
|
RefreshConfig: &RefreshConfig{
|
|
RefreshInterval: tc.expected.refreshInterval,
|
|
},
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, expectedCfg, telemetryCfg)
|
|
})
|
|
}
|
|
}
|