-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from maxekman/tracing
Tracing with OpenTracing
- Loading branch information
Showing
40 changed files
with
1,271 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2020 - The Event Horizon 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 tracing | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
|
||
eh "github.com/looplab/eventhorizon" | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
) | ||
|
||
// The string keys to marshal the context. | ||
const ( | ||
tracingSpanKeyStr = "eh_tracing_span" | ||
) | ||
|
||
func init() { | ||
eh.RegisterContextMarshaler(func(ctx context.Context, vals map[string]interface{}) { | ||
if span := opentracing.SpanFromContext(ctx); span != nil { | ||
tracer := opentracing.GlobalTracer() | ||
carrier := opentracing.TextMapCarrier{} | ||
if err := tracer.Inject(span.Context(), opentracing.TextMap, &carrier); err != nil { | ||
log.Printf("eventhorizon: could not inject tracing span: %s", err) | ||
return | ||
} | ||
js, err := json.Marshal(carrier) | ||
if err != nil { | ||
log.Printf("eventhorizon: could not marshal tracing span: %s", err) | ||
return | ||
} | ||
vals[tracingSpanKeyStr] = string(js) | ||
} | ||
}) | ||
eh.RegisterContextUnmarshaler(func(ctx context.Context, vals map[string]interface{}) context.Context { | ||
if js, ok := vals[tracingSpanKeyStr].(string); ok { | ||
tracer := opentracing.GlobalTracer() | ||
carrier := opentracing.TextMapCarrier{} | ||
if err := json.Unmarshal([]byte(js), &carrier); err != nil { | ||
log.Printf("eventhorizon: could not unmarshal tracing span: %s", err) | ||
return ctx | ||
} | ||
parentSpanContext, err := tracer.Extract(opentracing.TextMap, carrier) | ||
if err != nil && err != opentracing.ErrSpanContextNotFound { | ||
log.Printf("eventhorizon: could not extract tracing span: %s", err) | ||
return ctx | ||
} | ||
span := tracer.StartSpan("eventbus", ext.RPCServerOption(parentSpanContext)) | ||
ctx = opentracing.ContextWithSpan(ctx, span) | ||
} | ||
return ctx | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2020 - The Event Horizon 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 tracing | ||
|
||
import ( | ||
"context" | ||
|
||
eh "github.com/looplab/eventhorizon" | ||
"github.com/looplab/eventhorizon/middleware/eventhandler/tracing" | ||
) | ||
|
||
// EventBus is an event bus wrapper that adds tracing. | ||
type EventBus struct { | ||
eh.EventBus | ||
h eh.EventHandler | ||
} | ||
|
||
// NewEventBus creates a EventBus. | ||
func NewEventBus(eventBus eh.EventBus) *EventBus { | ||
return &EventBus{ | ||
EventBus: eventBus, | ||
// Wrap the eh.EventHandler part of the bus with tracing middleware, | ||
// set as producer to set the correct tags. | ||
h: eh.UseEventHandlerMiddleware(eventBus, tracing.NewMiddleware()), | ||
} | ||
} | ||
|
||
// HandleEvent implements the HandleEvent method of the eventhorizon.EventHandler interface. | ||
func (b *EventBus) HandleEvent(ctx context.Context, event eh.Event) error { | ||
return b.h.HandleEvent(ctx, event) | ||
} | ||
|
||
// AddHandler implements the AddHandler method of the eventhorizon.EventBus interface. | ||
func (b *EventBus) AddHandler(ctx context.Context, m eh.EventMatcher, h eh.EventHandler) error { | ||
if h == nil { | ||
return eh.ErrMissingHandler | ||
} | ||
// Wrap the handlers in tracing middleware. | ||
h = eh.UseEventHandlerMiddleware(h, tracing.NewMiddleware()) | ||
return b.EventBus.AddHandler(ctx, m, h) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2020 - The Event Horizon 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 tracing | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/looplab/eventhorizon/eventbus" | ||
"github.com/looplab/eventhorizon/eventbus/local" | ||
) | ||
|
||
func TestEventBus(t *testing.T) { | ||
group := local.NewGroup() | ||
if group == nil { | ||
t.Fatal("there should be a group") | ||
} | ||
innerBus1 := local.NewEventBus(group) | ||
if innerBus1 == nil { | ||
t.Fatal("there should be a bus") | ||
} | ||
innerBus2 := local.NewEventBus(group) | ||
if innerBus2 == nil { | ||
t.Fatal("there should be a bus") | ||
} | ||
|
||
bus1 := NewEventBus(innerBus1) | ||
if bus1 == nil { | ||
t.Fatal("there should be a bus") | ||
} | ||
|
||
bus2 := NewEventBus(innerBus2) | ||
if bus2 == nil { | ||
t.Fatal("there should be a bus") | ||
} | ||
|
||
eventbus.AcceptanceTest(t, bus1, bus2, time.Second) | ||
} | ||
|
||
func TestEventBusLoad(t *testing.T) { | ||
innerBus := local.NewEventBus(nil) | ||
if innerBus == nil { | ||
t.Fatal("there should be a bus") | ||
} | ||
|
||
bus := NewEventBus(innerBus) | ||
if bus == nil { | ||
t.Fatal("there should be a bus") | ||
} | ||
|
||
eventbus.LoadTest(t, bus) | ||
} | ||
|
||
func BenchmarkEventBus(b *testing.B) { | ||
innerBus := local.NewEventBus(nil) | ||
if innerBus == nil { | ||
b.Fatal("there should be a bus") | ||
} | ||
|
||
bus := NewEventBus(innerBus) | ||
if bus == nil { | ||
b.Fatal("there should be a bus") | ||
} | ||
|
||
eventbus.Benchmark(b, bus) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.