From a353527eaa3198be6bcaca63d08af8ca68833c74 Mon Sep 17 00:00:00 2001 From: Ashvitha Sridharan Date: Wed, 26 Jul 2023 16:48:23 -0400 Subject: [PATCH] Remove HCP dependencies in telemetry package --- agent/hcp/client/metrics_client.go | 8 ++------ agent/hcp/client/mock_metrics_client.go | 4 +++- agent/hcp/deps.go | 2 +- agent/hcp/telemetry/otel_exporter.go | 12 ++++++++---- agent/hcp/telemetry/otel_exporter_test.go | 6 ++---- agent/hcp/telemetry/otel_sink.go | 4 +--- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/agent/hcp/client/metrics_client.go b/agent/hcp/client/metrics_client.go index 0bcb90b81ce2..3c5b5c4fb9d6 100644 --- a/agent/hcp/client/metrics_client.go +++ b/agent/hcp/client/metrics_client.go @@ -18,6 +18,7 @@ import ( "golang.org/x/oauth2" "google.golang.org/protobuf/proto" + "github.com/hashicorp/consul/agent/hcp/telemetry" "github.com/hashicorp/consul/version" ) @@ -38,11 +39,6 @@ const ( defaultErrRespBodyLength = 100 ) -// MetricsClient exports Consul metrics in OTLP format to the HCP Telemetry Gateway. -type MetricsClient interface { - ExportMetrics(ctx context.Context, protoMetrics *metricpb.ResourceMetrics, endpoint string) error -} - // cloudConfig represents cloud config for TLS abstracted in an interface for easy testing. type CloudConfig interface { HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpcfg.HCPConfig, error) @@ -58,7 +54,7 @@ type otlpClient struct { // NewMetricsClient returns a configured MetricsClient. // The current implementation uses otlpClient to provide retry functionality. -func NewMetricsClient(ctx context.Context, cfg CloudConfig) (MetricsClient, error) { +func NewMetricsClient(ctx context.Context, cfg CloudConfig) (telemetry.MetricsClient, error) { if cfg == nil { return nil, fmt.Errorf("failed to init telemetry client: provide valid cloudCfg (Cloud Configuration for TLS)") } diff --git a/agent/hcp/client/mock_metrics_client.go b/agent/hcp/client/mock_metrics_client.go index a30b1f1c62c0..bc46adf0fddd 100644 --- a/agent/hcp/client/mock_metrics_client.go +++ b/agent/hcp/client/mock_metrics_client.go @@ -1,5 +1,7 @@ package client +import "github.com/hashicorp/consul/agent/hcp/telemetry" + type MockMetricsClient struct { - MetricsClient + telemetry.MetricsClient } diff --git a/agent/hcp/deps.go b/agent/hcp/deps.go index 60883e29d158..7b99b3303d0a 100644 --- a/agent/hcp/deps.go +++ b/agent/hcp/deps.go @@ -62,7 +62,7 @@ func NewDeps(cfg config.CloudConfig, logger hclog.Logger) (Deps, error) { func sink( ctx context.Context, hcpClient hcpclient.Client, - metricsClient hcpclient.MetricsClient, + metricsClient telemetry.MetricsClient, ) (metrics.MetricSink, error) { logger := hclog.FromContext(ctx).Named("sink") reqCtx, cancel := context.WithTimeout(ctx, 5*time.Second) diff --git a/agent/hcp/telemetry/otel_exporter.go b/agent/hcp/telemetry/otel_exporter.go index 72cdeecac16f..8f6a88f27e52 100644 --- a/agent/hcp/telemetry/otel_exporter.go +++ b/agent/hcp/telemetry/otel_exporter.go @@ -9,10 +9,14 @@ import ( "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/aggregation" "go.opentelemetry.io/otel/sdk/metric/metricdata" - - hcpclient "github.com/hashicorp/consul/agent/hcp/client" + metricpb "go.opentelemetry.io/proto/otlp/metrics/v1" ) +// MetricsClient exports Consul metrics in OTLP format to the desired endpoint. +type MetricsClient interface { + ExportMetrics(ctx context.Context, protoMetrics *metricpb.ResourceMetrics, endpoint string) error +} + // EndpointProvider provides the endpoint where metrics are exported to by the OTELExporter. // EndpointProvider exposes the GetEndpoint() interface method to fetch the endpoint. // This abstraction layer offers flexibility, in particular for dynamic configuration or changes to the endpoint. @@ -24,12 +28,12 @@ type EndpointProvider interface { // The exporter is used by a OTEL Metrics SDK PeriodicReader to export aggregated metrics. // This allows us to use a custom client - HCP authenticated MetricsClient. type OTELExporter struct { - client hcpclient.MetricsClient + client MetricsClient endpointProvider EndpointProvider } // NewOTELExporter returns a configured OTELExporter. -func NewOTELExporter(client hcpclient.MetricsClient, endpointProvider EndpointProvider) *OTELExporter { +func NewOTELExporter(client MetricsClient, endpointProvider EndpointProvider) *OTELExporter { return &OTELExporter{ client: client, endpointProvider: endpointProvider, diff --git a/agent/hcp/telemetry/otel_exporter_test.go b/agent/hcp/telemetry/otel_exporter_test.go index 3229d3748c91..3c2c7e3cd2c4 100644 --- a/agent/hcp/telemetry/otel_exporter_test.go +++ b/agent/hcp/telemetry/otel_exporter_test.go @@ -15,8 +15,6 @@ import ( "go.opentelemetry.io/otel/sdk/metric/metricdata" "go.opentelemetry.io/otel/sdk/resource" metricpb "go.opentelemetry.io/proto/otlp/metrics/v1" - - "github.com/hashicorp/consul/agent/hcp/client" ) type mockMetricsClient struct { @@ -70,7 +68,7 @@ func TestExport(t *testing.T) { for name, test := range map[string]struct { wantErr string metrics *metricdata.ResourceMetrics - client client.MetricsClient + client MetricsClient }{ "earlyReturnWithoutScopeMetrics": { client: &mockMetricsClient{}, @@ -123,7 +121,7 @@ func TestExport(t *testing.T) { // sets a shared global sink. func TestExport_CustomMetrics(t *testing.T) { for name, tc := range map[string]struct { - client client.MetricsClient + client MetricsClient metricKey []string operation string }{ diff --git a/agent/hcp/telemetry/otel_sink.go b/agent/hcp/telemetry/otel_sink.go index 7e37fe2baaa9..60968fe4e009 100644 --- a/agent/hcp/telemetry/otel_sink.go +++ b/agent/hcp/telemetry/otel_sink.go @@ -15,8 +15,6 @@ import ( otelmetric "go.opentelemetry.io/otel/metric" otelsdk "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/resource" - - "github.com/hashicorp/consul/agent/hcp/client" ) // DefaultExportInterval is a default time interval between export of aggregated metrics. @@ -70,7 +68,7 @@ type OTELSink struct { // NewOTELReader returns a configured OTEL PeriodicReader to export metrics every X seconds. // It configures the reader with a custom OTELExporter with a MetricsClient to transform and export // metrics in OTLP format to an external url. -func NewOTELReader(client client.MetricsClient, endpointProvider EndpointProvider, exportInterval time.Duration) otelsdk.Reader { +func NewOTELReader(client MetricsClient, endpointProvider EndpointProvider, exportInterval time.Duration) otelsdk.Reader { exporter := NewOTELExporter(client, endpointProvider) return otelsdk.NewPeriodicReader(exporter, otelsdk.WithInterval(exportInterval)) }