Skip to content

Commit 0ed0638

Browse files
committed
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 #37
1 parent 0b3ebdb commit 0ed0638

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

logger.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 The OpenZipkin Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zipkin
16+
17+
import "log"
18+
19+
// Verify that the Logger interface is implemented by the stdlib logger.
20+
var _ Logger = (*log.Logger)(nil)
21+
22+
// Logger provides an interface defining the needed behavior for
23+
// loggers passed into various components.
24+
type Logger interface {
25+
Print(v ...interface{})
26+
Printf(format string, v ...interface{})
27+
}

middleware/http/transport.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type transport struct {
5656
defaultTags map[string]string
5757
errHandler ErrHandler
5858
errResponseReader *ErrResponseReader
59-
logger *log.Logger
59+
logger zipkin.Logger
6060
requestSampler RequestSamplerFunc
6161
remoteEndpoint *model.Endpoint
6262
}
@@ -115,6 +115,13 @@ func TransportLogger(l *log.Logger) TransportOption {
115115
}
116116
}
117117

118+
// TransportZipkinLogger allows to plug a logger into the transport
119+
func TransportZipkinLogger(l zipkin.Logger) TransportOption {
120+
return func(t *transport) {
121+
t.logger = l
122+
}
123+
}
124+
118125
// TransportRequestSampler allows one to set the sampling decision based on
119126
// the details found in the http.Request. It has preference over the existing
120127
// sampling decision contained in the context. The function returns a *bool,

reporter/amqp/amqp.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/streadway/amqp"
1313

14+
"github.com/openzipkin/zipkin-go"
1415
"github.com/openzipkin/zipkin-go/model"
1516
"github.com/openzipkin/zipkin-go/reporter"
1617
)
@@ -29,7 +30,7 @@ type rmqReporter struct {
2930
conn *amqp.Connection
3031
exchange string
3132
queue string
32-
logger *log.Logger
33+
logger zipkin.Logger
3334
}
3435

3536
// ReporterOption sets a parameter for the rmqReporter
@@ -43,6 +44,14 @@ func Logger(logger *log.Logger) ReporterOption {
4344
}
4445
}
4546

47+
// GenericLogger sets the logger used to report errors in the collection
48+
// process.
49+
func GenericLogger(logger zipkin.Logger) ReporterOption {
50+
return func(c *rmqReporter) {
51+
c.logger = logger
52+
}
53+
}
54+
4655
// Exchange sets the Exchange used to send messages (
4756
// see https://github.com/openzipkin/zipkin/tree/master/zipkin-collector/rabbitmq
4857
// if want to change default routing key or exchange

reporter/http/http.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sync"
2727
"time"
2828

29+
"github.com/openzipkin/zipkin-go"
2930
"github.com/openzipkin/zipkin-go/model"
3031
"github.com/openzipkin/zipkin-go/reporter"
3132
)
@@ -47,7 +48,7 @@ type HTTPDoer interface {
4748
type httpReporter struct {
4849
url string
4950
client HTTPDoer
50-
logger *log.Logger
51+
logger zipkin.Logger
5152
batchInterval time.Duration
5253
batchSize int
5354
maxBacklog int
@@ -233,6 +234,12 @@ func Logger(l *log.Logger) ReporterOption {
233234
return func(r *httpReporter) { r.logger = l }
234235
}
235236

237+
// GenericLogger sets the logger used to report errors in the collection
238+
// process.
239+
func GenericLogger(l zipkin.Logger) ReporterOption {
240+
return func(r *httpReporter) { r.logger = l }
241+
}
242+
236243
// Serializer sets the serialization function to use for sending span data to
237244
// Zipkin.
238245
func Serializer(serializer reporter.SpanSerializer) ReporterOption {

reporter/kafka/kafka.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323

2424
"github.com/Shopify/sarama"
25+
"github.com/openzipkin/zipkin-go"
2526
"github.com/openzipkin/zipkin-go/model"
2627
"github.com/openzipkin/zipkin-go/reporter"
2728
)
@@ -35,7 +36,7 @@ const defaultKafkaTopic = "zipkin"
3536
// broker.
3637
type kafkaReporter struct {
3738
producer sarama.AsyncProducer
38-
logger *log.Logger
39+
logger zipkin.Logger
3940
topic string
4041
serializer reporter.SpanSerializer
4142
}
@@ -51,6 +52,14 @@ func Logger(logger *log.Logger) ReporterOption {
5152
}
5253
}
5354

55+
// GenericLogger sets the logger used to report errors in the collection
56+
// process.
57+
func GenericLogger(logger zipkin.Logger) ReporterOption {
58+
return func(c *kafkaReporter) {
59+
c.logger = logger
60+
}
61+
}
62+
5463
// Producer sets the producer used to produce to Kafka. For tweaking
5564
// the reporting settings (e.g. reporting timeout or authentication)
5665
// check the sarama.Config struct.

0 commit comments

Comments
 (0)