Skip to content

Commit b48d0cc

Browse files
author
Tigran Najaryan
committed
Implement VMMetrics factory and config V2
Github issue: open-telemetry#35 Testing done: make && make otelsvc
1 parent ed9edaa commit b48d0cc

File tree

7 files changed

+257
-4
lines changed

7 files changed

+257
-4
lines changed

cmd/otelsvc/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
// and traces and exports to a configured backend.
1717
package main
1818

19-
import "github.com/open-telemetry/opentelemetry-service/otelsvc"
19+
import (
20+
"github.com/open-telemetry/opentelemetry-service/otelsvc"
21+
_ "github.com/open-telemetry/opentelemetry-service/receiver/vmmetricsreceiver"
22+
)
2023

2124
func main() {
2225
otelsvc.Run()

receiver/vmmetricsreceiver/config.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package vmmetricsreceiver
16+
17+
import (
18+
"time"
19+
20+
"github.com/open-telemetry/opentelemetry-service/models"
21+
)
22+
23+
// ConfigV2 defines configuration for Prometheus receiver.
24+
type ConfigV2 struct {
25+
models.ReceiverSettings `mapstructure:",squash"`
26+
ScrapeInterval time.Duration `mapstructure:"scrape_interval"`
27+
MountPoint string `mapstructure:"mount_point"`
28+
ProcessMountPoint string `mapstructure:"process_mount_point"`
29+
MetricPrefix string `mapstructure:"metric_prefix"`
30+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package vmmetricsreceiver
16+
17+
import (
18+
"path"
19+
"testing"
20+
"time"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
25+
"github.com/open-telemetry/opentelemetry-service/configv2"
26+
"github.com/open-telemetry/opentelemetry-service/models"
27+
"github.com/open-telemetry/opentelemetry-service/receiver"
28+
)
29+
30+
var _ = configv2.RegisterTestFactories()
31+
32+
func TestLoadConfig(t *testing.T) {
33+
factory := receiver.GetReceiverFactory(typeStr)
34+
35+
config, err := configv2.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"))
36+
37+
require.NoError(t, err)
38+
require.NotNil(t, config)
39+
40+
assert.Equal(t, len(config.Receivers), 2)
41+
42+
r0 := config.Receivers["vmmetrics"]
43+
assert.Equal(t, r0, factory.CreateDefaultConfig())
44+
45+
r1 := config.Receivers["vmmetrics/customname"].(*ConfigV2)
46+
assert.Equal(t, r1,
47+
&ConfigV2{
48+
ReceiverSettings: models.ReceiverSettings{
49+
TypeVal: typeStr,
50+
NameVal: "vmmetrics/customname",
51+
},
52+
ScrapeInterval: 5 * time.Second,
53+
MetricPrefix: "testmetric",
54+
MountPoint: "/mountpoint",
55+
ProcessMountPoint: "/proc",
56+
})
57+
}

receiver/vmmetricsreceiver/factory.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package vmmetricsreceiver
16+
17+
import (
18+
"context"
19+
20+
"go.uber.org/zap"
21+
22+
"github.com/open-telemetry/opentelemetry-service/consumer"
23+
"github.com/open-telemetry/opentelemetry-service/models"
24+
"github.com/open-telemetry/opentelemetry-service/receiver"
25+
)
26+
27+
// This file implements config V2 for Prometheus receiver.
28+
29+
var _ = receiver.RegisterReceiverFactory(&ReceiverFactory{})
30+
31+
const (
32+
// The value of "type" key in configuration.
33+
typeStr = "vmmetrics"
34+
)
35+
36+
// ReceiverFactory is the factory for receiver.
37+
type ReceiverFactory struct {
38+
}
39+
40+
// Type gets the type of the Receiver config created by this factory.
41+
func (f *ReceiverFactory) Type() string {
42+
return typeStr
43+
}
44+
45+
// CustomUnmarshaler returns custom unmarshaler for this config.
46+
func (f *ReceiverFactory) CustomUnmarshaler() receiver.CustomUnmarshaler {
47+
return nil
48+
}
49+
50+
// CreateDefaultConfig creates the default configuration for receiver.
51+
func (f *ReceiverFactory) CreateDefaultConfig() models.Receiver {
52+
return &ConfigV2{
53+
ReceiverSettings: models.ReceiverSettings{
54+
TypeVal: typeStr,
55+
NameVal: typeStr,
56+
},
57+
}
58+
}
59+
60+
// CreateTraceReceiver creates a trace receiver based on provided config.
61+
func (f *ReceiverFactory) CreateTraceReceiver(
62+
ctx context.Context,
63+
logger *zap.Logger,
64+
cfg models.Receiver,
65+
nextConsumer consumer.TraceConsumer,
66+
) (receiver.TraceReceiver, error) {
67+
// Prometheus does not support traces
68+
return nil, models.ErrDataTypeIsNotSupported
69+
}
70+
71+
// CreateMetricsReceiver creates a metrics receiver based on provided config.
72+
func (f *ReceiverFactory) CreateMetricsReceiver(
73+
logger *zap.Logger,
74+
config models.Receiver,
75+
consumer consumer.MetricsConsumer,
76+
) (receiver.MetricsReceiver, error) {
77+
78+
cfg := config.(*ConfigV2)
79+
80+
vmc, err := NewVMMetricsCollector(cfg.ScrapeInterval, cfg.MountPoint, cfg.ProcessMountPoint, cfg.MetricPrefix, consumer)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
vmr := &Receiver{
86+
vmc: vmc,
87+
}
88+
return vmr, nil
89+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package vmmetricsreceiver
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"go.uber.org/zap"
22+
23+
"github.com/open-telemetry/opentelemetry-service/models"
24+
"github.com/open-telemetry/opentelemetry-service/receiver"
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestCreateDefaultConfig(t *testing.T) {
29+
factory := receiver.GetReceiverFactory(typeStr)
30+
cfg := factory.CreateDefaultConfig()
31+
assert.NotNil(t, cfg, "failed to create default config")
32+
}
33+
34+
func TestCreateReceiver(t *testing.T) {
35+
factory := receiver.GetReceiverFactory(typeStr)
36+
cfg := factory.CreateDefaultConfig()
37+
38+
tReceiver, err := factory.CreateTraceReceiver(context.Background(), zap.NewNop(), cfg, nil)
39+
assert.Equal(t, err, models.ErrDataTypeIsNotSupported)
40+
assert.Nil(t, tReceiver)
41+
42+
// The default config does not provide scrape_config so we expect that metrics receiver
43+
// creation must also fail.
44+
mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil)
45+
assert.Nil(t, err)
46+
assert.NotNil(t, mReceiver)
47+
}

receiver/vmmetricsreceiver/metrics_receiver.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
package vmmetricsreceiver
1616

1717
import (
18-
"context"
1918
"errors"
2019
"fmt"
2120
"runtime"
2221
"sync"
2322
"time"
2423

24+
"github.com/open-telemetry/opentelemetry-service/receiver"
25+
2526
"github.com/spf13/viper"
2627

2728
"github.com/open-telemetry/opentelemetry-service/consumer"
@@ -76,8 +77,15 @@ func New(v *viper.Viper, consumer consumer.MetricsConsumer) (*Receiver, error) {
7677
return vmr, nil
7778
}
7879

80+
const metricsSource string = "VMMetrics"
81+
82+
// MetricsSource returns the name of the metrics data source.
83+
func (vmr *Receiver) MetricsSource() string {
84+
return metricsSource
85+
}
86+
7987
// StartMetricsReception scrapes VM metrics based on the OS platform.
80-
func (vmr *Receiver) StartMetricsReception(ctx context.Context, asyncErrorChan chan<- error) error {
88+
func (vmr *Receiver) StartMetricsReception(host receiver.Host) error {
8189
vmr.mu.Lock()
8290
defer vmr.mu.Unlock()
8391

@@ -97,7 +105,7 @@ func (vmr *Receiver) StartMetricsReception(ctx context.Context, asyncErrorChan c
97105
}
98106

99107
// StopMetricsReception stops and cancels the underlying VM metrics scrapers.
100-
func (vmr *Receiver) StopMetricsReception(ctx context.Context) error {
108+
func (vmr *Receiver) StopMetricsReception() error {
101109
vmr.mu.Lock()
102110
defer vmr.mu.Unlock()
103111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
receivers:
2+
vmmetrics:
3+
vmmetrics/customname:
4+
scrape_interval: 5s
5+
mount_point: /mountpoint
6+
process_mount_point: /proc
7+
metric_prefix: testmetric
8+
9+
processors:
10+
exampleprocessor:
11+
12+
exporters:
13+
exampleexporter:
14+
15+
pipelines:
16+
traces:
17+
receivers: [vmmetrics]
18+
processors: [exampleprocessor]
19+
exporters: [exampleexporter]

0 commit comments

Comments
 (0)