improved correlation via rabbitmq

This commit is contained in:
Steve Waterworth
2019-02-08 13:17:52 +00:00
parent 7a76c9db36
commit 61473b16f1
4 changed files with 61 additions and 26 deletions

2
.env
View File

@@ -1,3 +1,3 @@
# environment file for docker-compose # environment file for docker-compose
REPO=robotshop REPO=robotshop
TAG=0.4.2 TAG=0.4.3

View File

@@ -8,10 +8,10 @@ COPY src/ /go/src/github.com/instana/dispatch
WORKDIR /go/src/github.com/instana/dispatch WORKDIR /go/src/github.com/instana/dispatch
RUN curl -fsSL -o /usr/local/bin/dep ${DEP_URL} && \ RUN curl -fsSL -o /usr/local/bin/dep ${DEP_URL} && \
chmod +x /usr/local/bin/dep && \ chmod +x /usr/local/bin/dep
dep ensure && \
go build -o bin/gorcv
# @todo stage this build RUN dep ensure && go build -o bin/gorcv
# TODO stage this build
CMD bin/gorcv CMD bin/gorcv

View File

@@ -7,11 +7,12 @@ import (
"os" "os"
"math/rand" "math/rand"
"strconv" "strconv"
"encoding/json"
"github.com/streadway/amqp" "github.com/streadway/amqp"
"github.com/instana/golang-sensor" "github.com/instana/golang-sensor"
ot "github.com/opentracing/opentracing-go" ot "github.com/opentracing/opentracing-go"
ext "github.com/opentracing/opentracing-go/ext" // ext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log" otlog "github.com/opentracing/opentracing-go/log"
) )
@@ -81,7 +82,19 @@ func failOnError(err error, msg string) {
} }
} }
func createSpan(headers map[string]interface{}) { func getOrderId(order []byte) string {
id := "unknown"
var f interface{}
err := json.Unmarshal(order, &f)
if err == nil {
m := f.(map[string]interface{})
id = m["orderid"].(string)
}
return id
}
func createSpan(headers map[string]interface{}, order string) {
// headers is map[string]interface{} // headers is map[string]interface{}
// carrier is map[string]string // carrier is map[string]string
carrier := make(ot.TextMapCarrier) carrier := make(ot.TextMapCarrier)
@@ -90,6 +103,9 @@ func createSpan(headers map[string]interface{}) {
carrier[k] = v.(string) carrier[k] = v.(string)
} }
// get the order id
log.Printf("order %s\n", order)
// opentracing // opentracing
var span ot.Span var span ot.Span
tracer := ot.GlobalTracer() tracer := ot.GlobalTracer()
@@ -97,10 +113,14 @@ func createSpan(headers map[string]interface{}) {
if err == nil { if err == nil {
log.Println("Creating span") log.Println("Creating span")
// create span // create span
span = tracer.StartSpan("dispatch", ot.ChildOf(spanContext), ext.SpanKindConsumer) span = tracer.StartSpan("getOrder", ot.ChildOf(spanContext))
ext.MessageBusDestination.Set(span, "orders") span.SetTag("exchange", "robot-shop")
ext.Component.Set(span, "dispatch") span.SetTag("sort", "consume")
span.SetTag("address", "rabbitmq")
span.SetTag("key", "orders")
span.LogFields(otlog.String("orderid", order))
defer span.Finish() defer span.Finish()
time.Sleep(time.Duration(42 + rand.Int63n(42)) * time.Millisecond) time.Sleep(time.Duration(42 + rand.Int63n(42)) * time.Millisecond)
if rand.Intn(100) < errorPercent { if rand.Intn(100) < errorPercent {
span.SetTag("error", true) span.SetTag("error", true)
@@ -109,12 +129,21 @@ func createSpan(headers map[string]interface{}) {
otlog.String("message", "Failed to dispatch to SOP")) otlog.String("message", "Failed to dispatch to SOP"))
log.Println("Span tagged with error") log.Println("Span tagged with error")
} }
processSale(span)
} else { } else {
log.Println("Failed to get span context") log.Println("Failed to get span context")
log.Println(err) log.Println(err)
} }
} }
func processSale(parentSpan ot.Span) {
tracer := ot.GlobalTracer()
span := tracer.StartSpan("processSale", ot.ChildOf(parentSpan.Context()))
defer span.Finish()
span.LogFields(otlog.String("info", "Order sent for processing"))
time.Sleep(time.Duration(42 + rand.Int63n(42)) * time.Millisecond)
}
func main() { func main() {
// Instana tracing // Instana tracing
@@ -170,7 +199,8 @@ func main() {
for d := range msgs { for d := range msgs {
log.Printf("Order %s\n", d.Body) log.Printf("Order %s\n", d.Body)
log.Printf("Headers %v\n", d.Headers) log.Printf("Headers %v\n", d.Headers)
go createSpan(d.Headers) id := getOrderId(d.Body)
go createSpan(d.Headers, id)
} }
} }
}() }()

View File

@@ -108,13 +108,18 @@ def queueOrder(order):
# opentracing tracer is automatically set to Instana tracer # opentracing tracer is automatically set to Instana tracer
# start a span # start a span
# context = ot.tracer.current_context()
parent_span = ot.tracer.active_span parent_span = ot.tracer.active_span
with ot.tracer.start_active_span('queue-order', child_of=parent_span, with ot.tracer.start_active_span('queueOrder', child_of=parent_span,
tags={ tags={
tags.SPAN_KIND: 'producer', 'exchange': Publisher.EXCHANGE,
tags.COMPONENT: 'payment', 'key': Publisher.ROUTING_KEY
'message_bus.destination': 'orders' }) as tscope:
with ot.tracer.start_active_span('rabbitmq', child_of=tscope.span,
tags={
'exchange': Publisher.EXCHANGE,
'sort': 'publish',
'address': Publisher.HOST,
'key': Publisher.ROUTING_KEY
} }
) as scope: ) as scope: