Skip to content

Commit 229d228

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

File tree

7 files changed

+254
-4
lines changed

7 files changed

+254
-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 VMMetrics 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 factory for VMMetrics receiver.
28+
29+
var _ = receiver.RegisterReceiverFactory(&Factory{})
30+
31+
const (
32+
// The value of "type" key in configuration.
33+
typeStr = "vmmetrics"
34+
)
35+
36+
// Factory is the factory for receiver.
37+
type Factory struct {
38+
}
39+
40+
// Type gets the type of the Receiver config created by this factory.
41+
func (f *Factory) Type() string {
42+
return typeStr
43+
}
44+
45+
// CustomUnmarshaler returns custom unmarshaler for this config.
46+
func (f *Factory) CustomUnmarshaler() receiver.CustomUnmarshaler {
47+
return nil
48+
}
49+
50+
// CreateDefaultConfig creates the default configuration for receiver.
51+
func (f *Factory) 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 *Factory) CreateTraceReceiver(
62+
ctx context.Context,
63+
logger *zap.Logger,
64+
cfg models.Receiver,
65+
nextConsumer consumer.TraceConsumer,
66+
) (receiver.TraceReceiver, error) {
67+
// VMMetrics does not support traces
68+
return nil, models.ErrDataTypeIsNotSupported
69+
}
70+
71+
// CreateMetricsReceiver creates a metrics receiver based on provided config.
72+
func (f *Factory) 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+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
"github.com/stretchr/testify/assert"
22+
"go.uber.org/zap"
23+
24+
"github.com/open-telemetry/opentelemetry-service/models"
25+
"github.com/open-telemetry/opentelemetry-service/receiver"
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+
mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil)
43+
assert.Nil(t, err)
44+
assert.NotNil(t, mReceiver)
45+
}

receiver/vmmetricsreceiver/metrics_receiver.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package vmmetricsreceiver
1616

1717
import (
18-
"context"
1918
"errors"
2019
"fmt"
2120
"runtime"
@@ -25,6 +24,7 @@ import (
2524
"github.com/spf13/viper"
2625

2726
"github.com/open-telemetry/opentelemetry-service/consumer"
27+
"github.com/open-telemetry/opentelemetry-service/receiver"
2828
)
2929

3030
var (
@@ -76,8 +76,15 @@ func New(v *viper.Viper, consumer consumer.MetricsConsumer) (*Receiver, error) {
7676
return vmr, nil
7777
}
7878

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

@@ -97,7 +104,7 @@ func (vmr *Receiver) StartMetricsReception(ctx context.Context, asyncErrorChan c
97104
}
98105

99106
// StopMetricsReception stops and cancels the underlying VM metrics scrapers.
100-
func (vmr *Receiver) StopMetricsReception(ctx context.Context) error {
107+
func (vmr *Receiver) StopMetricsReception() error {
101108
vmr.mu.Lock()
102109
defer vmr.mu.Unlock()
103110

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)