From 0ed06381403a65f673671105c8075d0e2dbc1a91 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Fri, 5 Mar 2021 16:03:55 -0500 Subject: [PATCH] add options to configure logger by interface This commit provides users of this library with the ability to configure any logger of their choosing to be passed into the various components of this library. Doing so allows use cases such as using other open source logging packages, e.g. logrus or zap, in conjunction with this library. Closes https://github.com/openzipkin/zipkin-go/issues/37 --- logger.go | 27 +++++++++++++++++++++++++++ middleware/http/transport.go | 9 ++++++++- reporter/amqp/amqp.go | 11 ++++++++++- reporter/http/http.go | 9 ++++++++- reporter/kafka/kafka.go | 11 ++++++++++- 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 logger.go diff --git a/logger.go b/logger.go new file mode 100644 index 00000000..40f2a7db --- /dev/null +++ b/logger.go @@ -0,0 +1,27 @@ +// Copyright 2019 The OpenZipkin 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 zipkin + +import "log" + +// Verify that the Logger interface is implemented by the stdlib logger. +var _ Logger = (*log.Logger)(nil) + +// Logger provides an interface defining the needed behavior for +// loggers passed into various components. +type Logger interface { + Print(v ...interface{}) + Printf(format string, v ...interface{}) +} diff --git a/middleware/http/transport.go b/middleware/http/transport.go index af029186..6fd2972d 100644 --- a/middleware/http/transport.go +++ b/middleware/http/transport.go @@ -56,7 +56,7 @@ type transport struct { defaultTags map[string]string errHandler ErrHandler errResponseReader *ErrResponseReader - logger *log.Logger + logger zipkin.Logger requestSampler RequestSamplerFunc remoteEndpoint *model.Endpoint } @@ -115,6 +115,13 @@ func TransportLogger(l *log.Logger) TransportOption { } } +// TransportZipkinLogger allows to plug a logger into the transport +func TransportZipkinLogger(l zipkin.Logger) TransportOption { + return func(t *transport) { + t.logger = l + } +} + // TransportRequestSampler allows one to set the sampling decision based on // the details found in the http.Request. It has preference over the existing // sampling decision contained in the context. The function returns a *bool, diff --git a/reporter/amqp/amqp.go b/reporter/amqp/amqp.go index 5aca7f15..ff146388 100644 --- a/reporter/amqp/amqp.go +++ b/reporter/amqp/amqp.go @@ -11,6 +11,7 @@ import ( "github.com/streadway/amqp" + "github.com/openzipkin/zipkin-go" "github.com/openzipkin/zipkin-go/model" "github.com/openzipkin/zipkin-go/reporter" ) @@ -29,7 +30,7 @@ type rmqReporter struct { conn *amqp.Connection exchange string queue string - logger *log.Logger + logger zipkin.Logger } // ReporterOption sets a parameter for the rmqReporter @@ -43,6 +44,14 @@ func Logger(logger *log.Logger) ReporterOption { } } +// GenericLogger sets the logger used to report errors in the collection +// process. +func GenericLogger(logger zipkin.Logger) ReporterOption { + return func(c *rmqReporter) { + c.logger = logger + } +} + // Exchange sets the Exchange used to send messages ( // see https://github.com/openzipkin/zipkin/tree/master/zipkin-collector/rabbitmq // if want to change default routing key or exchange diff --git a/reporter/http/http.go b/reporter/http/http.go index a01f6899..69816f18 100644 --- a/reporter/http/http.go +++ b/reporter/http/http.go @@ -26,6 +26,7 @@ import ( "sync" "time" + "github.com/openzipkin/zipkin-go" "github.com/openzipkin/zipkin-go/model" "github.com/openzipkin/zipkin-go/reporter" ) @@ -47,7 +48,7 @@ type HTTPDoer interface { type httpReporter struct { url string client HTTPDoer - logger *log.Logger + logger zipkin.Logger batchInterval time.Duration batchSize int maxBacklog int @@ -233,6 +234,12 @@ func Logger(l *log.Logger) ReporterOption { return func(r *httpReporter) { r.logger = l } } +// GenericLogger sets the logger used to report errors in the collection +// process. +func GenericLogger(l zipkin.Logger) ReporterOption { + return func(r *httpReporter) { r.logger = l } +} + // Serializer sets the serialization function to use for sending span data to // Zipkin. func Serializer(serializer reporter.SpanSerializer) ReporterOption { diff --git a/reporter/kafka/kafka.go b/reporter/kafka/kafka.go index e4ca50ce..8130759f 100644 --- a/reporter/kafka/kafka.go +++ b/reporter/kafka/kafka.go @@ -22,6 +22,7 @@ import ( "os" "github.com/Shopify/sarama" + "github.com/openzipkin/zipkin-go" "github.com/openzipkin/zipkin-go/model" "github.com/openzipkin/zipkin-go/reporter" ) @@ -35,7 +36,7 @@ const defaultKafkaTopic = "zipkin" // broker. type kafkaReporter struct { producer sarama.AsyncProducer - logger *log.Logger + logger zipkin.Logger topic string serializer reporter.SpanSerializer } @@ -51,6 +52,14 @@ func Logger(logger *log.Logger) ReporterOption { } } +// GenericLogger sets the logger used to report errors in the collection +// process. +func GenericLogger(logger zipkin.Logger) ReporterOption { + return func(c *kafkaReporter) { + c.logger = logger + } +} + // Producer sets the producer used to produce to Kafka. For tweaking // the reporting settings (e.g. reporting timeout or authentication) // check the sarama.Config struct.