Skip to content

Commit

Permalink
Migrate Prometheus to the new MetricsExporter interface (#1477)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Aug 4, 2020
1 parent c4a3829 commit bd921af
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 59 deletions.
2 changes: 1 addition & 1 deletion exporter/prometheusexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestLoadConfig(t *testing.T) {
factories, err := componenttest.ExampleComponents()
assert.NoError(t, err)

factory := &Factory{}
factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

Expand Down
32 changes: 14 additions & 18 deletions exporter/prometheusexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,32 @@
package prometheusexporter

import (
"context"
"net"
"net/http"
"strings"

"github.com/orijtech/prometheus-go-metrics-exporter"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configerror"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

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

// Factory is the factory for Prometheus exporter.
type Factory struct {
// NewFactory creates a factory for OTLP exporter.
func NewFactory() component.ExporterFactory {
return exporterhelper.NewFactory(
typeStr,
createDefaultConfig,
exporterhelper.WithMetrics(createMetricsExporter))
}

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

// CreateDefaultConfig creates the default configuration for exporter.
func (f *Factory) CreateDefaultConfig() configmodels.Exporter {
func createDefaultConfig() configmodels.Exporter {
return &Config{
ExporterSettings: configmodels.ExporterSettings{
TypeVal: typeStr,
Expand All @@ -52,13 +50,11 @@ func (f *Factory) CreateDefaultConfig() configmodels.Exporter {
}
}

// CreateTraceExporter creates a trace exporter based on this config.
func (f *Factory) CreateTraceExporter(logger *zap.Logger, config configmodels.Exporter) (component.TraceExporterOld, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}

// CreateMetricsExporter creates a metrics exporter based on this config.
func (f *Factory) CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (component.MetricsExporterOld, error) {
func createMetricsExporter(
_ context.Context,
_ component.ExporterCreateParams,
cfg configmodels.Exporter,
) (component.MetricsExporter, error) {
pcfg := cfg.(*Config)

addr := strings.TrimSpace(pcfg.Endpoint)
Expand Down
26 changes: 9 additions & 17 deletions exporter/prometheusexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,31 @@
package prometheusexporter

import (
"context"
"testing"

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

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcheck"
"go.opentelemetry.io/collector/config/configerror"
)

func TestCreateDefaultConfig(t *testing.T) {
factory := Factory{}
require.NotNil(t, factory)

cfg := factory.CreateDefaultConfig()
cfg := createDefaultConfig()
assert.NotNil(t, cfg, "failed to create default config")
assert.NoError(t, configcheck.ValidateConfig(cfg))
}

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

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

func TestCreateMetricsExporter(t *testing.T) {
factory := Factory{}
cfg := factory.CreateDefaultConfig()
cfg := createDefaultConfig()
oCfg := cfg.(*Config)
oCfg.Endpoint = ""
consumer, err := factory.CreateMetricsExporter(zap.NewNop(), oCfg)
exp, err := createMetricsExporter(
context.Background(),
component.ExporterCreateParams{Logger: zap.NewNop()},
cfg)
require.Equal(t, errBlankPrometheusAddress, err)
require.Nil(t, consumer)
require.Nil(t, exp)
}
23 changes: 12 additions & 11 deletions exporter/prometheusexporter/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/orijtech/prometheus-go-metrics-exporter"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerdata"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
)

var errBlankPrometheusAddress = errors.New("expecting a non-blank address to run the Prometheus metrics handler")
Expand All @@ -37,19 +37,20 @@ type prometheusExporter struct {
shutdownFunc func() error
}

var _ consumer.MetricsConsumerOld = (*prometheusExporter)(nil)

func (pe *prometheusExporter) Start(_ context.Context, _ component.Host) error {
return nil
}

func (pe *prometheusExporter) ConsumeMetricsData(ctx context.Context, md consumerdata.MetricsData) error {
merged := make(map[string]*metricspb.Metric)
for _, metric := range md.Metrics {
merge(merged, metric)
}
for _, metric := range merged {
_ = pe.exporter.ExportMetric(ctx, md.Node, md.Resource, metric)
func (pe *prometheusExporter) ConsumeMetrics(ctx context.Context, md pdata.Metrics) error {
ocmds := pdatautil.MetricsToMetricsData(md)
for _, ocmd := range ocmds {
merged := make(map[string]*metricspb.Metric)
for _, metric := range ocmd.Metrics {
merge(merged, metric)
}
for _, metric := range merged {
_ = pe.exporter.ExportMetric(ctx, ocmd.Node, ocmd.Resource, metric)
}
}
return nil
}
Expand Down
31 changes: 20 additions & 11 deletions exporter/prometheusexporter/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumerdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
)

func TestPrometheusExporter(t *testing.T) {
Expand All @@ -52,21 +54,24 @@ func TestPrometheusExporter(t *testing.T) {
},
}

factory := Factory{}
factory := NewFactory()
creationParams := component.ExporterCreateParams{Logger: zap.NewNop()}
for _, tt := range tests {
// Run it a few times to ensure that shutdowns exit cleanly.
for j := 0; j < 3; j++ {
consumer, err := factory.CreateMetricsExporter(zap.NewNop(), tt.config)
exp, err := factory.CreateMetricsExporter(context.Background(), creationParams, tt.config)

if tt.wantErr != "" {
require.Equal(t, tt.wantErr, err.Error())
require.Error(t, err)
assert.Equal(t, tt.wantErr, err.Error())
continue
} else {
require.NoError(t, err)
}

assert.NotNil(t, consumer)

assert.NotNil(t, exp)
require.Nil(t, err)
require.NoError(t, consumer.Shutdown(context.Background()))
require.NoError(t, exp.Shutdown(context.Background()))
}
}
}
Expand All @@ -81,16 +86,20 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
Endpoint: ":7777",
}

factory := Factory{}
consumer, err := factory.CreateMetricsExporter(zap.NewNop(), config)
factory := NewFactory()
creationParams := component.ExporterCreateParams{Logger: zap.NewNop()}
exp, err := factory.CreateMetricsExporter(context.Background(), creationParams, config)
assert.NoError(t, err)

defer consumer.Shutdown(context.Background())
t.Cleanup(func() {
require.NoError(t, exp.Shutdown(context.Background()))
})

assert.NotNil(t, consumer)
assert.NotNil(t, exp)

for delta := 0; delta <= 20; delta += 10 {
consumer.ConsumeMetricsData(context.Background(), consumerdata.MetricsData{Metrics: metricBuilder(int64(delta))})
md := pdatautil.MetricsFromMetricsData([]consumerdata.MetricsData{{Metrics: metricBuilder(int64(delta))}})
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))

res, err := http.Get("http://localhost:7777/metrics")
require.NoError(t, err, "Failed to perform a scrape")
Expand Down
2 changes: 1 addition & 1 deletion service/defaultcomponents/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Components() (

exporters, err := component.MakeExporterFactoryMap(
&opencensusexporter.Factory{},
&prometheusexporter.Factory{},
prometheusexporter.NewFactory(),
loggingexporter.NewFactory(),
zipkinexporter.NewFactory(),
jaegerexporter.NewFactory(),
Expand Down

0 comments on commit bd921af

Please sign in to comment.