Skip to content

Commit

Permalink
Unify endpoint API that related to OTel exporter (#1401)
Browse files Browse the repository at this point in the history
* Rename `otlp.WithAddress` to `otlp.WithEndpoint`

* Unify the term of the endpoint from exporter

* Update CHANGELOG

* Update example/otel-collector/main.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Switch to the full word collector

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
  • Loading branch information
3 people authored Dec 29, 2020
1 parent 045c3ff commit 5c9221c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Remove duplicate checkout from GitHub Actions workflow (#1407)
- `NewExporter` from `exporters/otlp` now takes a `ProtocolDriver` as a parameter. (#1369)
- Many OTLP Exporter options became gRPC ProtocolDriver options. (#1369)
- Unify endpoint API that related to OTel exporter. (#1401)

### Removed

Expand Down
6 changes: 3 additions & 3 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func initProvider() func() {

// If the OpenTelemetry Collector is running on a local cluster (minikube or
// microk8s), it should be accessible through the NodePort service at the
// `localhost:30080` address. Otherwise, replace `localhost` with the
// address of your cluster. If you run the app inside k8s, then you can
// `localhost:30080` endpoint. Otherwise, replace `localhost` with the
// endpoint of your cluster. If you run the app inside k8s, then you can
// probably connect directly to the service through dns
driver := otlp.NewGRPCDriver(
otlp.WithInsecure(),
otlp.WithAddress("localhost:30080"),
otlp.WithEndpoint("localhost:30080"),
otlp.WithGRPCDialOption(grpc.WithBlock()), // useful for testing
)
exp, err := otlp.NewExporter(ctx, driver)
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ func Example_withDifferentSignalCollectors() {
// Set different endpoints for the metrics and traces collectors
metricsDriver := otlp.NewGRPCDriver(
otlp.WithInsecure(),
otlp.WithAddress("localhost:30080"),
otlp.WithEndpoint("localhost:30080"),
)
tracesDriver := otlp.NewGRPCDriver(
otlp.WithInsecure(),
otlp.WithAddress("localhost:30082"),
otlp.WithEndpoint("localhost:30082"),
)
splitCfg := otlp.SplitConfig{
ForMetrics: metricsDriver,
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/grpcconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (oc *grpcConnection) setConnection(cc *grpc.ClientConn) bool {
}

func (oc *grpcConnection) dialToCollector(ctx context.Context) (*grpc.ClientConn, error) {
addr := oc.c.collectorAddr
endpoint := oc.c.collectorEndpoint

dialOpts := []grpc.DialOption{}
if oc.c.grpcServiceConfig != "" {
Expand All @@ -223,7 +223,7 @@ func (oc *grpcConnection) dialToCollector(ctx context.Context) (*grpc.ClientConn
ctx, cancel := oc.contextWithStop(ctx)
defer cancel()
ctx = oc.contextWithMetadata(ctx)
return grpc.DialContext(ctx, addr, dialOpts...)
return grpc.DialContext(ctx, endpoint, dialOpts...)
}

func (oc *grpcConnection) contextWithMetadata(ctx context.Context) context.Context {
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/grpcdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type grpcDriver struct {

func NewGRPCDriver(opts ...GRPCConnectionOption) ProtocolDriver {
cfg := grpcConnectionConfig{
collectorAddr: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorPort),
collectorEndpoint: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorPort),
grpcServiceConfig: DefaultGRPCServiceConfig,
}
for _, opt := range opts {
Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/grpcoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const (

type grpcConnectionConfig struct {
canDialInsecure bool
collectorAddr string
collectorEndpoint string
compressor string
reconnectionPeriod time.Duration
grpcServiceConfig string
Expand All @@ -82,12 +82,12 @@ func WithInsecure() GRPCConnectionOption {
}
}

// WithAddress allows one to set the address that the exporter will
// WithEndpoint allows one to set the endpoint that the exporter will
// connect to the collector on. If unset, it will instead try to use
// connect to DefaultCollectorHost:DefaultCollectorPort.
func WithAddress(addr string) GRPCConnectionOption {
func WithEndpoint(endpoint string) GRPCConnectionOption {
return func(cfg *grpcConnectionConfig) {
cfg.collectorAddr = addr
cfg.collectorEndpoint = endpoint
}
}

Expand Down
52 changes: 26 additions & 26 deletions exporters/otlp/mock_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ import (
"google.golang.org/grpc"
metadata "google.golang.org/grpc/metadata"

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"
collectormetricpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/collector/metrics/v1"
collectortracepb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/collector/trace/v1"
commonpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/common/v1"
metricpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/metrics/v1"
resourcepb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/resource/v1"
tracepb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/trace/v1"
)

func makeMockCollector(t *testing.T) *mockCol {
return &mockCol{
func makeMockCollector(t *testing.T) *mockCollector {
return &mockCollector{
t: t,
traceSvc: &mockTraceService{
rsm: map[string]*tracepb.ResourceSpans{},
Expand Down Expand Up @@ -74,7 +74,7 @@ func (mts *mockTraceService) getResourceSpans() []*tracepb.ResourceSpans {
return rss
}

func (mts *mockTraceService) Export(ctx context.Context, exp *coltracepb.ExportTraceServiceRequest) (*coltracepb.ExportTraceServiceResponse, error) {
func (mts *mockTraceService) Export(ctx context.Context, exp *collectortracepb.ExportTraceServiceRequest) (*collectortracepb.ExportTraceServiceResponse, error) {
mts.mu.Lock()
mts.headers, _ = metadata.FromIncomingContext(ctx)
defer mts.mu.Unlock()
Expand All @@ -100,7 +100,7 @@ func (mts *mockTraceService) Export(ctx context.Context, exp *coltracepb.ExportT
}
}
}
return &coltracepb.ExportTraceServiceResponse{}, nil
return &collectortracepb.ExportTraceServiceResponse{}, nil
}

func resourceString(res *resourcepb.Resource) string {
Expand Down Expand Up @@ -133,7 +133,7 @@ func (mms *mockMetricService) getMetrics() []*metricpb.Metric {
return append(m, mms.metrics...)
}

func (mms *mockMetricService) Export(ctx context.Context, exp *colmetricpb.ExportMetricsServiceRequest) (*colmetricpb.ExportMetricsServiceResponse, error) {
func (mms *mockMetricService) Export(ctx context.Context, exp *collectormetricpb.ExportMetricsServiceRequest) (*collectormetricpb.ExportMetricsServiceResponse, error) {
mms.mu.Lock()
for _, rm := range exp.GetResourceMetrics() {
// TODO (rghetia) handle multiple resource and library info.
Expand All @@ -142,26 +142,26 @@ func (mms *mockMetricService) Export(ctx context.Context, exp *colmetricpb.Expor
}
}
mms.mu.Unlock()
return &colmetricpb.ExportMetricsServiceResponse{}, nil
return &collectormetricpb.ExportMetricsServiceResponse{}, nil
}

type mockCol struct {
type mockCollector struct {
t *testing.T

traceSvc *mockTraceService
metricSvc *mockMetricService

address string
endpoint string
stopFunc func() error
stopOnce sync.Once
}

var _ coltracepb.TraceServiceServer = (*mockTraceService)(nil)
var _ colmetricpb.MetricsServiceServer = (*mockMetricService)(nil)
var _ collectortracepb.TraceServiceServer = (*mockTraceService)(nil)
var _ collectormetricpb.MetricsServiceServer = (*mockMetricService)(nil)

var errAlreadyStopped = fmt.Errorf("already stopped")

func (mc *mockCol) stop() error {
func (mc *mockCollector) stop() error {
var err = errAlreadyStopped
mc.stopOnce.Do(func() {
if mc.stopFunc != nil {
Expand Down Expand Up @@ -191,37 +191,37 @@ func (mc *mockCol) stop() error {
return err
}

func (mc *mockCol) getSpans() []*tracepb.Span {
func (mc *mockCollector) getSpans() []*tracepb.Span {
return mc.traceSvc.getSpans()
}

func (mc *mockCol) getResourceSpans() []*tracepb.ResourceSpans {
func (mc *mockCollector) getResourceSpans() []*tracepb.ResourceSpans {
return mc.traceSvc.getResourceSpans()
}

func (mc *mockCol) getHeaders() metadata.MD {
func (mc *mockCollector) getHeaders() metadata.MD {
return mc.traceSvc.getHeaders()
}

func (mc *mockCol) getMetrics() []*metricpb.Metric {
func (mc *mockCollector) getMetrics() []*metricpb.Metric {
return mc.metricSvc.getMetrics()
}

// runMockCol is a helper function to create a mockCol
func runMockCol(t *testing.T) *mockCol {
return runMockColAtAddr(t, "localhost:0")
// runMockCollector is a helper function to create a mock Collector
func runMockCollector(t *testing.T) *mockCollector {
return runMockCollectorAtEndpoint(t, "localhost:0")
}

func runMockColAtAddr(t *testing.T, addr string) *mockCol {
ln, err := net.Listen("tcp", addr)
func runMockCollectorAtEndpoint(t *testing.T, endpoint string) *mockCollector {
ln, err := net.Listen("tcp", endpoint)
if err != nil {
t.Fatalf("Failed to get an address: %v", err)
t.Fatalf("Failed to get an endpoint: %v", err)
}

srv := grpc.NewServer()
mc := makeMockCollector(t)
coltracepb.RegisterTraceServiceServer(srv, mc.traceSvc)
colmetricpb.RegisterMetricsServiceServer(srv, mc.metricSvc)
collectortracepb.RegisterTraceServiceServer(srv, mc.traceSvc)
collectormetricpb.RegisterMetricsServiceServer(srv, mc.metricSvc)
go func() {
_ = srv.Serve(ln)
}()
Expand All @@ -233,7 +233,7 @@ func runMockColAtAddr(t *testing.T, addr string) *mockCol {

_, collectorPortStr, _ := net.SplitHostPort(ln.Addr().String())

mc.address = "localhost:" + collectorPortStr
mc.endpoint = "localhost:" + collectorPortStr
mc.stopFunc = deferFunc

return mc
Expand Down
Loading

0 comments on commit 5c9221c

Please sign in to comment.