I am trying to send metrics in Python using OpenCensus and Azure Application Insights.
Ideally I would like to send some Python dictionaries with arbitrary structure, however it seems that OpenCensus is "automatically listening to logging/print statements", but I see no evidence of that on the Azure portal when I search for these things.
Is print(...)
somehow special to OpenCensus? How does that capture the content of print statements?
I've tried 2 different things (see below for the code):
- Sending "Azure metrics" (see https://pypi.org/project/opencensus-ext-azure/, then the "Metrics" paragraph): so far I don't see anything on the Azure Portal, clicking on my app for Application Insights. I've monitored the last 24 hours via the "Search" tab.
- Sending some sort of "spans" via the Azure implementation (see https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure#trace): when I click on the "Search" tab checking the last 24 hours, then I actually see some events in there representing the spans (by span name), but then no metrics attached e.g. that key/value attribute etc.
AFAIK as a principle:
- there should be a "tracer" that is managing a "span"
- there can be parent/child tracers/spans
- each span should allow to send to a "collector" some metrics (HTTP stuff, arbitrary dictionaries/JSON, etc.)
- there should be a dashboard (e.g. the Azure Application Insights) that should show these parent/child spans on the timeline with attached metrics/messages
I would like to understand:
- How can I send arbitrary dictionaries as "metrics" in OpenCensus? How that would show up on the Azure Portal when using an app for Application Insights?
- What's special with
print(...)
(orlogging.info(...)
) and HTTP requests in OpenCensus? How that information should be useful on the Azure Portal in the app for Application Insights? - Is the above somehow agnostic to tracers/spans, or is a span a must when in need to send a metric?
import json
import psutil
from opencensus.trace.samplers import AlwaysOnSampler
from opencensus.trace.tracer import Tracer
from opencensus.ext.azure import metrics_exporter
from opencensus.ext.azure.trace_exporter import AzureExporter
if __name__ == "__main__":
# loading the instrumentation key (for the Azure Application Insights app) from a JSON file
azure_conf = json.loads(open("tf/ai_details.json", 'r').read())
ai_instrumentation_key = azure_conf['instrumentation_key']['value']
# print(ai_instrumentation_key)
# test 1: trying to "send a metric", does that mean that the metric exporter listens to "print(...)"?
_me = metrics_exporter.new_metrics_exporter(connection_string='InstrumentationKey={}'.format(ai_instrumentation_key))
print(psutil.virtual_memory())
print("Done recording metrics")
# test 2: trying to "send a metric", how can I make the "span" to send a dictionary?
azure_exporter = AzureExporter(connection_string='InstrumentationKey={}'.format(ai_instrumentation_key))
# https://opencensus.io/api/python/trace/api/tracer.html
tracer = Tracer(exporter=azure_exporter, sampler=AlwaysOnSampler())
# https://opencensus.io/api/python/trace/api/span.html#opencensus.trace.span.Span
with tracer.span(name='TestSpan') as span:
print('Hello, World!') # is the span only listening to "print(...)"?
span.add_attribute("foo-span-key", "foo-span-value") # this does not seem to do anything