From 3ae329c88679012be866209d32277b35e6ac5034 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Tue, 7 Jul 2020 10:21:58 -0700 Subject: [PATCH] Add observability report wrapper for scrapers and use it everywhere Signed-off-by: Bogdan Drutu --- .../scraper/cpuscraper/cpu_scraper.go | 6 +- .../internal/scraper/cpuscraper/factory.go | 3 +- .../scraper/diskscraper/disk_scraper.go | 6 +- .../internal/scraper/diskscraper/factory.go | 3 +- .../scraper/filesystemscraper/factory.go | 3 +- .../filesystemscraper/filesystem_scraper.go | 10 +-- .../internal/scraper/loadscraper/factory.go | 3 +- .../scraper/loadscraper/load_scraper.go | 6 +- .../internal/scraper/memoryscraper/factory.go | 3 +- .../scraper/memoryscraper/memory_scraper.go | 6 +- .../scraper/networkscraper/factory.go | 3 +- .../scraper/networkscraper/network_scraper.go | 10 +-- .../scraper/obsreportscraper/common.go | 19 +++++ .../scraper/obsreportscraper/common_test.go | 39 ++++++++++ .../obsreportresourcescraper.go | 56 ++++++++++++++ .../obsreportresourcescraper_test.go | 76 +++++++++++++++++++ .../obsreportscraper/obsreportscraper.go | 56 ++++++++++++++ .../obsreportscraper/obsreportscraper_test.go | 76 +++++++++++++++++++ .../scraper/processesscraper/factory.go | 3 +- .../processesscraper/processes_scraper.go | 6 +- .../scraper/processscraper/factory.go | 7 +- .../scraper/processscraper/process_scraper.go | 6 +- .../scraper/virtualmemoryscraper/factory.go | 5 +- .../virtualmemory_scraper_others.go | 6 +- .../virtualmemory_scraper_windows.go | 7 +- 25 files changed, 357 insertions(+), 67 deletions(-) create mode 100644 receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common.go create mode 100644 receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common_test.go create mode 100644 receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper.go create mode 100644 receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper_test.go create mode 100644 receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper.go create mode 100644 receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper_test.go diff --git a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go index 9f6d961dcab..438b5c6e745 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go @@ -20,7 +20,6 @@ import ( "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/host" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/consumer/pdata" ) @@ -53,10 +52,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "cpuscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() cpuTimes, err := cpu.Times(s.config.ReportPerCPU) diff --git a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/factory.go index 2956f540dcd..5daa4ea2f9b 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for CPU scraper. @@ -45,5 +46,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newCPUScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newCPUScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper.go index 7aa7b0779e5..a8c33998c85 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper.go @@ -20,7 +20,6 @@ import ( "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/host" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/consumer/pdata" ) @@ -53,10 +52,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "diskscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() ioCounters, err := disk.IOCounters() diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/factory.go index 2c531dee580..f6d152e52bb 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for Disk scraper. @@ -45,5 +46,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newDiskScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newDiskScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory.go index 480a5f331ba..bdb7586aa0c 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for FileSystem scraper. @@ -50,5 +51,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newFileSystemScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newFileSystemScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go index 08ff50d43d1..ccfe0980882 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go @@ -19,7 +19,6 @@ import ( "time" "github.com/shirou/gopsutil/disk" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer/pdata" @@ -51,16 +50,11 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "filesystemscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() // omit logical (virtual) filesystems (not relevant for windows) - all := false - - partitions, err := disk.Partitions(all) + partitions, err := disk.Partitions( /*all=*/ false) if err != nil { return metrics, err } diff --git a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/factory.go index 04b1f666715..c2af833b564 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for Load scraper. @@ -45,5 +46,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newLoadScraper(ctx, logger, cfg), nil + return obsreportscraper.WrapScraper(newLoadScraper(ctx, logger, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/load_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/load_scraper.go index e3c7dece2a4..aedc59185b0 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/load_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/load_scraper.go @@ -18,7 +18,6 @@ import ( "context" "time" - "go.opencensus.io/trace" "go.uber.org/zap" "go.opentelemetry.io/collector/consumer/pdata" @@ -46,10 +45,7 @@ func (s *scraper) Close(ctx context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "loadscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() avgLoadValues, err := getSampledLoadAverages() diff --git a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/factory.go index 9d2b0f42a6c..a488a133b05 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for Memory scraper. @@ -45,5 +46,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newMemoryScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newMemoryScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/memory_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/memory_scraper.go index c59fc92f353..5604a26c447 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/memory_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/memory_scraper.go @@ -19,7 +19,6 @@ import ( "time" "github.com/shirou/gopsutil/mem" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/consumer/pdata" ) @@ -45,10 +44,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "memoryscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() memInfo, err := mem.VirtualMemory() diff --git a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/factory.go index 558769e87d5..4e588e39d05 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for Network scraper. @@ -45,5 +46,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newNetworkScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newNetworkScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go index 3a66e68d76d..b7080e44586 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go @@ -21,7 +21,6 @@ import ( "github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/net" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer/pdata" @@ -55,10 +54,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "networkscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() var errors []error @@ -82,9 +78,7 @@ func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) func scrapeAndAppendNetworkCounterMetrics(metrics pdata.MetricSlice, startTime pdata.TimestampUnixNano) error { // get total stats only - perNetworkInterfaceController := false - - networkStatsSlice, err := net.IOCounters(perNetworkInterfaceController) + networkStatsSlice, err := net.IOCounters( /*perNetworkInterfaceController=*/ false) if err != nil { return err } diff --git a/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common.go b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common.go new file mode 100644 index 00000000000..ed65f18bf5c --- /dev/null +++ b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common.go @@ -0,0 +1,19 @@ +// Copyright The 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 obsreportscraper + +func scrapeMetricsSpanName(typeStr string) string { + return typeStr + "scraper.ScrapeMetrics" +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common_test.go b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common_test.go new file mode 100644 index 00000000000..9c58e94a627 --- /dev/null +++ b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/common_test.go @@ -0,0 +1,39 @@ +// Copyright The 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 obsreportscraper + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSpanName(t *testing.T) { + scraperTypes := map[string]string{ + "cpu": "cpuscraper.ScrapeMetrics", + "disk": "diskscraper.ScrapeMetrics", + "load": "loadscraper.ScrapeMetrics", + "filesystem": "filesystemscraper.ScrapeMetrics", + "memory": "memoryscraper.ScrapeMetrics", + "network": "networkscraper.ScrapeMetrics", + "processes": "processesscraper.ScrapeMetrics", + "virtualmemory": "virtualmemoryscraper.ScrapeMetrics", + "process": "processscraper.ScrapeMetrics", + } + + for typeStr, spanName := range scraperTypes { + assert.Equal(t, spanName, scrapeMetricsSpanName(typeStr)) + } +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper.go b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper.go new file mode 100644 index 00000000000..6023da06026 --- /dev/null +++ b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper.go @@ -0,0 +1,56 @@ +// Copyright The 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 obsreportscraper + +import ( + "context" + + "go.opencensus.io/trace" + + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" +) + +type resourceScraper struct { + delegate internal.ResourceScraper + scrapeMetricsSpanName string +} + +// WrapResourceScraper wraps an internal.ResourceScraper and provides observability support. +func WrapResourceScraper(delegate internal.ResourceScraper, typeStr string) internal.ResourceScraper { + return &resourceScraper{delegate: delegate, scrapeMetricsSpanName: scrapeMetricsSpanName(typeStr)} +} + +func (s *resourceScraper) Initialize(ctx context.Context) error { + return s.delegate.Initialize(ctx) +} + +func (s *resourceScraper) Close(ctx context.Context) error { + return s.delegate.Close(ctx) +} + +// ScrapeMetrics +func (s *resourceScraper) ScrapeMetrics(ctx context.Context) (pdata.ResourceMetricsSlice, error) { + // TODO: Add metrics. + ctx, span := trace.StartSpan(ctx, s.scrapeMetricsSpanName) + defer span.End() + + rms, err := s.delegate.ScrapeMetrics(ctx) + + if err != nil { + span.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}) + } + return rms, err +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper_test.go new file mode 100644 index 00000000000..2feca6e1e1b --- /dev/null +++ b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportresourcescraper_test.go @@ -0,0 +1,76 @@ +// Copyright The 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 obsreportscraper + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "go.opencensus.io/trace" + + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal/data/testdata" +) + +func TestWrapResourceScraper(t *testing.T) { + ts := &testResourceScraper{ + t: t, + err: nil, + } + obss := WrapResourceScraper(ts, "test") + assert.NoError(t, obss.Initialize(context.Background())) + rms, err := obss.ScrapeMetrics(context.Background()) + assert.NoError(t, err) + assert.EqualValues(t, generateResourceMetricsSlice(), rms) + assert.NoError(t, obss.Close(context.Background())) +} + +func TestWrapResourceScraper_Error(t *testing.T) { + ts := &testResourceScraper{ + t: t, + err: fmt.Errorf("my error"), + } + obss := WrapResourceScraper(ts, "test") + assert.Error(t, obss.Initialize(context.Background())) + rms, err := obss.ScrapeMetrics(context.Background()) + assert.Error(t, err) + assert.EqualValues(t, generateResourceMetricsSlice(), rms) + assert.Error(t, obss.Close(context.Background())) +} + +type testResourceScraper struct { + t *testing.T + err error +} + +func (s *testResourceScraper) Initialize(_ context.Context) error { + return s.err +} + +func (s *testResourceScraper) Close(_ context.Context) error { + return s.err +} + +// ScrapeMetrics +func (s *testResourceScraper) ScrapeMetrics(ctx context.Context) (pdata.ResourceMetricsSlice, error) { + assert.NotNil(s.t, trace.FromContext(ctx)) + return generateResourceMetricsSlice(), s.err +} + +func generateResourceMetricsSlice() pdata.ResourceMetricsSlice { + return testdata.GenerateMetricDataOneMetric().ResourceMetrics() +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper.go b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper.go new file mode 100644 index 00000000000..08e943a1cdd --- /dev/null +++ b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper.go @@ -0,0 +1,56 @@ +// Copyright The 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 obsreportscraper + +import ( + "context" + + "go.opencensus.io/trace" + + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" +) + +type scraper struct { + delegate internal.Scraper + scrapeMetricsSpanName string +} + +// WrapScraper wraps an internal.Scraper and provides observability support. +func WrapScraper(delegate internal.Scraper, typeStr string) internal.Scraper { + return &scraper{delegate: delegate, scrapeMetricsSpanName: scrapeMetricsSpanName(typeStr)} +} + +func (s *scraper) Initialize(ctx context.Context) error { + return s.delegate.Initialize(ctx) +} + +func (s *scraper) Close(ctx context.Context) error { + return s.delegate.Close(ctx) +} + +// ScrapeMetrics +func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { + // TODO: Add metrics. + ctx, span := trace.StartSpan(ctx, s.scrapeMetricsSpanName) + defer span.End() + + ms, err := s.delegate.ScrapeMetrics(ctx) + + if err != nil { + span.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}) + } + return ms, err +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper_test.go new file mode 100644 index 00000000000..ebd13db05fc --- /dev/null +++ b/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper/obsreportscraper_test.go @@ -0,0 +1,76 @@ +// Copyright The 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 obsreportscraper + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "go.opencensus.io/trace" + + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal/data/testdata" +) + +func TestWrapScraper(t *testing.T) { + ts := &testScraper{ + t: t, + err: nil, + } + obss := WrapScraper(ts, "test") + assert.NoError(t, obss.Initialize(context.Background())) + ms, err := obss.ScrapeMetrics(context.Background()) + assert.NoError(t, err) + assert.EqualValues(t, generateMetricsSlice(), ms) + assert.NoError(t, obss.Close(context.Background())) +} + +func TestWrapScraper_Error(t *testing.T) { + ts := &testScraper{ + t: t, + err: fmt.Errorf("my error"), + } + obss := WrapScraper(ts, "test") + assert.Error(t, obss.Initialize(context.Background())) + ms, err := obss.ScrapeMetrics(context.Background()) + assert.Error(t, err) + assert.EqualValues(t, generateMetricsSlice(), ms) + assert.Error(t, obss.Close(context.Background())) +} + +type testScraper struct { + t *testing.T + err error +} + +func (s *testScraper) Initialize(_ context.Context) error { + return s.err +} + +func (s *testScraper) Close(_ context.Context) error { + return s.err +} + +// ScrapeMetrics +func (s *testScraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { + assert.NotNil(s.t, trace.FromContext(ctx)) + return generateMetricsSlice(), s.err +} + +func generateMetricsSlice() pdata.MetricSlice { + return testdata.GenerateMetricDataOneMetric().ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics() +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/factory.go index f719ef01f0b..634c2fb6ecd 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for Processes scraper. @@ -45,5 +46,5 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newProcessesScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newProcessesScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper.go index 07cf368df8b..a5891119475 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper.go @@ -19,7 +19,6 @@ import ( "github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/load" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/consumer/pdata" ) @@ -57,10 +56,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "processesscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() err := appendSystemSpecificProcessesMetrics(metrics, 0, s.misc) return metrics, err diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/factory.go index e09b6f9e5f0..343a079f209 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for Process scraper. @@ -45,5 +46,9 @@ func (f *Factory) CreateMetricsScraper( config internal.Config, ) (internal.ResourceScraper, error) { cfg := config.(*Config) - return newProcessScraper(cfg) + ps, err := newProcessScraper(cfg) + if err != nil { + return nil, err + } + return obsreportscraper.WrapResourceScraper(ps, TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go index 13d23bd3b14..451f56c48f9 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go @@ -22,7 +22,6 @@ import ( "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/process" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer/pdata" @@ -79,10 +78,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.ResourceMetricsSlice, error) { - _, span := trace.StartSpan(ctx, "processscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.ResourceMetricsSlice, error) { var errs []error metadata, err := s.getProcessMetadata() diff --git a/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/factory.go b/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/factory.go index 952f5aa6bca..92bc1a0aa6b 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/factory.go +++ b/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/factory.go @@ -20,6 +20,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" + "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/obsreportscraper" ) // This file implements Factory for VirtualMemory scraper. @@ -41,9 +42,9 @@ func (f *Factory) CreateDefaultConfig() internal.Config { // CreateMetricsScraper creates a scraper based on provided config. func (f *Factory) CreateMetricsScraper( ctx context.Context, - logger *zap.Logger, + _ *zap.Logger, config internal.Config, ) (internal.Scraper, error) { cfg := config.(*Config) - return newVirtualMemoryScraper(ctx, cfg), nil + return obsreportscraper.WrapScraper(newVirtualMemoryScraper(ctx, cfg), TypeStr), nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_others.go b/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_others.go index 8bfdfd501f6..433f5ef4b6e 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_others.go +++ b/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_others.go @@ -22,7 +22,6 @@ import ( "github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/mem" - "go.opencensus.io/trace" "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer/pdata" @@ -56,10 +55,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "virtualmemoryscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() var errors []error diff --git a/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_windows.go b/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_windows.go index 24d9bb757a9..c8b2ed8d765 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_windows.go +++ b/receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/virtualmemory_scraper_windows.go @@ -21,8 +21,6 @@ import ( "math" "time" - "go.opencensus.io/trace" - "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/windows/pdh" @@ -89,10 +87,7 @@ func (s *scraper) Close(_ context.Context) error { } // ScrapeMetrics -func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { - _, span := trace.StartSpan(ctx, "virtualmemoryscraper.ScrapeMetrics") - defer span.End() - +func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { metrics := pdata.NewMetricSlice() var errors []error