Skip to content

Commit

Permalink
add options to configure logger by interface
Browse files Browse the repository at this point in the history
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 #37
  • Loading branch information
kujenga committed Mar 5, 2021
1 parent 0b3ebdb commit a8f3664
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
9 changes: 8 additions & 1 deletion middleware/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion reporter/amqp/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion reporter/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync"
"time"

"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/model"
"github.com/openzipkin/zipkin-go/reporter"
)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion reporter/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand All @@ -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.
Expand Down

0 comments on commit a8f3664

Please sign in to comment.