Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zipkin exporter factory #207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/open-telemetry/opentelemetry-service/exporter/loggingexporter"
"github.com/open-telemetry/opentelemetry-service/exporter/opencensusexporter"
"github.com/open-telemetry/opentelemetry-service/exporter/prometheusexporter"
"github.com/open-telemetry/opentelemetry-service/exporter/zipkinexporter"
"github.com/open-telemetry/opentelemetry-service/oterr"
"github.com/open-telemetry/opentelemetry-service/processor"
"github.com/open-telemetry/opentelemetry-service/processor/addattributesprocessor"
Expand Down Expand Up @@ -58,6 +59,7 @@ func Components() (
&opencensusexporter.Factory{},
&prometheusexporter.Factory{},
&loggingexporter.Factory{},
&zipkinexporter.Factory{},
)
if err != nil {
errs = append(errs, err)
Expand Down
2 changes: 2 additions & 0 deletions defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/open-telemetry/opentelemetry-service/exporter/loggingexporter"
"github.com/open-telemetry/opentelemetry-service/exporter/opencensusexporter"
"github.com/open-telemetry/opentelemetry-service/exporter/prometheusexporter"
"github.com/open-telemetry/opentelemetry-service/exporter/zipkinexporter"
"github.com/open-telemetry/opentelemetry-service/processor"
"github.com/open-telemetry/opentelemetry-service/processor/addattributesprocessor"
"github.com/open-telemetry/opentelemetry-service/processor/attributekeyprocessor"
Expand Down Expand Up @@ -57,6 +58,7 @@ func TestDefaultComponents(t *testing.T) {
"opencensus": &opencensusexporter.Factory{},
"prometheus": &prometheusexporter.Factory{},
"logging": &loggingexporter.Factory{},
"zipkin": &zipkinexporter.Factory{},
}

receivers, processors, exporters, err := Components()
Expand Down
8 changes: 3 additions & 5 deletions examples/demotrace/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ receivers:

exporters:
logging:

# TODO: enable zipkin exporter when it is implemented in otelsvc
# zipkin:
# endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
zipkin:
url: "http://zipkin-all-in-one:9411/api/v2/spans"

# TODO: enable jaeger exporter when it is implemented in otelsvc
# jaeger:
Expand All @@ -29,5 +27,5 @@ processors:
pipelines:
traces:
receivers: [opencensus]
exporters: [logging]
exporters: [logging, zipkin]
processors: [batch, queued-retry]
4 changes: 2 additions & 2 deletions exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Exports trace data to a [Zipkin](https://zipkin.io/) endpoint.

The following settings can be configured:

* `endpoint:` URL to which the exporter is going to send Zipkin trace data. This
* `url:` URL to which the exporter is going to send Zipkin trace data. This
setting doesn't have a default value and must be specified in the configuration.

Example:

```yaml
exporters:
zipkin:
endpoint: "http://some.url:9411/api/v2/spans"
url: "http://some.url:9411/api/v2/spans"
```
1 change: 1 addition & 0 deletions exporter/opencensusexporter/ocsender
Submodule ocsender added at 878037
28 changes: 28 additions & 0 deletions exporter/zipkinexporter/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019, 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 zipkinexporter

import (
"github.com/open-telemetry/opentelemetry-service/config/configmodels"
)

// Config defines configuration settings for the Zipkin exporter.
type Config struct {
configmodels.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.

// The URL to send the Zipkin trace data to (e.g.:
// http://some.url:9411/api/v2/spans).
URL string `mapstructure:"url"`
}
53 changes: 53 additions & 0 deletions exporter/zipkinexporter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019, 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 zipkinexporter

import (
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-service/config"
)

func TestLoadConfig(t *testing.T) {
receivers, processors, exporters, err := config.ExampleComponents()
assert.Nil(t, err)

factory := &Factory{}
exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(
t, path.Join(".", "testdata", "config.yaml"), receivers, processors, exporters,
)

require.NoError(t, err)
require.NotNil(t, cfg)

e0 := cfg.Exporters["zipkin"]

// URL doesn't have a default value so set it directly.
defaultCfg := factory.CreateDefaultConfig().(*Config)
defaultCfg.URL = "http://some.location.org:9411/api/v2/spans"
assert.Equal(t, defaultCfg, e0)

e1 := cfg.Exporters["zipkin/2"]
assert.Equal(t, "zipkin/2", e1.(*Config).Name())
assert.Equal(t, "https://somedest:1234/api/v2/spans", e1.(*Config).URL)
_, _, err = factory.CreateTraceExporter(zap.NewNop(), e1)
require.NoError(t, err)
}
71 changes: 71 additions & 0 deletions exporter/zipkinexporter/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2019, 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 zipkinexporter

import (
"errors"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-service/config/configerror"
"github.com/open-telemetry/opentelemetry-service/config/configmodels"
"github.com/open-telemetry/opentelemetry-service/consumer"
"github.com/open-telemetry/opentelemetry-service/exporter"
)

const (
// The value of "type" key in configuration.
typeStr = "zipkin"
)

// Factory is the factory for OpenCensus exporter.
type Factory struct {
}

// Type gets the type of the Exporter config created by this factory.
func (f *Factory) Type() string {
return typeStr
}

// CreateDefaultConfig creates the default configuration for exporter.
func (f *Factory) CreateDefaultConfig() configmodels.Exporter {
return &Config{
ExporterSettings: configmodels.ExporterSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
}
}

// CreateTraceExporter creates a trace exporter based on this config.
func (f *Factory) CreateTraceExporter(logger *zap.Logger, config configmodels.Exporter) (consumer.TraceConsumer, exporter.StopFunc, error) {
cfg := config.(*Config)

if cfg.URL == "" {
return nil, nil, errors.New("exporter config requires a non-empty 'url'") // TODO: better error
}

ze, err := newZipkinExporter(cfg.URL, "<missing service name>", 0)
if err != nil {
return nil, nil, err
}

return ze, ze.stop, nil
}

// CreateMetricsExporter creates a metrics exporter based on this config.
func (f *Factory) CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (consumer.MetricsConsumer, exporter.StopFunc, error) {
return nil, nil, configerror.ErrDataTypeIsNotSupported
}
64 changes: 64 additions & 0 deletions exporter/zipkinexporter/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019, 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 zipkinexporter

import (
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-service/config/configerror"
)

func TestCreateDefaultConfig(t *testing.T) {
factory := Factory{}
cfg := factory.CreateDefaultConfig()
assert.NotNil(t, cfg, "failed to create default config")
}

func TestCreateMetricsExporter(t *testing.T) {
factory := Factory{}
cfg := factory.CreateDefaultConfig()

_, _, err := factory.CreateMetricsExporter(zap.NewNop(), cfg)
assert.Error(t, err, configerror.ErrDataTypeIsNotSupported)
}

func TestCreateInstanceViaFactory(t *testing.T) {
factory := Factory{}

cfg := factory.CreateDefaultConfig()

// Default config doesn't have default endpoint so creating from it should
// fail.
ze, zeStopFn, err := factory.CreateTraceExporter(
zap.NewNop(),
cfg)
assert.Error(t, err)
assert.Nil(t, ze)
assert.Nil(t, zeStopFn)

// URL doesn't have a default value so set it directly.
zeCfg := cfg.(*Config)
zeCfg.URL = "http://some.location.org:9411/api/v2/spans"
ze, zeStopFn, err = factory.CreateTraceExporter(
zap.NewNop(),
cfg)
assert.NoError(t, err)
assert.NotNil(t, ze)
assert.NotNil(t, zeStopFn)
assert.NoError(t, zeStopFn())
}
17 changes: 17 additions & 0 deletions exporter/zipkinexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
receivers:
examplereceiver:

processors:
exampleprocessor:

exporters:
zipkin:
url: "http://some.location.org:9411/api/v2/spans"
zipkin/2:
url: "https://somedest:1234/api/v2/spans"

pipelines:
traces:
receivers: [examplereceiver]
processors: [exampleprocessor]
exporters: [zipkin, zipkin/2]
29 changes: 10 additions & 19 deletions exporter/zipkinexporter/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"sync"
"time"

commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
zipkinmodel "github.com/openzipkin/zipkin-go/model"
zipkinreporter "github.com/openzipkin/zipkin-go/reporter"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/spf13/viper"
"go.opencensus.io/trace"

commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
"github.com/open-telemetry/opentelemetry-service/consumer"
"github.com/open-telemetry/opentelemetry-service/consumer/consumerdata"
"github.com/open-telemetry/opentelemetry-service/consumer/consumererror"
Expand All @@ -39,10 +39,9 @@ import (

// ZipkinConfig holds the configuration of a Zipkin exporter.
type ZipkinConfig struct {
ServiceName string `mapstructure:"service_name,omitempty"`
Endpoint string `mapstructure:"endpoint,omitempty"`
LocalEndpointURI string `mapstructure:"local_endpoint,omitempty"`
UploadPeriod *time.Duration `mapstructure:"upload_period,omitempty"`
ServiceName string `mapstructure:"service_name,omitempty"`
Endpoint string `mapstructure:"endpoint,omitempty"`
UploadPeriod *time.Duration `mapstructure:"upload_period,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be added to new config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UploadPeriod is an option for the Zipkin exporter that i t should be handled in equivalent form via the pipeline, the same way that we do for OC (eg.: batcher, queued-retry).

ServiceName shouldn't actually be necessary, of course it still possible to have some bug in a client library not providing that information but it doesn't make much sense to inject it at the service as a configurable option, a place holder should suffice.

}

// zipkinExporter is a multiplexing exporter that spawns a new OpenCensus-Go Zipkin
Expand All @@ -54,11 +53,9 @@ type zipkinExporter struct {
// mu protects the fields below
mu sync.Mutex

defaultServiceName string
defaultLocalEndpointURI string
defaultServiceName string

endpointURI string
reporter zipkinreporter.Reporter
reporter zipkinreporter.Reporter
}

// Default values for Zipkin endpoint.
Expand Down Expand Up @@ -96,16 +93,12 @@ func ZipkinExportersFromViper(v *viper.Viper) (tps []consumer.TraceConsumer, mps
if zc.ServiceName != "" {
serviceName = zc.ServiceName
}
localEndpointURI := "192.168.1.5:5454"
if zc.LocalEndpointURI != "" {
localEndpointURI = zc.LocalEndpointURI
}
endpoint := zc.EndpointURL()
var uploadPeriod time.Duration
if zc.UploadPeriod != nil && *zc.UploadPeriod > 0 {
uploadPeriod = *zc.UploadPeriod
}
zle, err := newZipkinExporter(endpoint, serviceName, localEndpointURI, uploadPeriod)
zle, err := newZipkinExporter(endpoint, serviceName, uploadPeriod)
if err != nil {
return nil, nil, nil, fmt.Errorf("Cannot configure Zipkin exporter: %v", err)
}
Expand All @@ -114,17 +107,15 @@ func ZipkinExportersFromViper(v *viper.Viper) (tps []consumer.TraceConsumer, mps
return
}

func newZipkinExporter(finalEndpointURI, defaultServiceName, defaultLocalEndpointURI string, uploadPeriod time.Duration) (*zipkinExporter, error) {
func newZipkinExporter(finalEndpointURI, defaultServiceName string, uploadPeriod time.Duration) (*zipkinExporter, error) {
var opts []zipkinhttp.ReporterOption
if uploadPeriod > 0 {
opts = append(opts, zipkinhttp.BatchInterval(uploadPeriod))
}
reporter := zipkinhttp.NewReporter(finalEndpointURI, opts...)
zle := &zipkinExporter{
endpointURI: finalEndpointURI,
defaultServiceName: defaultServiceName,
defaultLocalEndpointURI: defaultLocalEndpointURI,
reporter: reporter,
defaultServiceName: defaultServiceName,
reporter: reporter,
}
return zle, nil
}
Expand Down