Reputation: 4891
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):
AFAIK as a principle:
I would like to understand:
print(...)
(or logging.info(...)
) and HTTP requests in OpenCensus? How that information should be useful on the Azure Portal in the app for Application Insights?
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
Upvotes: 2
Views: 3228
Reputation: 69
Thanks for using OpenCensus with Azure Monitor! You can find my answers in-line.
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 do you mean by arbitrary dictionaries? In your code snippet, it seems like you want to send individual data points as metric data to the Azure Monitor backend (Application Insights). For steps on how to get started with OpenCensus, take a look at the overview page on the Microsoft website. This will show you how to instrument your application properly with OpenCensus, and how to send your telemetry to Azure Monitor. If you are still confused at how to instrument your application to meet your business use case, here are some more examples you can take a look at.
What's special with print(...) (or logging.info(...)) and HTTP requests in OpenCensus? How that information should be useful on the Azure Portal in the app for Application Insights?
The print
command has no special implications in OpenCensus. For logs, you can automatically send logging telemetry from the Python standard logging library if you instrument with OpenCensus Azure Monitor and utilize the logging exporter. For HTTP requests, you can track incoming requests and outgoing requests with the trace exporter and various OpenCensus library integrations depending on what library you want to track telemetry for.
Is the above somehow agnostic to tracers/spans, or is a span a must when in need to send a metric?
Spans are a concept only used in tracing (using AzureExporter). You do not need to create spans to send metric data (metrics exporter). Take a look at the above links to see how to use each of these.
Upvotes: 2