From 531677ebdb45d5b9f80378a636aaccfd7cf3b964 Mon Sep 17 00:00:00 2001 From: Steven Karis Date: Wed, 23 Jan 2019 16:56:01 -0800 Subject: [PATCH] option: add gRPC Compression (#42) Added the option to pass in a gRPC Compression. --- ocagent.go | 4 ++++ options.go | 15 +++++++++++++++ viewdata_to_metrics_test.go | 6 ++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ocagent.go b/ocagent.go index 2eb5fb9..d7afd6b 100644 --- a/ocagent.go +++ b/ocagent.go @@ -66,6 +66,7 @@ type Exporter struct { grpcClientConn *grpc.ClientConn reconnectionPeriod time.Duration resource *resourcepb.Resource + compressor string startOnce sync.Once stopCh chan bool @@ -253,6 +254,9 @@ func (ae *Exporter) dialToAgent() (*grpc.ClientConn, error) { if ae.canDialInsecure { dialOpts = append(dialOpts, grpc.WithInsecure()) } + if ae.compressor != "" { + dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.UseCompressor(ae.compressor))) + } return grpc.Dial(addr, dialOpts...) } diff --git a/options.go b/options.go index 7df15fd..69b9adb 100644 --- a/options.go +++ b/options.go @@ -76,3 +76,18 @@ func (rp reconnectionPeriod) withExporter(e *Exporter) { func WithReconnectionPeriod(rp time.Duration) ExporterOption { return reconnectionPeriod(rp) } + +type compressorSetter string + +func (c compressorSetter) withExporter(e *Exporter) { + e.compressor = string(c) +} + +// UseCompressor will set the compressor for the gRPC client to use when sending requests. +// It is the responsibility of the caller to ensure that the compressor set has been registered +// with google.golang.org/grpc/encoding. This can be done by encoding.RegisterCompressor. Some +// compressors auto-register on import, such as gzip, which can be registered by calling +// `import _ "google.golang.org/grpc/encoding/gzip"` +func UseCompressor(compressorName string) ExporterOption { + return compressorSetter(compressorName) +} diff --git a/viewdata_to_metrics_test.go b/viewdata_to_metrics_test.go index 23087df..4529b32 100644 --- a/viewdata_to_metrics_test.go +++ b/viewdata_to_metrics_test.go @@ -55,9 +55,11 @@ func TestExportMetrics_conversionFromViewData(t *testing.T) { }() reconnectionPeriod := 2 * time.Millisecond - ocexp, err := NewExporter(WithInsecure(), + ocexp, err := NewExporter( + WithInsecure(), WithAddress(":"+agentPortStr), - WithReconnectionPeriod(reconnectionPeriod)) + WithReconnectionPeriod(reconnectionPeriod), + ) if err != nil { t.Fatalf("Failed to create the ocagent exporter: %v", err) }