Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
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
10 changes: 5 additions & 5 deletions azure_monitor/examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.ext import http_requests
from opentelemetry.sdk.trace import Tracer
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_preferred_tracer_implementation(lambda T: Tracer())
tracer = trace.tracer()
http_requests.enable(tracer)
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
tracer = trace.tracer_source().get_tracer(__name__)
http_requests.enable(trace.tracer_source())
span_processor = BatchExportSpanProcessor(
AzureMonitorSpanExporter(instrumentation_key="<INSTRUMENTATION KEY HERE>")
)
trace.tracer().add_span_processor(span_processor)
trace.tracer_source().add_span_processor(span_processor)

response = requests.get(url="http://127.0.0.1:8080/")
span_processor.shutdown()
11 changes: 6 additions & 5 deletions azure_monitor/examples/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.ext import http_requests
from opentelemetry.sdk.trace import Tracer
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

trace.set_preferred_tracer_implementation(Tracer)
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())

http_requests.enable(trace.tracer())
http_requests.enable(trace.tracer_source())
span_processor = SimpleExportSpanProcessor(
AzureMonitorSpanExporter(instrumentation_key="<INSTRUMENTATION KEY HERE>")
)
trace.tracer().add_span_processor(span_processor)
trace.tracer_source().add_span_processor(span_processor)
tracer = trace.tracer_source().get_tracer(__name__)

with trace.tracer().start_as_current_span("parent"):
with tracer.start_as_current_span("parent"):
response = requests.get("<URL HERE>", timeout=5)

23 changes: 15 additions & 8 deletions azure_monitor/examples/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@
from opentelemetry import trace
from opentelemetry.ext import http_requests
from opentelemetry.ext.wsgi import OpenTelemetryMiddleware
from opentelemetry.sdk.trace import Tracer
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_preferred_tracer_implementation(lambda T: Tracer())
# The preferred tracer implementation must be set, as the opentelemetry-api
# defines the interface with a no-op implementation.
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
tracer = trace.tracer_source().get_tracer(__name__)

http_requests.enable(trace.tracer())
span_processor = BatchExportSpanProcessor(
AzureMonitorSpanExporter(instrumentation_key="<INSTRUMENTATION KEY HERE>")
)
trace.tracer().add_span_processor(span_processor)
exporter = AzureMonitorSpanExporter(instrumentation_key="<INSTRUMENTATION KEY HERE>")

# SpanExporter receives the spans and send them to the target location.
span_processor = BatchExportSpanProcessor(exporter)
trace.tracer_source().add_span_processor(span_processor)

# Integrations are the glue that binds the OpenTelemetry API and the
# frameworks and libraries that are used together, automatically creating
# Spans and propagating context as appropriate.
http_requests.enable(trace.tracer_source())
app = flask.Flask(__name__)
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)


@app.route("/")
def hello():
with trace.tracer().start_as_current_span("parent"):
with tracer.start_as_current_span("parent"):
requests.get("https://www.wikipedia.org/wiki/Rabbit")
return "hello"

Expand Down
8 changes: 4 additions & 4 deletions azure_monitor/examples/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Licensed under the MIT License.
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace import Tracer
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

trace.set_preferred_tracer_implementation(lambda T: Tracer())
tracer = trace.tracer()
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
span_processor = SimpleExportSpanProcessor(
AzureMonitorSpanExporter(instrumentation_key="<INSTRUMENTATION KEY HERE>")
)
trace.tracer().add_span_processor(span_processor)
trace.tracer_source().add_span_processor(span_processor)
tracer = trace.tracer_source().get_tracer(__name__)

with tracer.start_as_current_span("hello") as span:
print("Hello, World!")
34 changes: 22 additions & 12 deletions azure_monitor/src/azure_monitor/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
from opentelemetry.sdk.util import ns_to_iso_str
from opentelemetry.trace import Span, SpanKind
from opentelemetry.trace.status import StatusCanonicalCode

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -78,16 +79,16 @@ def span_to_envelope(self, span): # noqa pylint: disable=too-many-branches
if parent:
envelope.tags[
"ai.operation.parentId"
] = "|{:032x}.{:016x}.".format(parent.trace_id, parent.span_id)
] = "{:016x}".format(parent.span_id)
if span.kind in (SpanKind.CONSUMER, SpanKind.SERVER):
envelope.name = "Microsoft.ApplicationInsights.Request"
data = protocol.Request(
id="|{:032x}.{:016x}.".format(
span.context.trace_id, span.context.span_id
id="{:016x}".format(
span.context.span_id
),
duration=utils.ns_to_duration(span.end_time - span.start_time),
responseCode="0",
success=False,
responseCode=str(span.status.value),
success=False, # Modify based off attributes or Status
properties={},
)
envelope.data = protocol.Data(
Expand All @@ -104,16 +105,18 @@ def span_to_envelope(self, span): # noqa pylint: disable=too-many-branches
status_code = span.attributes["http.status_code"]
data.responseCode = str(status_code)
data.success = 200 <= status_code < 400
elif span.status == StatusCanonicalCode.OK:
data.success = True
else:
envelope.name = "Microsoft.ApplicationInsights.RemoteDependency"
data = protocol.RemoteDependency(
name=span.name,
id="|{:032x}.{:016x}.".format(
span.context.trace_id, span.context.span_id
id="{:016x}".format(
span.context.span_id
),
resultCode="0", # TODO
resultCode=str(span.status.value),
duration=utils.ns_to_duration(span.end_time - span.start_time),
success=True, # TODO
success=False, # Modify based off attributes or Status
properties={},
)
envelope.data = protocol.Data(
Expand All @@ -136,10 +139,17 @@ def span_to_envelope(self, span): # noqa pylint: disable=too-many-branches
data.name = span.attributes["http.method"] \
+ "/" + parse_url.path
if "http.status_code" in span.attributes:
data.resultCode = str(span.attributes["http.status_code"])
status_code = span.attributes["http.status_code"]
data.resultCode = str(status_code)
data.success = 200 <= status_code < 400
elif span.status == StatusCanonicalCode.OK:
data.success = True
else: # SpanKind.INTERNAL
data.type = "InProc"
for key in span.attributes:
# This removes redundant data from ApplicationInsights
if key.startswith('http.'):
continue
data.properties[key] = span.attributes[key]
if span.links:
links = []
Expand All @@ -149,8 +159,8 @@ def span_to_envelope(self, span): # noqa pylint: disable=too-many-branches
"operation_Id": "{:032x}".format(
link.context.trace_id
),
"id": "|{:032x}.{:016x}.".format(
link.context.trace_id, link.context.span_id
"id": "{:016x}".format(
link.context.span_id
),
}
)
Expand Down
Loading