forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement VMMetrics factory and config V2
Github issue: open-telemetry#35 Testing done: make && make otelsvc
- Loading branch information
Tigran Najaryan
committed
Jul 2, 2019
1 parent
4979bbd
commit 3df1252
Showing
7 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Prometheus 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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 config V2 for Prometheus receiver. | ||
|
||
var _ = receiver.RegisterReceiverFactory(&ReceiverFactory{}) | ||
|
||
const ( | ||
// The value of "type" key in configuration. | ||
typeStr = "vmmetrics" | ||
) | ||
|
||
// ReceiverFactory is the factory for receiver. | ||
type ReceiverFactory struct { | ||
} | ||
|
||
// Type gets the type of the Receiver config created by this factory. | ||
func (f *ReceiverFactory) Type() string { | ||
return typeStr | ||
} | ||
|
||
// CustomUnmarshaler returns custom unmarshaler for this config. | ||
func (f *ReceiverFactory) CustomUnmarshaler() receiver.CustomUnmarshaler { | ||
return nil | ||
} | ||
|
||
// CreateDefaultConfig creates the default configuration for receiver. | ||
func (f *ReceiverFactory) CreateDefaultConfig() models.Receiver { | ||
return &ConfigV2{ | ||
ReceiverSettings: models.ReceiverSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
} | ||
} | ||
|
||
// CreateTraceReceiver creates a trace receiver based on provided config. | ||
func (f *ReceiverFactory) CreateTraceReceiver( | ||
ctx context.Context, | ||
logger *zap.Logger, | ||
cfg models.Receiver, | ||
nextConsumer consumer.TraceConsumer, | ||
) (receiver.TraceReceiver, error) { | ||
// Prometheus does not support traces | ||
return nil, models.ErrDataTypeIsNotSupported | ||
} | ||
|
||
// CreateMetricsReceiver creates a metrics receiver based on provided config. | ||
func (f *ReceiverFactory) 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-service/models" | ||
"github.com/open-telemetry/opentelemetry-service/receiver" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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) | ||
|
||
// The default config does not provide scrape_config so we expect that metrics receiver | ||
// creation must also fail. | ||
mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, mReceiver) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |