How to implement OpenCensus to send custom dimensions to AI in Python?

Published Mar 12 2021 12:32 AM 3,487 Views
Microsoft

With Python application, you can use OpenCensus to send the telemetry data. However, if your requirement is to use custom dimensions, you can use as below:

 

import logging
 
from opencensus.ext.azure.log_exporter import AzureLogHandler
 
logger = logging.getLogger(__name__)
# TODO: replace the all-zero GUID with your instrumentation key.
logger.addHandler(AzureLogHandler(
    connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
)
 
properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
 
# Use properties in logging statements
logger.warning('action', extra=properties)

 

 

In case, your requirement is to use correlation ID with custom dimensions, Function App does not have the feature available yet. The team is working on a solution to light up this feature.

Until then, use the example below and initialize the OpenCensus component with the correlation id in your function’s trigger.

 

import json
import logging
import requests
 
import azure.functions as func
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer
from opencensus.trace import execution_context
from opencensus.trace.propagation.trace_context_http_header_format import TraceContextPropagator
 
config_integration.trace_integrations(['requests'])
 
exporter = AzureExporter(instrumentation_key="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
 
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
 
    ## these below four lines
    span_context = TraceContextPropagator().from_headers({"traceparent": context.trace_context.Traceparent, "tracestate": context.trace_context.Tracestate})
    tracer = Tracer(span_context=span_context, exporter=exporter, sampler=ProbabilitySampler(1.0))
    execution_context.set_opencensus_tracer(tracer)  # < -- sets the passed in tracer as the current tracer
 
    with tracer.span("parent"):
        response = requests.get(url='http://example.com')
    return json.dumps({
        'method': req.method,
        'ctx_func_name': context.function_name,
        'ctx_func_dir': context.function_directory,
        'ctx_invocation_id': context.invocation_id,
        'ctx_trace_context_Traceparent': context.trace_context.Traceparent,
        'ctx_trace_context_Tracestate': context.trace_context.Tracestate,
    })

 

Also, check the OpenCensus Azure extension Repository: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure...

 

Hope this helps!

Shashank Ranjan

Azure App Services Support Engineering

%3CLINGO-SUB%20id%3D%22lingo-sub-2204634%22%20slang%3D%22en-US%22%3EHow%20to%20implement%20OpenCensus%20to%20send%20custom%20dimensions%20to%20AI%20in%20Python%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2204634%22%20slang%3D%22en-US%22%3E%3CP%3EWith%20Python%20application%2C%20you%20can%20use%20OpenCensus%20to%20send%20the%20telemetry%20data.%20However%2C%20if%20your%20requirement%20is%20to%20use%20custom%20dimensions%2C%20you%20can%20use%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22566%22%3E%3CH6%20id%3D%22toc-hId-901921529%22%20id%3D%22toc-hId-901920633%22%3Eimport%20logging%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--905532934%22%20id%3D%22toc-hId--905533830%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1581979899%22%20id%3D%22toc-hId-1581979003%22%3Efrom%20opencensus.ext.azure.log_exporter%20import%20AzureLogHandler%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--225474564%22%20id%3D%22toc-hId--225475460%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--2032929027%22%20id%3D%22toc-hId--2032929923%22%3Elogger%20%3D%20logging.getLogger(__name__)%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-454583806%22%20id%3D%22toc-hId-454582910%22%3E%23%20TODO%3A%20replace%20the%20all-zero%20GUID%20with%20your%20instrumentation%20key.%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1352870657%22%20id%3D%22toc-hId--1352871553%22%3Elogger.addHandler(AzureLogHandler(%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1134642176%22%20id%3D%22toc-hId-1134641280%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20connection_string%3D'InstrumentationKey%3D00000000-0000-0000-0000-000000000000')%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--672812287%22%20id%3D%22toc-hId--672813183%22%3E)%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1814700546%22%20id%3D%22toc-hId-1814699650%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-795217542%22%20id%3D%22toc-hId-795216646%22%3Eproperties%20%3D%20%7B'custom_dimensions'%3A%20%7B'key_1'%3A%20'value_1'%2C%20'key_2'%3A%20'value_2'%7D%7D%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1012236921%22%20id%3D%22toc-hId--1012237817%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1475275912%22%20id%3D%22toc-hId-1475275016%22%3E%23%20Use%20properties%20in%20logging%20statements%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--332178551%22%20id%3D%22toc-hId--332179447%22%3Elogger.warning('action'%2C%20extra%3Dproperties)%3C%2FH6%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20case%2C%20your%20requirement%20is%20to%20use%20correlation%20ID%20with%20custom%20dimensions%2C%20Function%20App%20does%20not%20have%20the%20feature%20available%20yet.%20The%20team%20is%20working%20on%20a%20solution%20to%20light%20up%20this%20feature.%3C%2FP%3E%0A%3CP%3EUntil%20then%2C%20use%20the%20example%20below%20%3CSPAN%3Eand%20initialize%20the%20OpenCensus%20component%20with%20the%20correlation%20id%20in%20your%20function%E2%80%99s%20trigger.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH6%20id%3D%22toc-hId--2139633014%22%20id%3D%22toc-hId--2139633910%22%3Eimport%26nbsp%3Bjson%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-347879819%22%20id%3D%22toc-hId-347878923%22%3Eimport%26nbsp%3Blogging%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1459574644%22%20id%3D%22toc-hId--1459575540%22%3Eimport%26nbsp%3Brequests%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1027938189%22%20id%3D%22toc-hId-1027937293%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--779516274%22%20id%3D%22toc-hId--779517170%22%3Eimport%26nbsp%3Bazure.functions%26nbsp%3Bas%26nbsp%3Bfunc%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1707996559%22%20id%3D%22toc-hId-1707995663%22%3Efrom%26nbsp%3Bopencensus.ext.azure.trace_exporter%26nbsp%3Bimport%26nbsp%3BAzureExporter%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-598704037%22%20id%3D%22toc-hId-598703141%22%3Efrom%26nbsp%3Bopencensus.trace%26nbsp%3Bimport%26nbsp%3Bconfig_integration%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1208750426%22%20id%3D%22toc-hId--1208751322%22%3Efrom%26nbsp%3Bopencensus.trace.samplers%26nbsp%3Bimport%26nbsp%3BProbabilitySampler%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1278762407%22%20id%3D%22toc-hId-1278761511%22%3Efrom%26nbsp%3Bopencensus.trace.tracer%26nbsp%3Bimport%26nbsp%3BTracer%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--528692056%22%20id%3D%22toc-hId--528692952%22%3Efrom%26nbsp%3Bopencensus.trace%26nbsp%3Bimport%26nbsp%3Bexecution_context%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1958820777%22%20id%3D%22toc-hId-1958819881%22%3Efrom%26nbsp%3Bopencensus.trace.propagation.trace_context_http_header_format%26nbsp%3Bimport%26nbsp%3BTraceContextPropagator%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-151366314%22%20id%3D%22toc-hId-151365418%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1656088149%22%20id%3D%22toc-hId--1656089045%22%3Econfig_integration.trace_integrations(%5B'requests'%5D)%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-831424684%22%20id%3D%22toc-hId-831423788%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--976029779%22%20id%3D%22toc-hId--976030675%22%3Eexporter%26nbsp%3B%3D%26nbsp%3BAzureExporter(instrumentation_key%3D%22aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee%22)%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1511483054%22%20id%3D%22toc-hId-1511482158%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-402190532%22%20id%3D%22toc-hId-402189636%22%3Edef%26nbsp%3Bmain(req%3A%26nbsp%3Bfunc.HttpRequest%2C%26nbsp%3Bcontext%3A%26nbsp%3Bfunc.Context)%26nbsp%3B-%26gt%3B%26nbsp%3Bfunc.HttpResponse%3A%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1405263931%22%20id%3D%22toc-hId--1405264827%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1082248902%22%20id%3D%22toc-hId-1082248006%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23%23%26nbsp%3Bthese%26nbsp%3Bbelow%26nbsp%3Bfour%26nbsp%3Blines%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--725205561%22%20id%3D%22toc-hId--725206457%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bspan_context%26nbsp%3B%3D%26nbsp%3BTraceContextPropagator().from_headers(%7B%22traceparent%22%3A%26nbsp%3Bcontext.trace_context.Traceparent%2C%26nbsp%3B%22tracestate%22%3A%26nbsp%3Bcontext.trace_context.Tracestate%7D)%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1762307272%22%20id%3D%22toc-hId-1762306376%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Btracer%26nbsp%3B%3D%26nbsp%3BTracer(span_context%3Dspan_context%2C%26nbsp%3Bexporter%3Dexporter%2C%26nbsp%3Bsampler%3DProbabilitySampler(1.0))%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--45147191%22%20id%3D%22toc-hId--45148087%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bexecution_context.set_opencensus_tracer(tracer)%26nbsp%3B%26nbsp%3B%23%26nbsp%3B%26lt%3B%26nbsp%3B--%26nbsp%3Bsets%26nbsp%3Bthe%26nbsp%3Bpassed%26nbsp%3Bin%26nbsp%3Btracer%26nbsp%3Bas%26nbsp%3Bthe%26nbsp%3Bcurrent%26nbsp%3Btracer%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1852601654%22%20id%3D%22toc-hId--1852602550%22%3E%26nbsp%3B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-634911179%22%20id%3D%22toc-hId-634910283%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bwith%26nbsp%3Btracer.span(%22parent%22)%3A%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1172543284%22%20id%3D%22toc-hId--1172544180%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bresponse%26nbsp%3B%3D%26nbsp%3Brequests.get(url%3D'%3CA%20href%3D%22http%3A%2F%2Fexample.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttp%3A%2F%2Fexample.com%3C%2FA%3E')%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1314969549%22%20id%3D%22toc-hId-1314968653%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Breturn%26nbsp%3Bjson.dumps(%7B%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-205677027%22%20id%3D%22toc-hId-205676131%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B'method'%3A%26nbsp%3Breq.method%2C%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--1601777436%22%20id%3D%22toc-hId--1601778332%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B'ctx_func_name'%3A%26nbsp%3Bcontext.function_name%2C%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-885735397%22%20id%3D%22toc-hId-885734501%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B'ctx_func_dir'%3A%26nbsp%3Bcontext.function_directory%2C%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--921719066%22%20id%3D%22toc-hId--921719962%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B'ctx_invocation_id'%3A%26nbsp%3Bcontext.invocation_id%2C%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId-1565793767%22%20id%3D%22toc-hId-1565792871%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B'ctx_trace_context_Traceparent'%3A%26nbsp%3Bcontext.trace_context.Traceparent%2C%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--241660696%22%20id%3D%22toc-hId--241661592%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B'ctx_trace_context_Tracestate'%3A%26nbsp%3Bcontext.trace_context.Tracestate%2C%3C%2FH6%3E%0A%3CH6%20id%3D%22toc-hId--2049115159%22%20id%3D%22toc-hId--2049116055%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D)%3C%2FH6%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20check%20the%20OpenCensus%20Azure%20extension%20Repository%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fgithub.com%252Fcensus-instrumentation%252Fopencensus-python%252Ftree%252Fmaster%252Fcontrib%252Fopencensus-ext-azure%2523installation%26amp%3Bdata%3D04%257C01%257CGaurav.Patole%2540microsoft.com%257C46a8a6e1deb54c9490d408d8e5324f6a%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C637511351530876486%257CUnknown%257CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%253D%257C1000%26amp%3Bsdata%3DMLkBAIFJ61BmX9uK7lfQO%252Fodse2jI%252FLTfdsxO3ES6ik%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fcensus-instrumentation%2Fopencensus-python%2Ftree%2Fmaster%2Fcontrib%2Fopencensus-ext-azure%23installation%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20this%20helps!%3C%2FP%3E%0A%3CP%3EShashank%20Ranjan%3C%2FP%3E%0A%3CP%3EAzure%20App%20Services%20Support%20Engineering%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2204634%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20Monitor%20supports%20distributed%20tracing%2C%20metric%20collection%2C%20and%20logging%20of%20Python%20applications%20through%20integration%20with%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fopencensus.io%2F%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EOpenCensus%3C%2FA%3E%3C%2FSPAN%3E.%3C%2FP%3E%0A%3CP%3EThe%20SDK%20uses%20three%20Azure%20Monitor%20exporters%20to%20send%20different%20types%20of%20telemetry%20to%20Azure%20Monitor.%20They're%20trace%2C%20metrics%2C%20and%20logs.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAccess%20to%20the%20Azure%20Functions%20runtime%20logger%20is%20available%20via%20a%20root%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Flogging.html%23module-logging%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Elogging%3C%2FA%3E%3C%2FSPAN%3E%26nbsp%3Bhandler%20in%20your%20function%20app.%20This%20logger%20is%20tied%20to%20Application%20Insights%20and%20allows%20you%20to%20flag%20warnings%20and%20errors%20encountered%20during%20the%20function%20execution.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20more%20details%20follow%20%E2%80%93%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Fapp%2Fopencensus-python%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Fapp%2Fopencensus-python%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-reference-python%23logging%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-reference-python%23logging%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EOpenCensus%20Azure%20extension%20Repository%20-%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcensus-instrumentation%2Fopencensus-python%2Ftree%2Fmaster%2Fcontrib%2Fopencensus-ext-azure%23installation%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fgithub.com%2Fcensus-instrumentation%2Fopencensus-python%2Ftree%2Fmaster%2Fcontrib%2Fopencensus-ext-azure%23installation%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2204634%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPython%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Mar 12 2021 01:23 AM
Updated by: