Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions instana/json_span.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class JsonSpan(object):
t = 0
p = None
s = 0
ts = 0
ta = "py"
d = 0
n = None
f = None
ec = 0
error = None
data = None

def __init__(self, **kwds):
for key in kwds:
self.__dict__[key] = kwds[key]


class Data(object):
service = None
http = None
baggage = None
custom = None
sdk = None

def __init__(self, **kwds):
self.__dict__.update(kwds)


class HttpData(object):
host = None
url = None
status = 0
method = None

def __init__(self, **kwds):
self.__dict__.update(kwds)


class CustomData(object):
tags = None
logs = None

def __init__(self, **kwds):
self.__dict__.update(kwds)


class SDKData(object):
name = None
Type = None
arguments = None
Return = None
custom = None

def __init__(self, **kwds):
self.__dict__.update(kwds)
20 changes: 10 additions & 10 deletions instana/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import opentracing.ext.tags as ext
from basictracer import Sampler, SpanRecorder
from .span import CustomData, Data, HttpData, InstanaSpan, SDKData
from .json_span import CustomData, Data, HttpData, JsonSpan, SDKData
from .agent_const import AGENT_TRACES_URL

import sys
Expand Down Expand Up @@ -65,21 +65,21 @@ def clear_spans(self):

def record_span(self, span):
"""
Convert the passed BasicSpan into an InstanaSpan and
Convert the passed BasicSpan into an JsonSpan and
add it to the span queue
"""
if self.sensor.agent.can_send() or "INSTANA_TEST" in os.environ:
instana_span = None
json_span = None

if span.operation_name in self.registered_spans:
instana_span = self.build_registered_span(span)
json_span = self.build_registered_span(span)
else:
instana_span = self.build_sdk_span(span)
json_span = self.build_sdk_span(span)

self.queue.put(instana_span)
self.queue.put(json_span)

def build_registered_span(self, span):
""" Takes a BasicSpan and converts it into a registered InstanaSpan """
""" Takes a BasicSpan and converts it into a registered JsonSpan """
data = Data(http=HttpData(host=self.get_host_name(span),
url=self.get_string_tag(span, ext.HTTP_URL),
method=self.get_string_tag(span, ext.HTTP_METHOD),
Expand All @@ -90,7 +90,7 @@ def build_registered_span(self, span):
entityFrom = {'e': self.sensor.agent.from_.pid,
'h': self.sensor.agent.from_.agentUuid}

return InstanaSpan(
return JsonSpan(
n=span.operation_name,
t=span.context.trace_id,
p=span.parent_id,
Expand All @@ -103,7 +103,7 @@ def build_registered_span(self, span):
data=data)

def build_sdk_span(self, span):
""" Takes a BasicSpan and converts into an SDK type InstanaSpan """
""" Takes a BasicSpan and converts into an SDK type JsonSpan """

custom_data = CustomData(tags=span.tags,
logs=self.collect_logs(span))
Expand All @@ -116,7 +116,7 @@ def build_sdk_span(self, span):
entityFrom = {'e': self.sensor.agent.from_.pid,
'h': self.sensor.agent.from_.agentUuid}

return InstanaSpan(
return JsonSpan(
t=span.context.trace_id,
p=span.parent_id,
s=span.context.span_id,
Expand Down
72 changes: 16 additions & 56 deletions instana/span.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,16 @@
class InstanaSpan(object):
t = 0
p = None
s = 0
ts = 0
ta = "py"
d = 0
n = None
f = None
ec = 0
error = None
data = None

def __init__(self, **kwds):
for key in kwds:
self.__dict__[key] = kwds[key]


class Data(object):
service = None
http = None
baggage = None
custom = None
sdk = None

def __init__(self, **kwds):
self.__dict__.update(kwds)


class HttpData(object):
host = None
url = None
status = 0
method = None

def __init__(self, **kwds):
self.__dict__.update(kwds)


class CustomData(object):
tags = None
logs = None

def __init__(self, **kwds):
self.__dict__.update(kwds)


class SDKData(object):
name = None
Type = None
arguments = None
Return = None
custom = None

def __init__(self, **kwds):
self.__dict__.update(kwds)
from basictracer.span import BasicSpan
from basictracer.context import SpanContext


class InstanaSpan(BasicSpan):
def finish(self, finish_time=None):
if self.parent_id is None:
self.tracer.cur_ctx = None
else:
# Set tracer context to the parent span
pctx = SpanContext(span_id=self.parent_id,
trace_id=self.context.trace_id,
baggage={},
sampled=True)
self.tracer.cur_ctx = pctx
super(InstanaSpan, self).finish(finish_time)
15 changes: 8 additions & 7 deletions instana/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import instana
import instana.options as o

from instana.util import generate_id
from instana.span import InstanaSpan
from basictracer.context import SpanContext
from basictracer.span import BasicSpan
from instana.http_propagator import HTTPPropagator
from instana.text_propagator import TextPropagator
from instana.util import generate_id


class InstanaTracer(BasicTracer):
sensor = None
current_span = None
cur_ctx = None

def __init__(self, options=o.Options()):
self.sensor = instana.global_sensor
Expand Down Expand Up @@ -58,20 +58,21 @@ def start_span(
ctx.sampled = self.sampler.sampled(ctx.trace_id)

# Tie it all together
self.current_span = BasicSpan(
span = InstanaSpan(
self,
operation_name=operation_name,
context=ctx,
parent_id=(None if parent_ctx is None else parent_ctx.span_id),
tags=tags,
start_time=start_time)

return self.current_span
self.cur_ctx = span.context
return span

def current_context(self):
context = None
if self.current_span:
context = self.current_span.context
if self.cur_ctx is not None:
context = self.cur_ctx
return context

def inject(self, span_context, format, carrier):
Expand Down
14 changes: 12 additions & 2 deletions instana/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import os
import json
import time
import struct
import binascii
Expand All @@ -14,8 +15,9 @@
_rnd = random.Random()
_current_pid = 0

BAD_ID_LONG = 3135097598 # Bad Cafe in base 10
BAD_ID_HEADER = "BADDCAFE" # Bad Cafe
BAD_ID_LONG = 3135097598 # Bad Cafe in base 10
BAD_ID_HEADER = "BADDCAFE" # Bad Cafe


def generate_id():
""" Generate a 64bit signed integer for use as a Span or Trace ID """
Expand Down Expand Up @@ -58,3 +60,11 @@ def header_to_id(header):
return struct.unpack('>q', r)[0]
except ValueError:
return BAD_ID_LONG


def to_json(obj):
try:
return json.dumps(obj, default=lambda obj: {k.lower(): v for k, v in obj.__dict__.items()},
sort_keys=False, separators=(',', ':')).encode()
except Exception as e:
log.info("to_json: ", e, obj)
20 changes: 15 additions & 5 deletions tests/apps/flaskalino.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import Flask, redirect
app = Flask(__name__)
app.debug = False
app.use_reloader = False
Expand All @@ -11,24 +11,34 @@ def hello():
return "<center><h1>🐍 Hello Stan! 🦄</h1></center>"


@app.route("/301")
def threehundredone():
return redirect('/', code=301)


@app.route("/302")
def threehundredtwo():
return redirect('/', code=302)


@app.route("/400")
def fourhundred():
return "Bad Request", 400
return "Simulated Bad Request", 400


@app.route("/405")
def fourhundredfive():
return "Method not allowed", 405
return "Simulated Method not allowed", 405


@app.route("/500")
def fivehundred():
return "Internal Server Error", 500
return "Simulated Internal Server Error", 500


@app.route("/504")
def fivehundredfour():
return "Gateway Timeout", 504
return "Simulated Gateway Timeout", 504


@app.route("/exception")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_vanilla_eum_snippet():
assert eum_string.find(trace_id) != -1
assert eum_string.find(eum_api_key) != -1


def test_eum_snippet_with_meta():
meta_kvs = {}
meta_kvs['meta1'] = meta1
Expand All @@ -34,6 +35,7 @@ def test_eum_snippet_with_meta():
assert eum_string.find(meta2) != -1
assert eum_string.find(meta3) != -1


def test_eum_snippet_error():
meta_kvs = {}
meta_kvs['meta1'] = meta1
Expand All @@ -44,6 +46,7 @@ def test_eum_snippet_error():
eum_string = eum_snippet(eum_api_key=eum_api_key, meta=meta_kvs)
assert_equals('', eum_string)


def test_vanilla_eum_test_snippet():
eum_string = eum_test_snippet(trace_id=trace_id, eum_api_key=eum_api_key)
assert type(eum_string) is str
Expand All @@ -53,6 +56,7 @@ def test_vanilla_eum_test_snippet():
assert eum_string.find('reportingUrl') != -1
assert eum_string.find('//eum-test-fullstack-0-us-west-2.instana.io') != -1


def test_eum_test_snippet_with_meta():
meta_kvs = {}
meta_kvs['meta1'] = meta1
Expand All @@ -70,6 +74,7 @@ def test_eum_test_snippet_with_meta():
assert eum_string.find(meta2) != -1
assert eum_string.find(meta3) != -1


def test_eum_test_snippet_error():
meta_kvs = {}
meta_kvs['meta1'] = meta1
Expand Down
Loading