diff --git a/cmd/otelsvc/main.go b/cmd/otelsvc/main.go index 4eb9d1d8e843..6173d5ed5346 100644 --- a/cmd/otelsvc/main.go +++ b/cmd/otelsvc/main.go @@ -16,7 +16,10 @@ // and traces and exports to a configured backend. package main -import "github.com/open-telemetry/opentelemetry-service/otelsvc" +import ( + "github.com/open-telemetry/opentelemetry-service/otelsvc" + _ "github.com/open-telemetry/opentelemetry-service/receiver/vmmetricsreceiver" +) func main() { otelsvc.Run() diff --git a/receiver/vmmetricsreceiver/config.go b/receiver/vmmetricsreceiver/config.go new file mode 100644 index 000000000000..58ecd3981492 --- /dev/null +++ b/receiver/vmmetricsreceiver/config.go @@ -0,0 +1,30 @@ +// 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 vmmetricsreceiver + +import ( + "time" + + "github.com/open-telemetry/opentelemetry-service/models" +) + +// ConfigV2 defines configuration for VMMetrics receiver. +type ConfigV2 struct { + models.ReceiverSettings `mapstructure:",squash"` + ScrapeInterval time.Duration `mapstructure:"scrape_interval"` + MountPoint string `mapstructure:"mount_point"` + ProcessMountPoint string `mapstructure:"process_mount_point"` + MetricPrefix string `mapstructure:"metric_prefix"` +} diff --git a/receiver/vmmetricsreceiver/config_test.go b/receiver/vmmetricsreceiver/config_test.go new file mode 100644 index 000000000000..18e9ddcb8967 --- /dev/null +++ b/receiver/vmmetricsreceiver/config_test.go @@ -0,0 +1,57 @@ +// 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 vmmetricsreceiver + +import ( + "path" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/open-telemetry/opentelemetry-service/configv2" + "github.com/open-telemetry/opentelemetry-service/models" + "github.com/open-telemetry/opentelemetry-service/receiver" +) + +var _ = configv2.RegisterTestFactories() + +func TestLoadConfig(t *testing.T) { + factory := receiver.GetReceiverFactory(typeStr) + + config, err := configv2.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml")) + + require.NoError(t, err) + require.NotNil(t, config) + + assert.Equal(t, len(config.Receivers), 2) + + r0 := config.Receivers["vmmetrics"] + assert.Equal(t, r0, factory.CreateDefaultConfig()) + + r1 := config.Receivers["vmmetrics/customname"].(*ConfigV2) + assert.Equal(t, r1, + &ConfigV2{ + ReceiverSettings: models.ReceiverSettings{ + TypeVal: typeStr, + NameVal: "vmmetrics/customname", + }, + ScrapeInterval: 5 * time.Second, + MetricPrefix: "testmetric", + MountPoint: "/mountpoint", + ProcessMountPoint: "/proc", + }) +} diff --git a/receiver/vmmetricsreceiver/factory.go b/receiver/vmmetricsreceiver/factory.go new file mode 100644 index 000000000000..57e796de3406 --- /dev/null +++ b/receiver/vmmetricsreceiver/factory.go @@ -0,0 +1,89 @@ +// 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 vmmetricsreceiver + +import ( + "context" + + "go.uber.org/zap" + + "github.com/open-telemetry/opentelemetry-service/consumer" + "github.com/open-telemetry/opentelemetry-service/models" + "github.com/open-telemetry/opentelemetry-service/receiver" +) + +// This file implements factory for VMMetrics receiver. + +var _ = receiver.RegisterReceiverFactory(&Factory{}) + +const ( + // The value of "type" key in configuration. + typeStr = "vmmetrics" +) + +// Factory is the factory for receiver. +type Factory struct { +} + +// Type gets the type of the Receiver config created by this factory. +func (f *Factory) Type() string { + return typeStr +} + +// CustomUnmarshaler returns custom unmarshaler for this config. +func (f *Factory) CustomUnmarshaler() receiver.CustomUnmarshaler { + return nil +} + +// CreateDefaultConfig creates the default configuration for receiver. +func (f *Factory) CreateDefaultConfig() models.Receiver { + return &ConfigV2{ + ReceiverSettings: models.ReceiverSettings{ + TypeVal: typeStr, + NameVal: typeStr, + }, + } +} + +// CreateTraceReceiver creates a trace receiver based on provided config. +func (f *Factory) CreateTraceReceiver( + ctx context.Context, + logger *zap.Logger, + cfg models.Receiver, + nextConsumer consumer.TraceConsumer, +) (receiver.TraceReceiver, error) { + // VMMetrics does not support traces + return nil, models.ErrDataTypeIsNotSupported +} + +// CreateMetricsReceiver creates a metrics receiver based on provided config. +func (f *Factory) CreateMetricsReceiver( + logger *zap.Logger, + config models.Receiver, + consumer consumer.MetricsConsumer, +) (receiver.MetricsReceiver, error) { + + cfg := config.(*ConfigV2) + + vmc, err := NewVMMetricsCollector(cfg.ScrapeInterval, cfg.MountPoint, cfg.ProcessMountPoint, cfg.MetricPrefix, consumer) + if err != nil { + return nil, err + } + + vmr := &Receiver{ + vmc: vmc, + } + return vmr, nil +} diff --git a/receiver/vmmetricsreceiver/factory_test.go b/receiver/vmmetricsreceiver/factory_test.go new file mode 100644 index 000000000000..2a52e84ffdbd --- /dev/null +++ b/receiver/vmmetricsreceiver/factory_test.go @@ -0,0 +1,45 @@ +// 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 vmmetricsreceiver + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "go.uber.org/zap" + + "github.com/open-telemetry/opentelemetry-service/models" + "github.com/open-telemetry/opentelemetry-service/receiver" +) + +func TestCreateDefaultConfig(t *testing.T) { + factory := receiver.GetReceiverFactory(typeStr) + cfg := factory.CreateDefaultConfig() + assert.NotNil(t, cfg, "failed to create default config") +} + +func TestCreateReceiver(t *testing.T) { + factory := receiver.GetReceiverFactory(typeStr) + cfg := factory.CreateDefaultConfig() + + tReceiver, err := factory.CreateTraceReceiver(context.Background(), zap.NewNop(), cfg, nil) + assert.Equal(t, err, models.ErrDataTypeIsNotSupported) + assert.Nil(t, tReceiver) + + mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil) + assert.Nil(t, err) + assert.NotNil(t, mReceiver) +} diff --git a/receiver/vmmetricsreceiver/metrics_receiver.go b/receiver/vmmetricsreceiver/metrics_receiver.go index 1acb4f05b745..cdf0b9ba3410 100644 --- a/receiver/vmmetricsreceiver/metrics_receiver.go +++ b/receiver/vmmetricsreceiver/metrics_receiver.go @@ -15,7 +15,6 @@ package vmmetricsreceiver import ( - "context" "errors" "fmt" "runtime" @@ -25,6 +24,7 @@ import ( "github.com/spf13/viper" "github.com/open-telemetry/opentelemetry-service/consumer" + "github.com/open-telemetry/opentelemetry-service/receiver" ) var ( @@ -76,8 +76,15 @@ func New(v *viper.Viper, consumer consumer.MetricsConsumer) (*Receiver, error) { return vmr, nil } +const metricsSource string = "VMMetrics" + +// MetricsSource returns the name of the metrics data source. +func (vmr *Receiver) MetricsSource() string { + return metricsSource +} + // StartMetricsReception scrapes VM metrics based on the OS platform. -func (vmr *Receiver) StartMetricsReception(ctx context.Context, asyncErrorChan chan<- error) error { +func (vmr *Receiver) StartMetricsReception(host receiver.Host) error { vmr.mu.Lock() defer vmr.mu.Unlock() @@ -97,7 +104,7 @@ func (vmr *Receiver) StartMetricsReception(ctx context.Context, asyncErrorChan c } // StopMetricsReception stops and cancels the underlying VM metrics scrapers. -func (vmr *Receiver) StopMetricsReception(ctx context.Context) error { +func (vmr *Receiver) StopMetricsReception() error { vmr.mu.Lock() defer vmr.mu.Unlock() diff --git a/receiver/vmmetricsreceiver/testdata/config.yaml b/receiver/vmmetricsreceiver/testdata/config.yaml new file mode 100644 index 000000000000..35a39afaca3f --- /dev/null +++ b/receiver/vmmetricsreceiver/testdata/config.yaml @@ -0,0 +1,19 @@ +receivers: + vmmetrics: + vmmetrics/customname: + scrape_interval: 5s + mount_point: /mountpoint + process_mount_point: /proc + metric_prefix: testmetric + +processors: + exampleprocessor: + +exporters: + exampleexporter: + +pipelines: + traces: + receivers: [vmmetrics] + processors: [exampleprocessor] + exporters: [exampleexporter]