census-instrumentation / opencensus-python

A stats collection and distributed tracing framework
Apache License 2.0
669 stars 250 forks source link

Function App Azure - Trace Exporter #787

Open victoraugustolls opened 5 years ago

victoraugustolls commented 5 years ago

Describe your environment. Timer scheduled function, running inside a container (Docker) in Azure Functions.

Steps to reproduce. Run a timer scheduled function inside docker on azure.

What is the expected behavior? Working tracer in Application Insights.

What is the actual behavior? Traces not showing.

reyang commented 5 years ago

@lmolkova need your help here on Azure Functions.

victoraugustolls commented 5 years ago

Simple example:

__init__.py:

import datetime
import logging

import azure.functions as func
from opencensus.ext.azure.trace_exporter import AzureExporter

from opencensus.trace import tracer
from opencensus.trace import samplers

def main(timer: func.TimerRequest) -> None:
    trace = tracer.Tracer(
        exporter=AzureExporter(),
        sampler=samplers.ProbabilitySampler(1.0)
    )
    db = Database()
    with trace.span(name="get_expired"):
        requests = db.get_expired_requests()
        logging.info("Test")
        logging.info(requests)

Dockefile:

FROM mcr.microsoft.com/azure-functions/python:2.0

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    apt-utils \
    apt-transport-https \
    build-essential \
    gcc \
    gnupg && \
    echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' >> /etc/apt/sources.list.d/pgdg.list && \
    curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
    # install SQL Server and PostgreSQL drivers
    apt-get update && ACCEPT_EULA=Y apt-get install -y --no-install-recommends \
    libpq-dev \
    postgresql-client-10 && \
    # clear installations
    apt-get remove -y apt-utils apt-transport-https build-essential curl gnupg && \
    rm -rf /var/lib/apt/lists/*

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY . /home/site/wwwroot

RUN cd /home/site/wwwroot && \
    pip install -r requirements.txt
victoraugustolls commented 5 years ago

@reyang @lmolkova if there are any leads, I can try and help solve this.

victoraugustolls commented 5 years ago

I will run the python function outside the container in Azure Functions to see if it is something related to Docker

victoraugustolls commented 5 years ago

tried to run out of Docker, still no luck...

victoraugustolls commented 5 years ago

Tried logging inside azure exporter and it looks like it isn't being called! @lmolkova

victoraugustolls commented 5 years ago

Diving deeper, the "problem" was not having a root tracer, with the SERVER kind. They were in fact being logged as dependencies but with no parents. Right now I'm taking a look if is possible to take the function root id or if I should create mine.

victoraugustolls commented 5 years ago

There should be an adaptation to Azure Exporter to work with Azure Functions, I have a working exporter for it in my local machine, will see how to adapt it better for more general cases.

lmolkova commented 5 years ago

root span (request) is generated in .NET Function host runtime.

Today there is no way to get the context out of .NET, but there is an active work in progress https://github.com/Azure/azure-functions-python-worker/pull/545

After this is shipped (AFAIK in the next month) it will be possible to get traceparent/tracestate out of function execution context. We'll also need to create some instructions on how to feed this to the opencensus/telemetry.

In future (no ETA yet) we plan some magic in functions that will populate this in an ambient context and make opencensus collection just work with proper correlation.

victoraugustolls commented 5 years ago

I see, thanks @lmolkova. Will subscribe to this thread so I can adapt my code to use the parent trace_id in the exporter and see if it works. And, if that's the case, I can post here how am I doing so and open a PR!