Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Extension for initializing OpenCensus tracer into Azure Functions #1010

Merged
merged 7 commits into from
Apr 28, 2021

Conversation

Hazhzeng
Copy link
Contributor

@Hazhzeng Hazhzeng commented Feb 9, 2021

Background

Currently, in Azure Functions, customers have a hard time initializing OpenCensus. The customer needs to write an initializing section before implementing the def main() function.

With this PR, Azure Functions exposes an extension interface to install the OpenCensus extension. Related changes in Azure Functions Python Worker and SDK are listed as follow:

  1. Python Worker Extension Interface (sdk) Azure/azure-functions-python-library#81
  2. Python Worker Extension Interface (worker) Azure/azure-functions-python-worker#815

Usage

  1. Enable the worker extension feature flag by configuring PYTHON_ENABLE_WORKER_EXTENSIONS = 1 in Azure Functions setting or local dev environment variable.
  2. In requirements.txt, make sure azure-function, opencensus-ext-azure and opencensus-ext-requests are properly installed.
# requirements.txt
azure-functions
opencensus-ext-azure
opencensus-ext-requests
  1. An example in HttpTrigger to send requests traces into your Azure Monitor:
    1. Initialize the OpenCensus module by importing the opencensus.ext.azure.extension.azure_functions.OpenCensusExtension class in any of function app's trigger.
    2. Configure the extension by calling OpenCensusExtension.configure(connection_string='InstrumentationKey=;...').
    3. Use the context.tracer to propagate trace context in your Azure Monitor or AppInsights resources.

Example code:

import json
import logging
import requests

import azure.functions as func
from opencensus.ext.azure.extension.azure_functions import OpenCensusExtension

OpenCensusExtension.configure(
    libraries=['requests'],
    connection_string='InstrumentationKey=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
)

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    logging.info('Executing HttpTrigger with OpenCensus extension')

    with context.tracer.span("parent"):
        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,
    })

/cc: @lzchen

@google-cla
Copy link

google-cla bot commented Feb 9, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Feb 9, 2021
@Hazhzeng Hazhzeng marked this pull request as draft February 9, 2021 19:59
@tonybaloney
Copy link
Contributor

This PR would also benefit from #886

@Hazhzeng Hazhzeng marked this pull request as ready for review April 22, 2021 17:33
@google-cla
Copy link

google-cla bot commented Apr 22, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: yes and removed cla: no labels Apr 23, 2021
@lzchen
Copy link
Contributor

lzchen commented Apr 26, 2021

It would be great if you could add that example to the examples folder and as well as some getting started directions (including linking to getting started with azure functions, as well as setting PYTHON_ENABLE_WORKER_EXTENTIONS.

@Hazhzeng
Copy link
Contributor Author

cc @stefanushinardi @vrdmr @AnatoliB

@Hazhzeng Hazhzeng requested a review from lzchen April 27, 2021 20:36
@Hazhzeng Hazhzeng requested a review from lzchen April 28, 2021 00:07
Rename file

OK

Update implementation to AppLevel extension

Add license info

Fix syntax

Raise more concrete exceptions

Address PR comment

Add example

Add Python VSCode link in example

Add requirements.txt section

prettify

Fix docstring

Address PR issues

Don't use specific ingestion endpoint

Add new endline

Update CHANGELOG.md
@lzchen lzchen merged commit 9bb688d into census-instrumentation:master Apr 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants