%3CLINGO-SUB%20id%3D%22lingo-sub-1247041%22%20slang%3D%22en-US%22%3EEnabling%20distributed%20tracing%20with%20the%20Azure%20SDK%20for%20Python%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1247041%22%20slang%3D%22en-US%22%3E%3CP%3ETodays%20topic%20is%20diagnostics%20with%20Python%20applications.%20I%20have%20a%20fairly%20complex%20application%2C%20and%20something%20is%20going%20wrong.%20Azure%20provides%20a%20capability%20within%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fmonitor%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Monitor%3C%2FA%3E%20called%20transaction%20monitoring%20that%20tracks%20a%20transaction%20(such%20as%20an%20API%20call)%20from%20your%20application%20all%20the%20way%20through%20to%20the%20service%20that%20fulfills%20the%20request.%20This%20is%20powered%20by%20%3CA%20href%3D%22https%3A%2F%2Fopentelemtry.io%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOpenTelemetry%3C%2FA%3E%2C%20a%20distributed%20tracing%20framework.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20always%2C%20when%20learning%20a%20new%20technology%2C%20I%20simplify%20the%20application%20so%20I%20can%20see%20what%20is%20going%20on.%20Here%20is%20a%20really%20simple%20application%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20os%0Afrom%20azure.identity%20import%20DefaultAzureCredential%0Afrom%20azure.core.exceptions%20import%20ResourceExistsError%0Afrom%20azure.storage.blob%20import%20BlobServiceClient%0A%0Adef%20upload_to(account_url%2C%20container_name%2C%20blob_name)%3A%0A%20%20%23%20Create%20a%20client%20for%20this%20request%0A%20%20client%20%3D%20BlobServiceClient(account_url%2C%20credential%3DDefaultAzureCredential())%0A%0A%20%20%23%20Create%20a%20client%20for%20the%20named%20container%0A%20%20container_client%20%3D%20client.get_container_client(container_name)%0A%20%20try%3A%0A%20%20%20%20container_client.create_container()%0A%20%20%20%20print(%22Container%20created%22)%0A%20%20except%20ResourceExistsError%3A%0A%20%20%20%20print(%22Container%20already%20exists%20(ignored).%22)%0A%0A%20%20%23%20Create%20a%20client%20for%20the%20named%20blob%20and%20upload%20data%0A%20%20blob_client%20%3D%20container_client.get_blob_client(blob_name)%0A%20%20with%20open(blob_name%2C%20%22rb%22)%20as%20data%3A%0A%20%20%20%20blob_client.upload_blob(data%2C%20overwrite%3DTrue)%0A%0A%23%20Upload%20main.py%20to%20the%20container%0Astorage_url%3Dos.environ%5B%22APP_STORAGE_ACCOUNT_URL%22%5D%0Aupload_to(storage_url%2C%20%22demo%22%2C%20%22main.py%22)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EThis%20simple%20console%20application%20creates%20a%20container%20within%20the%20storage%20account%2C%20then%20uploads%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3Emain.py%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bfile%20(which%20contains%20the%20source%20code%20to%20the%20app)%20as%20a%20blob.%20To%20support%20this%2C%20I%E2%80%99ve%20created%20a%20resource%20group%2C%20a%20storage%20account%2C%20and%20a%20service%20principal%20with%20permissions%20to%20access%20the%20storage%20account%20using%20the%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Fget-started-with-azure-cli%3Fview%3Dazure-cli-latest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20CLI%3C%2FA%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eaz%20group%20create%20%5C%0A%20%20--name%20python-demoapp%20%5C%0A%20%20--location%20westus%0Aaz%20storage%20account%20create%20%5C%0A%20%20--name%20pythondemoapp03242020%20%5C%0A%20%20--resource-group%20python-demoapp%20%5C%0A%20%20--location%20westus%20%5C%0A%20%20--sku%20Standard_LRS%20%5C%0A%20%20--kind%20StorageV2%0Aaz%20ad%20sp%20create-for-rbac%20%5C%0A%20%20--name%20http%3A%2F%2Fpythondemoapp03242020%20%5C%0A%20%20--role%20%22Storage%20Blob%20Data%20Contributor%22%20%5C%0A%20%20--scopes%20%2Fsubscriptions%2F%3CSUBSCRIPTION%3E%2FresourceGroups%2Fpython-demoapp%3C%2FSUBSCRIPTION%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EThe%20storage%20account%20name%20must%20be%20unique%20across%20Azure%2C%20so%20pick%20a%20good%20name!%20Replace%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3ESUBSCRIPTION%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bwith%20your%20Subscription%20ID.%20You%20can%20use%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3Eaz%20account%20show%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bcommand%20to%20see%20the%20subscription.%20You%20can%20then%20set%20some%20environment%20variables%20based%20on%20the%20output%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eexport%20AZURE_TENANT_ID%3D%22%3CCREATE-FOR-RBAC%20output%3D%22%22%20tenant%3D%22%22%3E%22%0Aexport%20AZURE_CLIENT_ID%3D%22%3CCREATE-FOR-RBAC%20output%3D%22%22%20appid%3D%22%22%3E%22%0Aexport%20AZURE_CLIENT_SECRET%3D%22%3CCREATE-FOR-RBAC%20output%3D%22%22%20password%3D%22%22%3E%22%0Aexport%20APP_STORAGE_ACCOUNT_URL%3D%22%3CSTORAGE-CREATE%20output%3D%22%22%20primaryendpoints.blob%3D%22%22%3E%22%3C%2FSTORAGE-CREATE%3E%3C%2FCREATE-FOR-RBAC%3E%3C%2FCREATE-FOR-RBAC%3E%3C%2FCREATE-FOR-RBAC%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWhen%20you%20run%20the%20application%2C%20you%20will%20likely%20see%20a%20one%20liner%20output%20that%20shows%20that%20the%20container%20is%20created%20or%20not.%20If%20there%20is%20an%20error%2C%20it%20will%20be%20displayed%20because%20of%20exception%20handling.%20However%2C%20you%20probably%20won%E2%80%99t%20know%20where%20the%20application%20is%20breaking.%20There%20is%20a%20more%20insidious%20error%2C%20though.%20What%20happens%20if%20the%20container%20create%20call%20takes%20a%20long%20time%3F%20How%20would%20you%20know%20it%20was%20that%20call%20and%20not%20the%20upload%2C%20or%20some%20of%20your%20other%20logic%2C%20for%20example%3F%3C%2FP%3E%0A%3CH2%20id%3D%22basic-logging%22%20id%3D%22toc-hId--1383934487%22%20id%3D%22toc-hId--1383868144%22%3EBasic%20logging%3C%2FH2%3E%0A%3CP%3EThe%20Azure%20SDK%20for%20Python%20uses%20Python%20logging%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20logging%0A%0Alogging.basicConfig()%0Aazure_root_logger%20%3D%20logging.getLogger('azure')%0Aazure_root_logger.setLevel(logging.DEBUG)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThis%20will%20print%20the%20request%20and%20response.%20Information%20that%20may%20have%20PII%20is%20redacted%2C%20but%20otherwise%20you%20get%20all%20the%20headers%20along%20with%20the%20request%20and%20response.%20This%20allows%20you%20to%20see%20if%20the%20SDK%20is%20doing%20the%20right%20thing%2C%20and%20it%20will%20also%20allow%20you%20to%20assure%20yourself%20that%20your%20code%20is%20handling%20the%20responses%20correctly%2C%20especially%20when%20augmented%20by%20logging%20of%20your%20own.%3C%2FP%3E%0A%3CH2%20id%3D%22distributed-tracing%22%20id%3D%22toc-hId-1103578346%22%20id%3D%22toc-hId-1103644689%22%3EDistributed%20tracing%3C%2FH2%3E%0A%3CP%3EWhen%20you%20are%20looking%20at%20why%20a%20particular%20network%20operation%20is%20slow%2C%20logging%20alone%20won%E2%80%99t%20help%20you.%20You%20need%20to%20be%20able%20to%20trace%20the%20entire%20request%20from%20beginning%20to%20end%2C%20including%20%E2%80%9Cacross%20the%20wire%E2%80%9D%20to%20the%20service.%20Consider%20a%20typical%20application%20has%20the%20application%2C%20a%20piece%20of%20middleware%20that%20implements%20a%20REST%20or%20GraphQL%20endpoint%2C%20and%20then%20a%20database%20connection.%20Which%20bit%20of%20the%20infrastructure%20is%20slow%20gets%20really%20complicated.%20As%20the%20infrastructure%20becomes%20more%20complex%2C%20so%20does%20the%20difficulty%20of%20figuring%20out%20which%20piece%20of%20the%20infrastructure%20is%20having%20issues.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnter%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fopentelemetry.io%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOpenTelemetry%3C%2FA%3E.%20OpenTelemetry%20is%20an%20open%20standard%20that%20allows%20you%20to%20trace%20requests%20across%20your%20infrastructure.%20It%20passes%20headers%20between%20the%20various%20components%20to%20move%20the%20request%20ID%20through%20the%20system.%20Let%E2%80%99s%20start%20by%20instrumenting%20the%20application.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CSTRONG%3EWARNING%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CSPAN%3EThe%20OpenTelemetry%20library%20is%20in%20preview%20right%20now%2C%20and%20the%20API%20is%20changing%20with%20new%20versions.%20Ensure%20you%20are%20using%20an%20API%2FSDK%20version%20that%20is%20compatible%20with%20your%20exporter%20and%20plugins.%20Before%20you%20use%20preview%20packages%2C%20create%20and%20activate%20a%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.python.org%2F3%2Ftutorial%2Fvenv.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Evirtual%20environment%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bfor%20your%20work.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFirst%2C%20we%20need%20some%20libraries%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-bash%22%20data-lang%3D%22bash%22%3Epip%20%3CSPAN%20class%3D%22nb%22%3Einstall%20%3C%2FSPAN%3Eopentelemetry-api%3CSPAN%20class%3D%22o%22%3E%3D%3D%3C%2FSPAN%3E0.4a0%0Apip%20%3CSPAN%20class%3D%22nb%22%3Einstall%20%3C%2FSPAN%3Eopentelemetry-sdk%3CSPAN%20class%3D%22o%22%3E%3D%3D%3C%2FSPAN%3E0.4a0%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3CSPAN%3EWe%20are%20pinning%20the%20version%20of%20the%20OpenTelemetry%20SDK%20here%20so%20that%20the%20instructions%20work%20properly.%20The%20API%20for%20OpenTelemetry%20is%20changing%2C%20so%20make%20sure%20you%20follow%20the%20appropriate%20instructions.%20You%20can%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fopen-telemetry%2Fopentelemetry-python%2Ftree%2Fv0.4a.x%2Fdocs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Efind%20the%20documentation%20for%20this%20version%20on%20GitHub%3C%2FA%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EUpdate%20the%20app%20to%20support%20basic%20tracing%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20os%0Afrom%20azure.identity%20import%20DefaultAzureCredential%0Afrom%20azure.core.exceptions%20import%20ResourceExistsError%0Afrom%20azure.storage.blob%20import%20BlobServiceClient%0Afrom%20opentelemetry%20import%20trace%0Afrom%20opentelemetry.sdk.trace%20import%20TracerSource%0Afrom%20opentelemetry.sdk.trace.export%20import%20SimpleExportSpanProcessor%0A%0A%23%20Configure%20OpenTelemetry%20Exporter%0Afrom%20opentelemetry.sdk.trace.export%20import%20ConsoleSpanExporter%0Aexporter%20%3D%20ConsoleSpanExporter()%0A%0A%23%20Configure%20OpenTelemetry%20Tracing%0Atrace.set_preferred_tracer_source_implementation(lambda%20T%3A%20TracerSource())%0Atrace.tracer_source().add_span_processor(%0A%20%20%20%20SimpleExportSpanProcessor(exporter)%0A)%0Atracer%20%3D%20trace.get_tracer(__name__)%0A%0Adef%20upload_to(account_url%2C%20container_name%2C%20blob_name)%3A%0A%20%20with%20tracer.start_as_current_span(%22upload_to%22)%3A%0A%20%20%20%20%23%20Create%20a%20client%20for%20this%20request%0A%20%20%20%20client%20%3D%20BlobServiceClient(account_url%2C%20credential%3DDefaultAzureCredential())%0A%0A%20%20%20%20%23%20Create%20a%20client%20for%20the%20named%20container%0A%20%20%20%20container_client%20%3D%20client.get_container_client(container_name)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20container_client.create_container()%0A%20%20%20%20%20%20print(%22Container%20created%22)%0A%20%20%20%20except%20ResourceExistsError%3A%0A%20%20%20%20%20%20print(%22Container%20already%20exists%20(ignored).%22)%0A%0A%20%20%20%20%23%20Create%20a%20client%20for%20the%20named%20blob%0A%20%20%20%20blob_client%20%3D%20container_client.get_blob_client(blob_name)%0A%20%20%20%20with%20open(blob_name%2C%20%22rb%22)%20as%20data%3A%0A%20%20%20%20%20%20blob_client.upload_blob(data%2C%20overwrite%3DTrue)%0A%0A%23%20Upload%20main.py%20to%20the%20container%0Awith%20tracer.start_as_current_span(%22mainapp%22)%3A%0A%20%20storage_url%3Dos.environ%5B'APP_STORAGE_ACCOUNT_URL'%5D%0A%20%20upload_to(storage_url%2C%20%22demo%22%2C%20%22main.py%22)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EFirst%2C%20you%20need%20to%20decide%20where%20you%20are%20going%20to%20put%20the%20trace%20information%20you%20collect.%20This%20is%20known%20as%20an%20Exporter.%20There%20are%20several%20good%20ones%20(and%20we%E2%80%99ll%20get%20onto%20that%20in%20a%20moment)%2C%20but%2C%20for%20now%2C%20we%20are%20just%20sending%20the%20trace%20information%20to%20the%20console.%20We%20can%20then%20configure%20the%20rest%20of%20the%20OpenTelemetry%20system.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you%20have%20the%20OpenTelemetry%20library%20configured%2C%20just%20wrap%20each%20of%20the%20blocks%20you%20want%20to%20group%20together%20with%20a%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3Ewith%20tracer.start_as_current_span(name)%3C%2FCODE%3E.%20This%20creates%20a%20unique%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3ESpan%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E-%20a%20block%20of%20operations%20with%20a%20start%20and%20an%20end%20that%20you%20can%20time%20individually.%20I%E2%80%99ve%20got%20one%20span%20for%20the%20main%20application%2C%20and%20another%20(inner)%20span%20for%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3Eupload_to%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERunning%20the%20app%2C%20this%20is%20what%20the%20output%20looks%20like%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%24%20python%20main.py%0AContainer%20already%20exists%20(ignored).%0ASpan(name%3D%22upload_to%22%2C%20context%3DSpanContext(trace_id%3D0x55dfc91e9feb3a56df74d9add43038f2%2C%20span_id%3D0x8dab600f714bfb13%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.INTERNAL%2C%20parent%3DSpan(name%3D%22mainapp%22%2C%20context%3DSpanContext(trace_id%3D0x55dfc91e9feb3a56df74d9add43038f2%2C%20span_id%3D0xb2a773d445c6237b%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A40%3A29.285159Z%2C%20end_time%3D2020-03-19T21%3A40%3A30.060733Z)Span(name%3D%22mainapp%22%2C%20context%3DSpanContext(trace_id%3D0x55dfc91e9feb3a56df74d9add43038f2%2C%20span_id%3D0xb2a773d445c6237b%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.INTERNAL%2C%20parent%3DNone%2C%20start_time%3D2020-03-19T21%3A40%3A29.285159Z%2C%20end_time%3D2020-03-19T21%3A40%3A30.061730Z)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EYou%20can%20see%20two%20spans%20-%20one%20inside%20the%20other%20-%20with%20the%20appropriate%20names.%3C%2FP%3E%0A%3CH3%20id%3D%22enable-distributed-tracing-in-the-azure-sdk%22%20id%3D%22toc-hId-1794139820%22%20id%3D%22toc-hId-1794206163%22%3EEnable%20distributed%20tracing%20in%20the%20Azure%20SDK%3C%2FH3%3E%0A%3CP%3ETo%20enable%20distributed%20tracing%20within%20the%20Azure%20SDK%2C%20you%20need%20to%20tell%20Azure%20Core%20which%20tracing%20library%20you%20are%20using.%20There%20is%20a%20tracing%20implementation%20for%20the%20older%20OpenCensus%20and%20the%20newer%20OpenTelemetry.%20You%20could%20also%20write%20your%20own%2C%20although%20that%20would%20be%20more%20work.%20Install%20the%20preview%20tracing%20package%20for%20the%20Azure%20SDK%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Epip%20install%20azure-core-tracing-opentelemetry%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EAdd%20the%20following%20three%20lines%20to%20the%20start%20of%20your%20application%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20Configure%20distributed%20tracing%20in%20Azure%20SDK%0Afrom%20azure.core.settings%20import%20settings%0Afrom%20azure.core.tracing.ext.opentelemetry_span%20import%20OpenTelemetrySpan%0Asettings.tracing_implementation%20%3D%20OpenTelemetrySpan%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3ERun%20the%20app%20again%20to%20see%20the%20results%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ESpan(name%3D%22%2Fxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx%2Foauth2%2Fv2.0%2Ftoken%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x2e3ee534daffef73%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.CLIENT%2C%20parent%3DSpan(name%3D%22ContainerClient.create_container%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x78c07183a6860ff7%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A43%3A33.383835Z%2C%20end_time%3D2020-03-19T21%3A43%3A33.725025Z)Span(name%3D%22%2Fdemo%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x6c6e9f9ce09e3c03%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.CLIENT%2C%20parent%3DSpan(name%3D%22ContainerClient.create_container%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x78c07183a6860ff7%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A43%3A33.726025Z%2C%20end_time%3D2020-03-19T21%3A43%3A34.035472Z)Span(name%3D%22ContainerClient.create_container%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x78c07183a6860ff7%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.INTERNAL%2C%20parent%3DSpan(name%3D%22upload_to%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x3e61cb726380e524%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A43%3A33.360182Z%2C%20end_time%3D2020-03-19T21%3A43%3A34.051102Z)Container%20already%20exists%20(ignored).%0ASpan(name%3D%22%2Fdemo%2Fmain.py%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x6d60987367993bab%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.CLIENT%2C%20parent%3DSpan(name%3D%22BlobClient.upload_blob%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x52f5313f59b27526%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A43%3A34.095871Z%2C%20end_time%3D2020-03-19T21%3A43%3A34.139872Z)Span(name%3D%22BlobClient.upload_blob%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x52f5313f59b27526%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.INTERNAL%2C%20parent%3DSpan(name%3D%22upload_to%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x3e61cb726380e524%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A43%3A34.093871Z%2C%20end_time%3D2020-03-19T21%3A43%3A34.145869Z)Span(name%3D%22upload_to%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x3e61cb726380e524%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.INTERNAL%2C%20parent%3DSpan(name%3D%22mainapp%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x1f1c8cafdd0189ea%2C%20trace_state%3D%7B%7D))%2C%20start_time%3D2020-03-19T21%3A43%3A33.328939Z%2C%20end_time%3D2020-03-19T21%3A43%3A34.145869Z)Span(name%3D%22mainapp%22%2C%20context%3DSpanContext(trace_id%3D0x48776c79e95d494e1b24353ba9046b29%2C%20span_id%3D0x1f1c8cafdd0189ea%2C%20trace_state%3D%7B%7D)%2C%20kind%3DSpanKind.INTERNAL%2C%20parent%3DNone%2C%20start_time%3D2020-03-19T21%3A43%3A33.328939Z%2C%20end_time%3D2020-03-19T21%3A43%3A34.148870Z)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWe%20received%20two%20lines%20of%20output.%20One%20of%20the%20lines%20is%20a%20forked%20trace%20purely%20for%20the%20authentication%20flow.%20The%20second%20one%20is%20for%20the%20main%20application.%26nbsp%3B%20Now%20we%20can%20see%20what%20is%20going%20on%20%22under%20the%20covers%22%20of%20the%20Azure%20SDK.%3C%2FP%3E%0A%3CH3%20id%3D%22send-the-traces-to-azure-monitor%22%20id%3D%22toc-hId--13314643%22%20id%3D%22toc-hId--13248300%22%3ESend%20the%20traces%20to%20Azure%20Monitor%3C%2FH3%3E%0A%3CP%3EOf%20course%2C%20you%20don%E2%80%99t%20always%20get%20to%20see%20the%20application%20when%20it%20is%20running.%20In%20fact%2C%20it%E2%80%99s%20rare%20that%20you%20can%20run%20an%20application%20on%20the%20console.%20Dumping%20the%20spans%20to%20the%20console%20is%20probably%20not%20the%20best%20idea.%20Visually%2C%20you%20can%20also%20use%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fzipkin.io%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EZipkin%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eor%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.jaegertracing.io%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EJaeger%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eto%20visualize%20the%20traces%20during%20development.%20For%20production%20apps%2C%20you%20want%20to%20integrate%20with%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fmonitor%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Monitor%3C%2FA%3E%2C%20which%20provides%20an%20integration%20point%20for%20your%20logs%2C%20metrics%2C%20alerts%2C%20and%20other%20production%20needs.%20First%2C%20create%20an%20Application%20Insights%20resource%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eaz%20extension%20add%20-n%20application-insights%0Aaz%20monitor%20app-insights%20component%20create%20%5C%0A%20%20--app%20pythondemoapp03242020%20%5C%0A%20%20--location%20westus%20%5C%0A%20%20--kind%20web%20%5C%0A%20%20-g%20python-demoapp%20%5C%0A%20%20--application-type%20web%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EThis%20will%20output%20the%20instrumentation%20key%2C%20which%20you%20need%20to%20place%20in%20an%20environment%20variable%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eexport%20APPINSIGHTS_INSTRUMENTATION_KEY%3D%22%3COUTPUT%20instrumentationkey%3D%22%22%3E%22%3C%2FOUTPUT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EReplace%20the%20exporter%20in%20your%20code%20with%20the%20following%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Efrom%20azure_monitor%20import%20AzureMonitorSpanExporter%0Aexporter%20%3D%20AzureMonitorSpanExporter(%0A%20%20instrumentation_key%20%3D%20os.environ%5B'APPINSIGHTS_INSTRUMENTATION_KEY'%5D%0A)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3ERun%20your%20code.%20You%20can%20now%20view%20the%20application%20map%2C%20along%20with%20all%20the%20resources%20it%20connects%20to%20within%20Azure%20Monitor.%20The%20traces%20also%20show%20up%20in%20the%20dependencies%20logs%2C%20which%20allows%20them%20to%20be%20used%20in%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Fapp%2Ftransaction-diagnostics%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eend-to-end%20transaction%20monitoring%3C%2FA%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%2203242020-image1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F178901i2BDB3765E290A553%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%2203242020-image1.png%22%20alt%3D%2203242020-image1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFrom%20this%20application%20map%2C%20you%20can%20see%20that%20two%20HTTP%20calls%20happened%20to%20the%20storage%20resource%2C%20and%20one%20call%20to%20the%20Azure%20AD%20resource.%20You%20can%20see%20the%20average%20time%20they%20took.%20From%20here%2C%20you%20can%20dive%20in%20deeper%20to%20performance%20problems%20in%20your%20application.%3C%2FP%3E%0A%3CH2%20id%3D%22want-to-hear-more%22%20id%3D%22toc-hId--23817747%22%20id%3D%22toc-hId--23751404%22%3EWant%20to%20hear%20more%3F%3C%2FH2%3E%0A%3CP%3EWhat%20do%20you%20think%20of%20distributed%20tracing%3F%20We%E2%80%99re%20actively%20soliciting%20your%20feedback%20on%20this%20feature%2C%20so%20let%20us%20know!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFollow%20us%20on%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FAzureSDK%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ETwitter%20at%20%40AzureSDK%3C%2FA%3E.%20We%E2%80%99ll%20be%20covering%20more%20best%20practices%20in%20cloud-native%20development%20as%20well%20as%20providing%20updates%20on%20our%20progress%20in%20developing%20the%20next%20generation%20of%20Azure%20SDK.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1247041%22%20slang%3D%22en-US%22%3E%3CP%3ETodays%20topic%20is%20diagnostics%20with%20Python%20applications.%20I%20have%20a%20fairly%20complex%20application%2C%20and%20something%20is%20going%20wrong.%20Azure%20provides%20a%20capability%20within%20Azure%20Monitor%20called%20transaction%20monitoring%20that%20tracks%20a%20transaction%20(such%20as%20an%20API%20call)%20from%20your%20application%20all%20the%20way%20through%20to%20the%20service%20that%20fulfills%20the%20request.%20This%20is%20powered%20by%20OpenTelemetry%2C%20a%20distributed%20tracing%20framework.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

Todays topic is diagnostics with Python applications. I have a fairly complex application, and something is going wrong. Azure provides a capability within Azure Monitor called transaction monitoring that tracks a transaction (such as an API call) from your application all the way through to the service that fulfills the request. This is powered by OpenTelemetry, a distributed tracing framework.

 

As always, when learning a new technology, I simplify the application so I can see what is going on. Here is a really simple application:

import os
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import ResourceExistsError
from azure.storage.blob import BlobServiceClient

def upload_to(account_url, container_name, blob_name):
  # Create a client for this request
  client = BlobServiceClient(account_url, credential=DefaultAzureCredential())

  # Create a client for the named container
  container_client = client.get_container_client(container_name)
  try:
    container_client.create_container()
    print("Container created")
  except ResourceExistsError:
    print("Container already exists (ignored).")

  # Create a client for the named blob and upload data
  blob_client = container_client.get_blob_client(blob_name)
  with open(blob_name, "rb") as data:
    blob_client.upload_blob(data, overwrite=True)

# Upload main.py to the container
storage_url=os.environ["APP_STORAGE_ACCOUNT_URL"]
upload_to(storage_url, "demo", "main.py")

This simple console application creates a container within the storage account, then uploads the main.py file (which contains the source code to the app) as a blob. To support this, I’ve created a resource group, a storage account, and a service principal with permissions to access the storage account using the Azure CLI.

az group create \
  --name python-demoapp \
  --location westus
az storage account create \
  --name pythondemoapp03242020 \
  --resource-group python-demoapp \
  --location westus \
  --sku Standard_LRS \
  --kind StorageV2
az ad sp create-for-rbac \
  --name http://pythondemoapp03242020 \
  --role "Storage Blob Data Contributor" \
  --scopes /subscriptions/<SUBSCRIPTION>/resourceGroups/python-demoapp

The storage account name must be unique across Azure, so pick a good name! Replace SUBSCRIPTION with your Subscription ID. You can use the az account show command to see the subscription. You can then set some environment variables based on the output:

export AZURE_TENANT_ID="<create-for-rbac output tenant>"
export AZURE_CLIENT_ID="<create-for-rbac output appId>"
export AZURE_CLIENT_SECRET="<create-for-rbac output password>"
export APP_STORAGE_ACCOUNT_URL="<storage-create output primaryEndpoints.blob>"

When you run the application, you will likely see a one liner output that shows that the container is created or not. If there is an error, it will be displayed because of exception handling. However, you probably won’t know where the application is breaking. There is a more insidious error, though. What happens if the container create call takes a long time? How would you know it was that call and not the upload, or some of your other logic, for example?

Basic logging

The Azure SDK for Python uses Python logging:

import logging

logging.basicConfig()
azure_root_logger = logging.getLogger('azure')
azure_root_logger.setLevel(logging.DEBUG)

This will print the request and response. Information that may have PII is redacted, but otherwise you get all the headers along with the request and response. This allows you to see if the SDK is doing the right thing, and it will also allow you to assure yourself that your code is handling the responses correctly, especially when augmented by logging of your own.

Distributed tracing

When you are looking at why a particular network operation is slow, logging alone won’t help you. You need to be able to trace the entire request from beginning to end, including “across the wire” to the service. Consider a typical application has the application, a piece of middleware that implements a REST or GraphQL endpoint, and then a database connection. Which bit of the infrastructure is slow gets really complicated. As the infrastructure becomes more complex, so does the difficulty of figuring out which piece of the infrastructure is having issues.

 

Enter OpenTelemetry. OpenTelemetry is an open standard that allows you to trace requests across your infrastructure. It passes headers between the various components to move the request ID through the system. Let’s start by instrumenting the application.

 

WARNING

The OpenTelemetry library is in preview right now, and the API is changing with new versions. Ensure you are using an API/SDK version that is compatible with your exporter and plugins. Before you use preview packages, create and activate a virtual environment for your work.

 

First, we need some libraries:

pip install opentelemetry-api==0.4a0
pip install opentelemetry-sdk==0.4a0

 We are pinning the version of the OpenTelemetry SDK here so that the instructions work properly. The API for OpenTelemetry is changing, so make sure you follow the appropriate instructions. You can find the documentation for this version on GitHub.

 

Update the app to support basic tracing:

import os
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import ResourceExistsError
from azure.storage.blob import BlobServiceClient
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

# Configure OpenTelemetry Exporter
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
exporter = ConsoleSpanExporter()

# Configure OpenTelemetry Tracing
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
trace.tracer_source().add_span_processor(
    SimpleExportSpanProcessor(exporter)
)
tracer = trace.get_tracer(__name__)

def upload_to(account_url, container_name, blob_name):
  with tracer.start_as_current_span("upload_to"):
    # Create a client for this request
    client = BlobServiceClient(account_url, credential=DefaultAzureCredential())

    # Create a client for the named container
    container_client = client.get_container_client(container_name)
    try:
      container_client.create_container()
      print("Container created")
    except ResourceExistsError:
      print("Container already exists (ignored).")

    # Create a client for the named blob
    blob_client = container_client.get_blob_client(blob_name)
    with open(blob_name, "rb") as data:
      blob_client.upload_blob(data, overwrite=True)

# Upload main.py to the container
with tracer.start_as_current_span("mainapp"):
  storage_url=os.environ['APP_STORAGE_ACCOUNT_URL']
  upload_to(storage_url, "demo", "main.py")

First, you need to decide where you are going to put the trace information you collect. This is known as an Exporter. There are several good ones (and we’ll get onto that in a moment), but, for now, we are just sending the trace information to the console. We can then configure the rest of the OpenTelemetry system.

 

Once you have the OpenTelemetry library configured, just wrap each of the blocks you want to group together with a with tracer.start_as_current_span(name). This creates a unique Span - a block of operations with a start and an end that you can time individually. I’ve got one span for the main application, and another (inner) span for the upload_to method.

 

Running the app, this is what the output looks like:

$ python main.py
Container already exists (ignored).
Span(name="upload_to", context=SpanContext(trace_id=0x55dfc91e9feb3a56df74d9add43038f2, span_id=0x8dab600f714bfb13, trace_state={}), kind=SpanKind.INTERNAL, parent=Span(name="mainapp", context=SpanContext(trace_id=0x55dfc91e9feb3a56df74d9add43038f2, span_id=0xb2a773d445c6237b, trace_state={})), start_time=2020-03-19T21:40:29.285159Z, end_time=2020-03-19T21:40:30.060733Z)Span(name="mainapp", context=SpanContext(trace_id=0x55dfc91e9feb3a56df74d9add43038f2, span_id=0xb2a773d445c6237b, trace_state={}), kind=SpanKind.INTERNAL, parent=None, start_time=2020-03-19T21:40:29.285159Z, end_time=2020-03-19T21:40:30.061730Z)

You can see two spans - one inside the other - with the appropriate names.

Enable distributed tracing in the Azure SDK

To enable distributed tracing within the Azure SDK, you need to tell Azure Core which tracing library you are using. There is a tracing implementation for the older OpenCensus and the newer OpenTelemetry. You could also write your own, although that would be more work. Install the preview tracing package for the Azure SDK:

pip install azure-core-tracing-opentelemetry

Add the following three lines to the start of your application:

# Configure distributed tracing in Azure SDK
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
settings.tracing_implementation = OpenTelemetrySpan

Run the app again to see the results:

Span(name="/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx/oauth2/v2.0/token", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x2e3ee534daffef73, trace_state={}), kind=SpanKind.CLIENT, parent=Span(name="ContainerClient.create_container", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x78c07183a6860ff7, trace_state={})), start_time=2020-03-19T21:43:33.383835Z, end_time=2020-03-19T21:43:33.725025Z)Span(name="/demo", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x6c6e9f9ce09e3c03, trace_state={}), kind=SpanKind.CLIENT, parent=Span(name="ContainerClient.create_container", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x78c07183a6860ff7, trace_state={})), start_time=2020-03-19T21:43:33.726025Z, end_time=2020-03-19T21:43:34.035472Z)Span(name="ContainerClient.create_container", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x78c07183a6860ff7, trace_state={}), kind=SpanKind.INTERNAL, parent=Span(name="upload_to", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x3e61cb726380e524, trace_state={})), start_time=2020-03-19T21:43:33.360182Z, end_time=2020-03-19T21:43:34.051102Z)Container already exists (ignored).
Span(name="/demo/main.py", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x6d60987367993bab, trace_state={}), kind=SpanKind.CLIENT, parent=Span(name="BlobClient.upload_blob", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x52f5313f59b27526, trace_state={})), start_time=2020-03-19T21:43:34.095871Z, end_time=2020-03-19T21:43:34.139872Z)Span(name="BlobClient.upload_blob", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x52f5313f59b27526, trace_state={}), kind=SpanKind.INTERNAL, parent=Span(name="upload_to", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x3e61cb726380e524, trace_state={})), start_time=2020-03-19T21:43:34.093871Z, end_time=2020-03-19T21:43:34.145869Z)Span(name="upload_to", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x3e61cb726380e524, trace_state={}), kind=SpanKind.INTERNAL, parent=Span(name="mainapp", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x1f1c8cafdd0189ea, trace_state={})), start_time=2020-03-19T21:43:33.328939Z, end_time=2020-03-19T21:43:34.145869Z)Span(name="mainapp", context=SpanContext(trace_id=0x48776c79e95d494e1b24353ba9046b29, span_id=0x1f1c8cafdd0189ea, trace_state={}), kind=SpanKind.INTERNAL, parent=None, start_time=2020-03-19T21:43:33.328939Z, end_time=2020-03-19T21:43:34.148870Z)

