From dbfa430248aaa964f6f5344673f4ed6b39a7e46b Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Tue, 1 Feb 2022 15:28:07 -0500 Subject: [PATCH] cmd/otelopscol: remove error filter (#82) * cmd/otelopscol: remove error filter This was only useful when the upstream fix didn't exist. The upstream fixed is now much more desirable than this one. * go.mod: remove zapfilter and tidy --- cmd/otelopscol/filters.go | 83 -------------------------------- cmd/otelopscol/filters_test.go | 88 ---------------------------------- cmd/otelopscol/main.go | 11 +---- go.mod | 1 - go.sum | 3 -- 5 files changed, 2 insertions(+), 184 deletions(-) delete mode 100644 cmd/otelopscol/filters.go delete mode 100644 cmd/otelopscol/filters_test.go diff --git a/cmd/otelopscol/filters.go b/cmd/otelopscol/filters.go deleted file mode 100644 index dd15f606d..000000000 --- a/cmd/otelopscol/filters.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2021 Google LLC -// -// 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 main - -import ( - "strings" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "moul.io/zapfilter" -) - -type errorFilterConfig struct { - fileName string - errorSubstrings []string -} - -// Returns zap options that will filter logs with some error message from a file. -func errorFilterOptions() []zap.Option { - errorFilterConfigs := []errorFilterConfig{ - // Filter out a problematic upstream otel spam log from hostmetrics. - // Upstream issue: https://github.com/open-telemetry/opentelemetry-collector/issues/3004 - { - fileName: "scrapercontroller.go", - errorSubstrings: []string{"error reading process name for pid"}, - }, - } - - options := []zap.Option{} - for _, filter := range errorFilterConfigs { - options = append(options, makeErrorFilterOption(filter)) - } - - return options -} - -func makeErrorFilterOption(filterConfig errorFilterConfig) zap.Option { - logFilterFunc := func(entry zapcore.Entry, fields []zapcore.Field) bool { - if filterConfig.fileName != "" && - !strings.Contains(entry.Caller.File, filterConfig.fileName) { - return true - } - for _, field := range fields { - if field.Key == "error" { - logError, ok := field.Interface.(error) - if !ok { - return true - } - return !matchAny(logError.Error(), filterConfig.errorSubstrings) - } - } - return true - } - - return zap.WrapCore(func(core zapcore.Core) zapcore.Core { - return zapfilter.NewFilteringCore(core, logFilterFunc) - }) -} - -func matchAny(s string, subs []string) bool { - matches := make([]bool, len(subs)) - for i, sub := range subs { - matches[i] = strings.Contains(s, sub) - } - for _, match := range matches { - if match { - return true - } - } - return false -} diff --git a/cmd/otelopscol/filters_test.go b/cmd/otelopscol/filters_test.go deleted file mode 100644 index 47cfb081a..000000000 --- a/cmd/otelopscol/filters_test.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2021 Google LLC -// -// 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 main - -import ( - "errors" - "testing" - - "go.uber.org/zap" - "go.uber.org/zap/zaptest/observer" -) - -func TestErrorFilterOption(t *testing.T) { - testCases := []struct { - filterConfigs []errorFilterConfig - errors []error - expectedLogCount int - }{ - { - filterConfigs: []errorFilterConfig{ - { - errorSubstrings: []string{"error reading process name"}, - }, - }, - errors: []error{ - errors.New("error reading process name for pid 1"), - }, - expectedLogCount: 0, - }, - { - filterConfigs: []errorFilterConfig{ - { - errorSubstrings: []string{"error reading process name"}, - }, - }, - errors: []error{ - errors.New("error reading process name for pid 1"), - errors.New("error reading process name for pid 2"), - errors.New("error reading process name for pid 0"), - }, - expectedLogCount: 0, - }, - { - filterConfigs: []errorFilterConfig{ - { - errorSubstrings: []string{"error reading process name"}, - }, - }, - errors: []error{ - errors.New("error reading process name for pid 1"), - errors.New("unrelated error"), - }, - expectedLogCount: 1, - }, - } - - for _, testCase := range testCases { - logger, observedLogs := makeErrorFilterLogger(testCase.filterConfigs) - for _, err := range testCase.errors { - logger.Error("error", zap.Error(err)) - } - if len(observedLogs.All()) != testCase.expectedLogCount { - t.Fatalf("expected %d logs, got %d", testCase.expectedLogCount, len(observedLogs.All())) - } - } -} - -func makeErrorFilterLogger(filterConfigs []errorFilterConfig) (*zap.Logger, *observer.ObservedLogs) { - observedCore, observedLogs := observer.New(zap.InfoLevel) - options := []zap.Option{} - for _, filterConfig := range filterConfigs { - options = append(options, makeErrorFilterOption(filterConfig)) - } - logger := zap.New(observedCore, options...) - return logger, observedLogs -} diff --git a/cmd/otelopscol/main.go b/cmd/otelopscol/main.go index 114fa8c22..7b0086de2 100644 --- a/cmd/otelopscol/main.go +++ b/cmd/otelopscol/main.go @@ -20,7 +20,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/service" - "go.uber.org/zap" "github.com/GoogleCloudPlatform/opentelemetry-operations-collector/internal/env" "github.com/GoogleCloudPlatform/opentelemetry-operations-collector/internal/version" @@ -42,15 +41,9 @@ func main() { Version: version.Version, } - loggingOptions := append( - []zap.Option{}, - errorFilterOptions()..., - ) - params := service.CollectorSettings{ - Factories: factories, - BuildInfo: info, - LoggingOptions: loggingOptions, + Factories: factories, + BuildInfo: info, } if err := run(params); err != nil { diff --git a/go.mod b/go.mod index 88675228c..dc005e801 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,6 @@ require ( go.uber.org/multierr v1.7.0 go.uber.org/zap v1.20.0 golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 - moul.io/zapfilter v1.6.1 ) require ( diff --git a/go.sum b/go.sum index c2a71dd08..e8d32b3a1 100644 --- a/go.sum +++ b/go.sum @@ -1726,7 +1726,6 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.14.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= -go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI= go.uber.org/zap v1.20.0 h1:N4oPlghZwYG55MlU6LXk/Zp00FVNE9X9wrYO8CEs4lc= @@ -2485,8 +2484,6 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/ k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b h1:wxEMGetGMur3J1xuGLQY7GEQYg9bZxKn3tKo5k/eYcs= k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= -moul.io/zapfilter v1.6.1 h1:D5ySvqGTADgtOmjhKCrCOnH3DHZjIqIAQD0kwCeyBIw= -moul.io/zapfilter v1.6.1/go.mod h1:zfT2z4z2YfI63Kifpe8GMt0M+sBXs8WQ4mfne4bGmic= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=