Skip to content

Commit

Permalink
[receiver/windowsperfcounters] Returning partial errors for failures
Browse files Browse the repository at this point in the history
Returning partial errors for failure during scraping to prevent throwing
out all succesfully retrieved metrics.
  • Loading branch information
chrislbs committed Dec 5, 2022
1 parent 14da010 commit d8647aa
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package windowsperfcountersreceiver // import "github.com/open-telemetry/opentel

import (
"context"
"go.opentelemetry.io/collector/receiver/scrapererror"
"time"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -136,10 +137,12 @@ func (s *scraper) scrape(context.Context) (pmetric.Metrics, error) {
metrics[name] = builtMetric
}

scrapeFailures := 0
for _, watcher := range s.watchers {
counterVals, err := watcher.ScrapeData()
if err != nil {
errs = multierr.Append(errs, err)
scrapeFailures += 1
continue
}

Expand All @@ -157,6 +160,9 @@ func (s *scraper) scrape(context.Context) (pmetric.Metrics, error) {
initializeMetricDps(metric, now, val, watcher.MetricRep.Attributes)
}
}
if scrapeFailures != 0 && scrapeFailures != len(s.watchers) {
errs = scrapererror.NewPartialScrapeError(errs, scrapeFailures)
}
return md, errs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package windowsperfcountersreceiver

import (
"context"
"errors"
"fmt"
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.uber.org/multierr"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -64,6 +68,19 @@ func mockPerfCounterFactory(mpc mockPerfCounter) newWatcherFunc {
}
}

func mockPerfCounterFactoryInvocations(mpcs ...mockPerfCounter) newWatcherFunc {
invocationNum := 0
return func(string, string, string) (winperfcounters.PerfCounterWatcher, error) {
if invocationNum == len(mpcs) {
return nil, fmt.Errorf("invoked watcher %d times but only %d were setup", invocationNum+1, len(mpcs))
}
mpc := mpcs[invocationNum]
invocationNum += 1

return &mpc, nil
}
}

func Test_WindowsPerfCounterScraper(t *testing.T) {
type testCase struct {
name string
Expand Down Expand Up @@ -286,9 +303,9 @@ func TestInitWatchers(t *testing.T) {

func TestScrape(t *testing.T) {
testCases := []struct {
name string
cfg Config
mockCounterValues []winperfcounters.CounterValue
name string
cfg Config
mockPerfCounters []mockPerfCounter
}{
{
name: "metricsWithoutInstance",
Expand Down Expand Up @@ -317,7 +334,10 @@ func TestScrape(t *testing.T) {
"metric2": {Description: "metric2 description", Unit: "2"},
},
},
mockCounterValues: []winperfcounters.CounterValue{{Value: 1.0}},
mockPerfCounters: []mockPerfCounter{
{counterValues: []winperfcounters.CounterValue{{Value: 1.0}}},
{counterValues: []winperfcounters.CounterValue{{Value: 2.0}}},
},
},
{
name: "metricsWithInstance",
Expand Down Expand Up @@ -346,18 +366,74 @@ func TestScrape(t *testing.T) {
"metric2": {Description: "metric2 description", Unit: "2"},
},
},
mockCounterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 1.0}},
mockPerfCounters: []mockPerfCounter{
{counterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 1.0}}},
{counterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 2.0}}},
},
},
{
name: "metricsWithSingleCounterFailure",
cfg: Config{
PerfCounters: []ObjectConfig{
{
Counters: []CounterConfig{
{
MetricRep: MetricRep{
Name: "metric1",
},
},
{
MetricRep: MetricRep{
Name: "metric2",
Attributes: map[string]string{
"test.attribute": "test-value",
},
},
},
{
MetricRep: MetricRep{
Name: "metric3",
},
},
},
},
},
MetricMetaData: map[string]MetricConfig{
"metric1": {Description: "metric1 description", Unit: "1"},
"metric2": {Description: "metric2 description", Unit: "2"},
"metric3": {Description: "metric3 description", Unit: "3"},
},
},
mockPerfCounters: []mockPerfCounter{
{counterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 1.0}}},
{scrapeErr: errors.New("unable to scrape metric 2")},
{scrapeErr: errors.New("unable to scrape metric 3")},
},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
mpc := mockPerfCounter{counterValues: test.mockCounterValues}
s := &scraper{cfg: &test.cfg, newWatcher: mockPerfCounterFactory(mpc)}
s := &scraper{cfg: &test.cfg, newWatcher: mockPerfCounterFactoryInvocations(test.mockPerfCounters...)}
errs := s.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, errs)

var expectedErrors []error
for _, mpc := range test.mockPerfCounters {
if mpc.scrapeErr != nil {
expectedErrors = append(expectedErrors, mpc.scrapeErr)
}
}

m, err := s.scrape(context.Background())
require.NoError(t, err)
if len(expectedErrors) != 0 {
require.IsType(t, scrapererror.PartialScrapeError{}, err)
partialErr := err.(scrapererror.PartialScrapeError)
require.Equal(t, len(expectedErrors), partialErr.Failed)
expectedError := multierr.Combine(expectedErrors...)
require.Equal(t, expectedError.Error(), partialErr.Error())
} else {
require.NoError(t, err)
}
require.Equal(t, 1, m.ResourceMetrics().Len())
require.Equal(t, 1, m.ResourceMetrics().At(0).ScopeMetrics().Len())

Expand All @@ -367,15 +443,18 @@ func TestScrape(t *testing.T) {
})
curMetricsNum := 0
for _, pc := range test.cfg.PerfCounters {
for _, counterCfg := range pc.Counters {

for counterIdx, counterCfg := range pc.Counters {
metric := metrics.At(curMetricsNum)
assert.Equal(t, counterCfg.MetricRep.Name, metric.Name())
metricData := test.cfg.MetricMetaData[counterCfg.MetricRep.Name]
assert.Equal(t, metricData.Description, metric.Description())
assert.Equal(t, metricData.Unit, metric.Unit())
dps := metric.Gauge().DataPoints()
assert.Equal(t, len(test.mockCounterValues), dps.Len())
for dpIdx, val := range test.mockCounterValues {

counterValues := test.mockPerfCounters[counterIdx].counterValues
assert.Equal(t, len(counterValues), dps.Len())
for dpIdx, val := range counterValues {
assert.Equal(t, val.Value, dps.At(dpIdx).DoubleValue())
expectedAttributeLen := len(counterCfg.MetricRep.Attributes)
if val.InstanceName != "" {
Expand Down

0 comments on commit d8647aa

Please sign in to comment.