We received two lines of output. One of the lines is a forked trace purely for the authentication flow. The second one is for the main application.  Now we can see what is going on "under the covers" of the Azure SDK.

Send the traces to Azure Monitor

Of course, you don’t always get to see the application when it is running. In fact, it’s rare that you can run an application on the console. Dumping the spans to the console is probably not the best idea. Visually, you can also use Zipkin or Jaeger to visualize the traces during development. For production apps, you want to integrate with Azure Monitor, which provides an integration point for your logs, metrics, alerts, and other production needs. First, create an Application Insights resource:

az extension add -n application-insights
az monitor app-insights component create \
  --app pythondemoapp03242020 \
  --location westus \
  --kind web \
  -g python-demoapp \
  --application-type web

This will output the instrumentation key, which you need to place in an environment variable:

export APPINSIGHTS_INSTRUMENTATION_KEY="<output instrumentationKey>"

Replace the exporter in your code with the following:

from azure_monitor import AzureMonitorSpanExporter
exporter = AzureMonitorSpanExporter(
  instrumentation_key = os.environ['APPINSIGHTS_INSTRUMENTATION_KEY']
)

Run your code. You can now view the application map, along with all the resources it connects to within Azure Monitor. The traces also show up in the dependencies logs, which allows them to be used in end-to-end transaction monitoring.

03242020-image1.png

 

From this application map, you can see that two HTTP calls happened to the storage resource, and one call to the Azure AD resource. You can see the average time they took. From here, you can dive in deeper to performance problems in your application.

Want to hear more?

What do you think of distributed tracing? We’re actively soliciting your feedback on this feature, so let us know!

 

Follow us on Twitter at @AzureSDK. We’ll be covering more best practices in cloud-native development as well as providing updates on our progress in developing the next generation of Azure SDK.