From afa02aceee8fed95ae16d817af94eda4f292f058 Mon Sep 17 00:00:00 2001 From: Manabu Sonoda Date: Thu, 11 Feb 2021 13:35:04 +0900 Subject: [PATCH] supported go1.13 error wrap --- config.go | 6 +- dnstap_fluentd_output.go | 7 +- dnstap_fstrm_file_input.go | 10 +- dnstap_fstrm_file_output.go | 6 +- dnstap_fstrm_input.go | 7 +- dnstap_fstrm_socket_input.go | 6 +- dnstap_fstrm_socket_output.go | 4 +- dnstap_fstrm_tail_input.go | 8 +- dnstap_fstrm_tcp_socket_input.go | 5 +- dnstap_fstrm_tcp_socket_output.go | 7 +- dnstap_fstrm_unix_socket_input.go | 14 ++- dnstap_fstrm_unix_socket_output.go | 9 +- dnstap_kafka_output.go | 8 +- dnstap_nats_output.go | 4 +- example/kafka/dnstap_kafka_output copy.go | 134 ---------------------- flat.go | 3 +- 16 files changed, 50 insertions(+), 188 deletions(-) delete mode 100644 example/kafka/dnstap_kafka_output copy.go diff --git a/config.go b/config.go index f4337f5..7be199a 100644 --- a/config.go +++ b/config.go @@ -224,10 +224,10 @@ func NewConfigFromReader(r io.Reader) (*Config, error) { v.SetConfigType("toml") v.SetDefault("InputMsgBuffer", 10000) if err := v.ReadConfig(r); err != nil { - return nil, errors.Wrap(err, "can't read config") + return nil, fmt.Errorf("failed to read config: %w", err) } if err := v.Unmarshal(c); err != nil { - return nil, errors.Wrap(err, "can't parse config") + return nil, fmt.Errorf("failed to parse config: %w", err) } return c, nil } @@ -592,7 +592,7 @@ func (o *OutputStdoutConfig) Validate() error { } t, err := template.New("stdout").Parse(o.TemplateStr) if err != nil { - valerr.Add(errors.Wrap(err, "Template parse error")) + valerr.Add(fmt.Errorf("Template parse error: %w", err)) } o.template = t default: diff --git a/dnstap_fluentd_output.go b/dnstap_fluentd_output.go index 7e63630..5ac7026 100644 --- a/dnstap_fluentd_output.go +++ b/dnstap_fluentd_output.go @@ -17,9 +17,10 @@ package dtap import ( + "fmt" + dnstap "github.com/dnstap/golang-dnstap" framestream "github.com/farsightsec/golang-framestream" - "github.com/pkg/errors" "github.com/fluent/fluent-logger-golang/fluent" "github.com/golang/protobuf/proto" @@ -52,7 +53,7 @@ func (o *DnstapFluentdOutput) open() error { var err error o.client, err = fluent.New(o.fluetConfig) if err != nil { - return errors.Wrapf(err, "can't create fluent logger") + return fmt.Errorf("failed to create fluent logger: %w", err) } return nil @@ -68,7 +69,7 @@ func (o *DnstapFluentdOutput) write(frame []byte) error { return err } if err := o.client.Post(o.tag, *data); err != nil { - return errors.Wrapf(err, "failed to post fluent message, tag: %s", o.tag) + return fmt.Errorf("failed to post fluent message, tag: %s %w", o.tag, err) } return nil } diff --git a/dnstap_fstrm_file_input.go b/dnstap_fstrm_file_input.go index a39ca42..764b623 100644 --- a/dnstap_fstrm_file_input.go +++ b/dnstap_fstrm_file_input.go @@ -20,11 +20,11 @@ import ( "compress/bzip2" "compress/gzip" "context" + "fmt" "io" "os" "strings" - "github.com/pkg/errors" "github.com/ulikunitz/xz" ) @@ -55,13 +55,13 @@ func NewDnstapFstrmFileInput(config *InputFileConfig) (*DnstapFstrmFileInput, er var r io.ReadCloser f, err := os.Open(config.GetPath()) if err != nil { - return nil, errors.Wrapf(err, "watch failed, path: %s", config.GetPath()) + return nil, fmt.Errorf("failed to watci file, path: %s err: %w", config.GetPath(), err) } if strings.HasSuffix(config.GetPath(), "gz") { r, err = gzip.NewReader(f) if err != nil { - return nil, errors.Wrapf(err, "failed to create gzip reader, path: %s", config.GetPath()) + return nil, fmt.Errorf("failed to create gzip reader, path: %s err: %w", config.GetPath(), err) } } else if strings.HasSuffix(config.GetPath(), "bz2") { cmp := bzip2.NewReader(f) @@ -70,14 +70,14 @@ func NewDnstapFstrmFileInput(config *InputFileConfig) (*DnstapFstrmFileInput, er cmp, err := xz.NewReader(f) r = NewDnstapFstrmFileReadCloser(cmp, f) if err != nil { - return nil, errors.Wrapf(err, "failed to create xz reader, path: %s", config.GetPath()) + return nil, fmt.Errorf("failed to create xz reader, path: %s err: %w", config.GetPath(), err) } } else { r = f } input, err := NewDnstapFstrmInput(r, false) if err != nil { - return nil, errors.Wrapf(err, "failed to create fstrm input, path: %s", config.GetPath()) + return nil, fmt.Errorf("failed to create fstrm input, path: %s err: %w", config.GetPath(), err) } i := &DnstapFstrmFileInput{ diff --git a/dnstap_fstrm_file_output.go b/dnstap_fstrm_file_output.go index 48e03d0..b8d8e2e 100644 --- a/dnstap_fstrm_file_output.go +++ b/dnstap_fstrm_file_output.go @@ -17,6 +17,7 @@ package dtap import ( + "fmt" "io" "os" "time" @@ -24,7 +25,6 @@ import ( dnstap "github.com/dnstap/golang-dnstap" framestream "github.com/farsightsec/golang-framestream" strftime "github.com/jehiah/go-strftime" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -49,13 +49,13 @@ func (o *DnstapFstrmFileOutput) open() error { f, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { - return errors.Wrapf(err, "can't create file %s", filename) + return fmt.Errorf("failed to create file %s err: %w", filename, err) } o.writer = f o.enc, err = framestream.NewEncoder(o.writer, &framestream.EncoderOptions{ContentType: dnstap.FSContentType, Bidirectional: false}) if err != nil { - return errors.Wrapf(err, "can't create framestream encorder %s", filename) + return fmt.Errorf("failed to create framestream encorder %s err: %w", filename, err) } o.currentFilename = filename o.opened = make(chan bool) diff --git a/dnstap_fstrm_input.go b/dnstap_fstrm_input.go index 0ee73c4..31d8fa2 100644 --- a/dnstap_fstrm_input.go +++ b/dnstap_fstrm_input.go @@ -18,10 +18,9 @@ package dtap import ( "context" + "fmt" "io" - "github.com/pkg/errors" - dnstap "github.com/dnstap/golang-dnstap" framestream "github.com/farsightsec/golang-framestream" @@ -40,7 +39,7 @@ func NewDnstapFstrmInput(rc io.ReadCloser, bi bool) (*DnstapFstrmInput, error) { Bidirectional: bi, }) if err != nil { - return nil, errors.Wrapf(err, "can't create framestream Decoder") + return nil, fmt.Errorf("failed to create framestream Decoder: %w", err) } return &DnstapFstrmInput{ rc: rc, @@ -56,7 +55,7 @@ func (i *DnstapFstrmInput) read(rbuf *RBuf) { i.readError <- nil return } - i.readError <- errors.Wrap(err, "decode error") + i.readError <- fmt.Errorf("decode error: %w", err) return } newbuf := make([]byte, len(buf)) diff --git a/dnstap_fstrm_socket_input.go b/dnstap_fstrm_socket_input.go index 3b3e5af..7328218 100644 --- a/dnstap_fstrm_socket_input.go +++ b/dnstap_fstrm_socket_input.go @@ -18,10 +18,10 @@ package dtap import ( "context" + "fmt" "net" "strings" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -49,12 +49,12 @@ func (i *DnstapFstrmSocketInput) runRead(ctx context.Context, rbuf *RBuf) { i.readError <- nil return } - i.readError <- errors.Wrapf(err, "can't accept socket") + i.readError <- fmt.Errorf("failed to accept socket", err) return } input, err := NewDnstapFstrmInput(conn, true) if err != nil { - log.Debugf("can't create NewDnstapFstrmInput: %s", err) + log.Debugf("failed to create NewDnstapFstrmInput: %s", err) continue } childCtx, _ := context.WithCancel(readCtx) diff --git a/dnstap_fstrm_socket_output.go b/dnstap_fstrm_socket_output.go index 2cd259a..925a608 100644 --- a/dnstap_fstrm_socket_output.go +++ b/dnstap_fstrm_socket_output.go @@ -17,10 +17,10 @@ package dtap import ( + "fmt" "time" framestream "github.com/farsightsec/golang-framestream" - "github.com/pkg/errors" ) type DnstapFstrmSocketOutput struct { @@ -39,7 +39,7 @@ func NewDnstapFstrmSocketOutput(handler SocketOutput, params *DnstapOutputParams func (o *DnstapFstrmSocketOutput) open() error { var err error if o.enc, err = o.handler.newConnect(); err != nil { - return errors.Wrapf(err, "can't connect socket") + return fmt.Errorf("failed to connect socket: %w") } o.opened = make(chan bool) go func() { diff --git a/dnstap_fstrm_tail_input.go b/dnstap_fstrm_tail_input.go index df5e443..c32681b 100644 --- a/dnstap_fstrm_tail_input.go +++ b/dnstap_fstrm_tail_input.go @@ -18,11 +18,11 @@ package dtap import ( "context" + "fmt" "os" "path/filepath" "time" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/fsnotify/fsnotify" @@ -50,7 +50,7 @@ func (i *DnstapFstrmTailInput) runSearchPath(ctx context.Context) error { for { matches, err := filepath.Glob(i.config.GetPath()) if err != nil { - return errors.Wrapf(err, "search file error, path: %s", i.config.GetPath()) + return fmt.Errorf("search file error, path: %s err: %w", i.config.GetPath(), err) } else { for _, filename := range matches { if _, ok := i.readers[filename]; ok != false { @@ -76,11 +76,11 @@ func (i *DnstapFstrmTailInput) runReadFile(ctx context.Context, filename string, modify := make(chan bool) f, err := os.Open(filename) if err != nil { - return errors.Wrapf(err, "can't open file, path: %s", filename) + return fmt.Errorf("failed to open file, path: %s err: %w", filename, err) } watcher, err := fsnotify.NewWatcher() if err != nil { - return errors.Wrapf(err, "can't create file watcher") + return fmt.Errorf("failed to create file watcher: %w", err) } defer watcher.Close() watcher.Add(filename) diff --git a/dnstap_fstrm_tcp_socket_input.go b/dnstap_fstrm_tcp_socket_input.go index 637f0fe..6f75b40 100644 --- a/dnstap_fstrm_tcp_socket_input.go +++ b/dnstap_fstrm_tcp_socket_input.go @@ -17,15 +17,14 @@ package dtap import ( + "fmt" "net" - - "github.com/pkg/errors" ) func NewDnstapFstrmTCPSocketInput(config *InputTCPSocketConfig) (*DnstapFstrmSocketInput, error) { l, err := net.Listen("tcp", config.GetNet()) if err != nil { - return nil, errors.Wrapf(err, "can't listen %s", config.GetNet()) + return nil, fmt.Errorf("failed to listen %s err: %w", config.GetNet(), err) } return NewDnstapFstrmSocketInput(l) } diff --git a/dnstap_fstrm_tcp_socket_output.go b/dnstap_fstrm_tcp_socket_output.go index 145169c..76bbccd 100644 --- a/dnstap_fstrm_tcp_socket_output.go +++ b/dnstap_fstrm_tcp_socket_output.go @@ -17,11 +17,11 @@ package dtap import ( + "fmt" "net" dnstap "github.com/dnstap/golang-dnstap" framestream "github.com/farsightsec/golang-framestream" - "github.com/pkg/errors" ) type DnstapFstrmTCPSocketOutput struct { @@ -37,12 +37,11 @@ func (o *DnstapFstrmTCPSocketOutput) newConnect() (*framestream.Encoder, error) w, err := net.Dial("tcp", o.config.GetAddress()) if err != nil { - return nil, errors.Wrapf(err, "can't connect tcp socket, address: %s", o.config.GetAddress()) + return nil, fmt.Errorf("failed to connect tcp socket, address: %s err: %w", o.config.GetAddress(), err) } enc, err := framestream.NewEncoder(w, &framestream.EncoderOptions{ContentType: dnstap.FSContentType, Bidirectional: true}) if err != nil { - - return nil, errors.Wrapf(err, "can't create fstrm encorder, address: %s", o.config.GetAddress()) + return nil, fmt.Errorf("failed to create fstrm encorder, address: %s err: %w", o.config.GetAddress(), err) } return enc, nil } diff --git a/dnstap_fstrm_unix_socket_input.go b/dnstap_fstrm_unix_socket_input.go index 57af790..0c1340b 100644 --- a/dnstap_fstrm_unix_socket_input.go +++ b/dnstap_fstrm_unix_socket_input.go @@ -17,31 +17,33 @@ package dtap import ( + "fmt" "net" "os" "os/user" "strconv" - - "github.com/pkg/errors" ) func NewDnstapFstrmUnixSocketInput(config *InputUnixSocketConfig) (*DnstapFstrmSocketInput, error) { os.Remove(config.GetPath()) l, err := net.Listen("unix", config.GetPath()) if err != nil { - return nil, errors.Wrapf(err, "can't listen %s", config.GetPath()) + return nil, fmt.Errorf("failed to listen %s: %w", config.GetPath(), err) } if config.GetUser() != "" { if u, err := user.Lookup(config.GetUser()); err != nil { - return nil, errors.Wrapf(err, "can't get chown user %s", config.GetUser()) + return nil, fmt.Errorf("failed to get chown user %s: %w", config.GetUser(), err) } else { uid, err := strconv.Atoi(u.Uid) if err != nil { - return nil, errors.Wrapf(err, "can't chown this system") + return nil, fmt.Errorf("failed to get uid: %w", err) } gid, err := strconv.Atoi(u.Gid) + if err != nil { + return nil, fmt.Errorf("failed to get gid: %w", err) + } if err := os.Chown(config.GetPath(), uid, gid); err != nil { - return nil, errors.Wrapf(err, "can't chown user %s (%s:%s)", config.GetUser(), u.Uid, u.Gid) + return nil, fmt.Errorf("failed to change owner %s (%s:%s): %w", config.GetUser(), u.Uid, u.Gid, err) } } } diff --git a/dnstap_fstrm_unix_socket_output.go b/dnstap_fstrm_unix_socket_output.go index 83a89cf..d137cb1 100644 --- a/dnstap_fstrm_unix_socket_output.go +++ b/dnstap_fstrm_unix_socket_output.go @@ -17,10 +17,9 @@ package dtap import ( + "fmt" "net" - "github.com/pkg/errors" - dnstap "github.com/dnstap/golang-dnstap" framestream "github.com/farsightsec/golang-framestream" ) @@ -39,13 +38,11 @@ func NewDnstapFstrmUnixSockOutput(config *OutputUnixSocketConfig, params *Dnstap func (o *DnstapFstrmUnixSockOutput) newConnect() (*framestream.Encoder, error) { w, err := net.Dial("unix", o.config.GetPath()) if err != nil { - - return nil, errors.Wrapf(err, "can't connect unix socket, path: %s", o.config.GetPath()) + return nil, fmt.Errorf("failed to connect unix socket, path: %s: %w", o.config.GetPath(), err) } enc, err := framestream.NewEncoder(w, &framestream.EncoderOptions{ContentType: dnstap.FSContentType, Bidirectional: true}) if err != nil { - - return nil, errors.Wrapf(err, "can't create fstrm encorder, path: %s", o.config.GetPath()) + return nil, fmt.Errorf("failed to create fstrm encorder, path: %s: %w", o.config.GetPath(), err) } return enc, nil } diff --git a/dnstap_kafka_output.go b/dnstap_kafka_output.go index 1b561f2..7abe356 100644 --- a/dnstap_kafka_output.go +++ b/dnstap_kafka_output.go @@ -19,6 +19,7 @@ package dtap import ( "encoding/binary" "encoding/json" + "fmt" "io/ioutil" "github.com/dangkaka/go-kafka-avro" @@ -29,7 +30,6 @@ import ( dnstap "github.com/dnstap/golang-dnstap" "github.com/golang/protobuf/proto" _ "github.com/mimuret/dtap/statik" - "github.com/pkg/errors" ) var schemaStr string @@ -85,14 +85,14 @@ func (o *DnstapKafkaOutput) open() error { var err error o.producer, err = sarama.NewSyncProducer(o.config.Hosts, o.kafkaConfig) if err != nil { - return errors.Wrapf(err, "can't create kafka producer") + return fmt.Errorf("failed to create kafka producer: %w", err) } if o.config.GetOutputType() == "avro" { if o.valueSchemaID, err = o.getSchemaID(o.config.GetTopic()+"-value", o.valueCodec); err != nil { - return errors.Wrapf(err, "can't get schema id") + return fmt.Errorf("failed to get schema id: %w", err) } if o.keySchemaID, err = o.getSchemaID(o.config.GetTopic()+"-key", o.keyCodec); err != nil { - return errors.Wrapf(err, "can't get schema id") + return fmt.Errorf("failed to get schema id: %w", err) } } return nil diff --git a/dnstap_nats_output.go b/dnstap_nats_output.go index 0a004ad..c42a8f5 100644 --- a/dnstap_nats_output.go +++ b/dnstap_nats_output.go @@ -19,6 +19,7 @@ package dtap import ( "context" "encoding/json" + "fmt" "sync" "time" @@ -26,7 +27,6 @@ import ( framestream "github.com/farsightsec/golang-framestream" "github.com/golang/protobuf/proto" nats "github.com/nats-io/go-nats" - "github.com/pkg/errors" "github.com/prometheus/common/log" ) @@ -62,7 +62,7 @@ func (o *DnstapNatsOutput) open() error { o.con, err = nats.Connect(o.config.GetHost()) } if err != nil { - return errors.Wrapf(err, "can't create nats producer") + return fmt.Errorf("failed to create nats producer: %w", err) } o.closeCh = make(chan struct{}) ctx, cancelFunc := context.WithCancel(context.Background()) diff --git a/example/kafka/dnstap_kafka_output copy.go b/example/kafka/dnstap_kafka_output copy.go deleted file mode 100644 index 6685de2..0000000 --- a/example/kafka/dnstap_kafka_output copy.go +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (c) 2019 Manabu Sonoda - * - * 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 dtap - -import ( - "encoding/json" - "fmt" - "io/ioutil" - - kafka "github.com/dangkaka/go-kafka-avro" - "github.com/rakyll/statik/fs" - - "github.com/Shopify/sarama" - dnstap "github.com/dnstap/golang-dnstap" - "github.com/golang/protobuf/proto" - _ "github.com/mimuret/dtap/statik" - "github.com/pkg/errors" -) - -var schemaStr string - -func init() { - var err error - statikFS, _ := fs.New() - f, _ := statikFS.Open("/flat.avsc") - b, _ := ioutil.ReadAll(f) - schemaStr = string(b) -} - -type KafkaClient interface { - Add(string, string, []byte, []byte) error -} - -type DnstapKafkaOutput struct { - config *OutputKafkaConfig - kafkaConfig *sarama.Config - producer KafkaClient -} - -func NewDnstapKafkaOutput(config *OutputKafkaConfig, params *DnstapOutputParams) *DnstapOutput { - kafkaConfig := sarama.NewConfig() - kafkaConfig.Producer.Return.Successes = true - kafkaConfig.Producer.Return.Errors = true - kafkaConfig.Producer.Retry.Max = int(config.GetRetry()) - - params.Handler = &DnstapKafkaOutput{ - config: config, - kafkaConfig: kafkaConfig, - } - return NewDnstapOutput(params) -} - -func (o *DnstapKafkaOutput) open() error { - var err error - o.producer, err = kafka.NewAvroProducer(o.config.Hosts, o.config.SchemaRegistries) - if err != nil { - return errors.Wrapf(err, "can't create kafka producer") - } - return nil -} - -func (o *DnstapKafkaOutput) write(frame []byte) error { - var k, v sarama.Encoder - var bs []byte - switch o.config.GetOutputType() { - case "avro": - dt := dnstap.Dnstap{} - if err := proto.Unmarshal(frame, &dt); err != nil { - return err - } - if o.config.GetOutputType() == "json" { - bs, err := json.Marshal(dt) - if err != nil { - return err - } - k = sarama.StringEncoder(o.config.GetKey()) - v = sarama.StringEncoder(string(bs)) - } else { - dt := dnstap.Dnstap{} - if err := proto.Unmarshal(frame, &dt); err != nil { - return err - } - data, err := FlatDnstap(&dt, &o.config.Flat) - if err != nil { - return err - } - if o.config.GetOutputType() == "avro" { - bs, err := json.Marshal(data) - if err != nil { - return err - } - o.producer.Add(o.config.GetTopic(), schemaStr, []byte(o.config.GetKey()), bs) - /* mapString := data.ToMapString() - log.Debug(mapString) - binary, err := codec.BinaryFromNative(nil, mapString) - if err != nil { - return err - } - k = sarama.ByteEncoder(o.config.GetKey()) - v = sarama.ByteEncoder(binary) - */ - } else { - bs, err := json.Marshal(data) - if err != nil { - return err - } - k = sarama.StringEncoder(o.config.GetKey()) - v = sarama.StringEncoder(string(bs)) - - } - } - default: - panic(fmt.Errorf("OutputType error: %s", o.config.GetOutputType())) - } - - return nil -} - -func (o *DnstapKafkaOutput) close() { -} diff --git a/flat.go b/flat.go index eb1926b..a0385a2 100644 --- a/flat.go +++ b/flat.go @@ -25,7 +25,6 @@ import ( dnstap "github.com/dnstap/golang-dnstap" "github.com/miekg/dns" - "github.com/pkg/errors" ) type DnstapFlatT struct { @@ -127,7 +126,7 @@ func FlatDnstap(dt *dnstap.Dnstap, opt DnstapFlatOption) (*DnstapFlatT, error) { data.Extra = string(dt.GetExtra()) dnsMsg := dns.Msg{} if err := dnsMsg.Unpack(dnsMessage); err != nil { - return nil, errors.Wrapf(err, "can't parse dns message() failed: %s\n", err) + return nil, fmt.Errorf("failed to parse dns message() err: %w", err) } if len(dnsMsg.Question) > 0 {