Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
option: add gRPC Compression (#42)
Browse files Browse the repository at this point in the history
Added the option to pass in a gRPC Compression.
  • Loading branch information
Steven Karis authored and Emmanuel T Odeke committed Jan 24, 2019
1 parent 7f300d5 commit 531677e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ocagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Exporter struct {
grpcClientConn *grpc.ClientConn
reconnectionPeriod time.Duration
resource *resourcepb.Resource
compressor string

startOnce sync.Once
stopCh chan bool
Expand Down Expand Up @@ -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...)
}

Expand Down
15 changes: 15 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions viewdata_to_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 531677e

Please sign in to comment.