Skip to content

Commit

Permalink
tracing tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
steveww committed Jan 31, 2018
1 parent e778bd7 commit 3f5034a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 23 deletions.
29 changes: 24 additions & 5 deletions cart/server.js
@@ -1,16 +1,17 @@
const instana = require('instana-nodejs-sensor');
const redis = require('redis');
const request = require('request');
const bodyParser = require('body-parser');
const express = require('express');

// init tracing
// MUST be done before loading anything else!
instana({
tracing: {
enabled: true
}
});

const redis = require('redis');
const request = require('request');
const bodyParser = require('body-parser');
const express = require('express');

var redisConnected = false;

const app = express();
Expand Down Expand Up @@ -66,6 +67,24 @@ app.delete('/cart/:id', (req, res) => {
});
});

// rename cart i.e. at login
app.get('/rename/:from/:to', (req, res) => {
redisClient.get(req.params.from, (err, data) => {
if(err) {
console.log('ERROR', err);
res.status(500).send(err);
} else {
if(data == null) {
res.status(404).send('cart not found');
} else {
var cart = JSON.parse(data);
saveCart(req.params.to, cart);
res.json(cart);
}
}
});
});

// update/create cart
app.get('/add/:id/:sku/:qty', (req, res) => {
// check quantity
Expand Down
11 changes: 6 additions & 5 deletions catalogue/server.js
@@ -1,16 +1,17 @@
const instana = require('instana-nodejs-sensor');
const mongoClient = require('mongodb').MongoClient;
const mongoObjectID = require('mongodb').ObjectID;
const bodyParser = require('body-parser');
const express = require('express');

// init tracing
// MUST be done before loading anything else!
instana({
tracing: {
enabled: true
}
});

const mongoClient = require('mongodb').MongoClient;
const mongoObjectID = require('mongodb').ObjectID;
const bodyParser = require('body-parser');
const express = require('express');

// MongoDB
var db;
var collection;
Expand Down
31 changes: 30 additions & 1 deletion dispatch/src/main.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/streadway/amqp"
"github.com/instana/golang-sensor"
ot "github.com/opentracing/opentracing-go"
// ext "github.com/opentracing/opentracing-go/ext"
ext "github.com/opentracing/opentracing-go/ext"
)

const (
Expand Down Expand Up @@ -77,6 +77,33 @@ func failOnError(err error, msg string) {
}
}

func createSpan(headers map[string]interface{}) {
// headers is map[string]interface{}
// carrier is map[string]string
carrier := make(ot.TextMapCarrier)
// convert by copying k, v
for k, v := range headers {
carrier[k] = v.(string)
}

// opentracing
var span ot.Span
tracer := ot.GlobalTracer()
spanContext, err := tracer.Extract(ot.HTTPHeaders, carrier)
if err == nil {
log.Println("Creating span")
// create span
span = tracer.StartSpan("dispatch", ot.ChildOf(spanContext), ext.SpanKindConsumer)
ext.MessageBusDestination.Set(span, "orders")
ext.Component.Set(span, "dispatch")
defer span.Finish()
time.Sleep(42 * time.Millisecond)
} else {
log.Println("Failed to get span context")
log.Println(err)
}
}


func main() {
// Instana tracing
Expand Down Expand Up @@ -106,6 +133,8 @@ func main() {

for d := range msgs {
log.Printf("Order %s\n", d.Body)
log.Printf("Headers %v\n", d.Headers)
go createSpan(d.Headers)
}
}
}()
Expand Down
23 changes: 22 additions & 1 deletion payment/payment.py
Expand Up @@ -4,6 +4,8 @@
import logging
import uuid
import requests
import opentracing as ot
import opentracing.ext.tags as tags
from flask import Flask
from flask import request
from flask import jsonify
Expand Down Expand Up @@ -36,7 +38,26 @@ def pay(id):

def queueOrder(order):
app.logger.info('queue order')
publisher.publish(order)
# RabbitMQ is not currently traced automatically
# opentracing tracer is automatically set to Instana tracer
# start a span
context = ot.tracer.current_context()
span = ot.tracer.start_span(operation_name='queue-order',
child_of=ot.tracer.current_context(),
tags={
tags.SPAN_KIND: 'producer',
tags.COMPONENT: 'payment',
'message_bus.destination': 'orders'
}
)

headers = {}
ot.tracer.inject(span.context, ot.Format.HTTP_HEADERS, headers)
app.logger.info('msg headers {}'.format(headers))

publisher.publish(order, headers)

span.finish()

# RabbitMQ
publisher = Publisher(app.logger)
Expand Down
9 changes: 5 additions & 4 deletions payment/rabbitmq.py
Expand Up @@ -24,22 +24,23 @@ def _connect(self):
self._channel.exchange_declare(exchange=self.EXCHANGE, exchange_type=self.TYPE, durable=True)
self._logger.info('connected to broker')

def _publish(self, msg):
def _publish(self, msg, headers):
self._channel.basic_publish(exchange=self.EXCHANGE,
routing_key=self.ROUTING_KEY,
properties=pika.BasicProperties(headers=headers),
body=json.dumps(msg).encode())
self._logger.info('message sent')

#Publish msg, reconnecting if necessary.
def publish(self, msg):
def publish(self, msg, headers):
if self._channel is None:
self._connect()
try:
self._publish(msg)
self._publish(msg, headers)
except pika.exceptions.ConnectionClosed:
self._logger.info('reconnecting to queue')
self._connect()
self._publish(msg)
self._publish(msg, headers)

def close(self):
if self._conn and self._conn.is_open:
Expand Down
18 changes: 11 additions & 7 deletions user/server.js
@@ -1,17 +1,18 @@
const instana = require('instana-nodejs-sensor');
const mongoClient = require('mongodb').MongoClient;
const mongoObjectID = require('mongodb').ObjectID;
const redis = require('redis');
const bodyParser = require('body-parser');
const express = require('express');

// init tracing
// MUST be done before loading anything else!
instana({
tracing: {
enabled: true
}
});

const mongoClient = require('mongodb').MongoClient;
const mongoObjectID = require('mongodb').ObjectID;
const redis = require('redis');
const bodyParser = require('body-parser');
const express = require('express');

// MongoDB
var db;
var collection;
Expand Down Expand Up @@ -39,11 +40,14 @@ app.get('/health', (req, res) => {
// use REDIS INCR to track anonymous users
app.get('/uniqueid', (req, res) => {
// get number from Redis
redisClient.incr('user', (err, r) => {
redisClient.incr('anonymous-counter', (err, r) => {
if(!err) {
res.json({
uuid: 'anonymous-' + r
});
} else {
console.log('ERROR', err);
res.status(500).send(err);
}
});
});
Expand Down

0 comments on commit 3f5034a

Please sign in to comment.