Skip to content

Commit

Permalink
Add observability report wrapper for scrapers and use it everywhere (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#1276)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored and wyTrivail committed Jul 13, 2020
1 parent af91be6 commit 741900a
Show file tree
Hide file tree
Showing 25 changed files with 357 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"time"

"go.opencensus.io/trace"
"go.uber.org/zap"

"go.opentelemetry.io/collector/consumer/pdata"
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"time"

"github.com/shirou/gopsutil/mem"
"go.opencensus.io/trace"

"go.opentelemetry.io/collector/consumer/pdata"
)
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 741900a

Please sign in to comment.