-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split connection management away from exporter (#1369)
* Split protocol handling away from exporter This commits adds a ProtocolDriver interface, which the exporter will use to connect to the collector and send both metrics and traces to it. That way, the Exporter type is free from dealing with any connection/protocol details, as this business is taken over by the implementations of the ProtocolDriver interface. The gRPC code from the exporter is moved into the implementation of ProtocolDriver. Currently it only maintains a single connection, just as the Exporter used to do. With the split, most of the Exporter options became actually gRPC connection manager's options. Currently the only option that remained to be Exporter's is about setting the export kind selector. * Update changelog * Increase the test coverage of GRPC driver * Do not close a channel with multiple senders The disconnected channel can be used for sending by multiple goroutines (for example, by metric controller and span processor), so this channel should not be closed at all. Dropping this line closes a race between closing a channel and sending to it. * Simplify new connection handler The callbacks never return an error, so drop the return type from it. * Access clients under a lock The client may change as a result on reconnection in background, so guard against a racy access. * Simplify the GRPC driver a bit The config type was exported earlier to have a consistent way of configuring the driver, when also the multiple connection driver would appear. Since we are not going to add a multiple connection driver, pass the options directly to the driver constructor. Also shorten the name of the constructor to `NewGRPCDriver`. * Merge common gRPC code back into the driver The common code was supposed to be shared between single connection driver and multiple connection driver, but since the latter won't be happening, it makes no sense to keep the not-so-common code in a separate file. Also drop some abstraction too. * Rename the file with gRPC driver implementation * Update changelog * Sleep for a second to trigger the timeout Sometimes CI has it's better moments, so it's blazing fast and manages to finish shutting the exporter down within the 1 microsecond timeout. * Increase the timeout for shutting down the exporter One millisecond is quite short, and I was getting failures locally or in CI: go test ./... + race in ./exporters/otlp 2020/12/14 18:27:54 rpc error: code = Canceled desc = context canceled 2020/12/14 18:27:54 context deadline exceeded --- FAIL: TestNewExporter_withMultipleAttributeTypes (0.37s) otlp_integration_test.go:541: resource span count: got 0, want 1 FAIL FAIL go.opentelemetry.io/otel/exporters/otlp 5.278s or go test ./... + coverage in ./exporters/otlp 2020/12/14 17:41:16 rpc error: code = Canceled desc = context canceled 2020/12/14 17:41:16 exporter disconnected --- FAIL: TestNewExporter_endToEnd (1.53s) --- FAIL: TestNewExporter_endToEnd/WithCompressor (0.41s) otlp_integration_test.go:246: span counts: got 3, want 4 2020/12/14 17:41:18 context canceled FAIL coverage: 35.3% of statements in ./... FAIL go.opentelemetry.io/otel/exporters/otlp 4.753s * Shut down the providers in end to end test This is to make sure that all batched spans are actually flushed before closing the exporter.
- Loading branch information
Showing
13 changed files
with
682 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package otlp // import "go.opentelemetry.io/otel/exporters/otlp" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"google.golang.org/grpc" | ||
|
||
colmetricpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/collector/metrics/v1" | ||
coltracepb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/collector/trace/v1" | ||
metricpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/metrics/v1" | ||
tracepb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/trace/v1" | ||
"go.opentelemetry.io/otel/exporters/otlp/internal/transform" | ||
metricsdk "go.opentelemetry.io/otel/sdk/export/metric" | ||
tracesdk "go.opentelemetry.io/otel/sdk/export/trace" | ||
) | ||
|
||
type grpcDriver struct { | ||
connection *grpcConnection | ||
|
||
lock sync.Mutex | ||
metricsClient colmetricpb.MetricsServiceClient | ||
tracesClient coltracepb.TraceServiceClient | ||
} | ||
|
||
func NewGRPCDriver(opts ...GRPCConnectionOption) ProtocolDriver { | ||
cfg := grpcConnectionConfig{ | ||
collectorAddr: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorPort), | ||
grpcServiceConfig: DefaultGRPCServiceConfig, | ||
} | ||
for _, opt := range opts { | ||
opt(&cfg) | ||
} | ||
d := &grpcDriver{} | ||
d.connection = newGRPCConnection(cfg, d.handleNewConnection) | ||
return d | ||
} | ||
|
||
func (d *grpcDriver) handleNewConnection(cc *grpc.ClientConn) { | ||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
if cc != nil { | ||
d.metricsClient = colmetricpb.NewMetricsServiceClient(cc) | ||
d.tracesClient = coltracepb.NewTraceServiceClient(cc) | ||
} else { | ||
d.metricsClient = nil | ||
d.tracesClient = nil | ||
} | ||
} | ||
|
||
func (d *grpcDriver) Start(ctx context.Context) error { | ||
d.connection.startConnection(ctx) | ||
return nil | ||
} | ||
|
||
func (d *grpcDriver) Stop(ctx context.Context) error { | ||
return d.connection.shutdown(ctx) | ||
} | ||
|
||
func (d *grpcDriver) ExportMetrics(ctx context.Context, cps metricsdk.CheckpointSet, selector metricsdk.ExportKindSelector) error { | ||
if !d.connection.connected() { | ||
return errDisconnected | ||
} | ||
ctx, cancel := d.connection.contextWithStop(ctx) | ||
defer cancel() | ||
|
||
rms, err := transform.CheckpointSet(ctx, selector, cps, 1) | ||
if err != nil { | ||
return err | ||
} | ||
if len(rms) == 0 { | ||
return nil | ||
} | ||
|
||
return d.uploadMetrics(ctx, rms) | ||
} | ||
|
||
func (d *grpcDriver) uploadMetrics(ctx context.Context, protoMetrics []*metricpb.ResourceMetrics) error { | ||
ctx = d.connection.contextWithMetadata(ctx) | ||
err := func() error { | ||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
if d.metricsClient == nil { | ||
return errNoClient | ||
} | ||
_, err := d.metricsClient.Export(ctx, &colmetricpb.ExportMetricsServiceRequest{ | ||
ResourceMetrics: protoMetrics, | ||
}) | ||
return err | ||
}() | ||
if err != nil { | ||
d.connection.setStateDisconnected(err) | ||
} | ||
return err | ||
} | ||
|
||
func (d *grpcDriver) ExportTraces(ctx context.Context, ss []*tracesdk.SpanSnapshot) error { | ||
if !d.connection.connected() { | ||
return errDisconnected | ||
} | ||
ctx, cancel := d.connection.contextWithStop(ctx) | ||
defer cancel() | ||
|
||
protoSpans := transform.SpanData(ss) | ||
if len(protoSpans) == 0 { | ||
return nil | ||
} | ||
|
||
return d.uploadTraces(ctx, protoSpans) | ||
} | ||
|
||
func (d *grpcDriver) uploadTraces(ctx context.Context, protoSpans []*tracepb.ResourceSpans) error { | ||
ctx = d.connection.contextWithMetadata(ctx) | ||
err := func() error { | ||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
if d.tracesClient == nil { | ||
return errNoClient | ||
} | ||
_, err := d.tracesClient.Export(ctx, &coltracepb.ExportTraceServiceRequest{ | ||
ResourceSpans: protoSpans, | ||
}) | ||
return err | ||
}() | ||
if err != nil { | ||
d.connection.setStateDisconnected(err) | ||
} | ||
return err | ||
} |
Oops, something went wrong.