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

Implement VMMetrics factory and config V2 #90

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
5 changes: 4 additions & 1 deletion cmd/otelsvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions receiver/vmmetricsreceiver/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
57 changes: 57 additions & 0 deletions receiver/vmmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -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",
})
}
89 changes: 89 additions & 0 deletions receiver/vmmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions receiver/vmmetricsreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 10 additions & 3 deletions receiver/vmmetricsreceiver/metrics_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package vmmetricsreceiver

import (
"context"
"errors"
"fmt"
"runtime"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/spf13/viper"

"github.com/open-telemetry/opentelemetry-service/consumer"
"github.com/open-telemetry/opentelemetry-service/receiver"
)

var (
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

vmr.mu.Lock()
defer vmr.mu.Unlock()

Expand All @@ -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()

Expand Down
19 changes: 19 additions & 0 deletions receiver/vmmetricsreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -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]