Skip to main content
Version: Cloud

Python in Kubernetes

Supported Frameworks

Django | Flask | Odoo 15 | Odoo 16

Standard Library Modules

Celery

Django

Prerequisite

To enable tracing for an application developed by the Django framework, sf-elastic-apm and sf-apm-lib must be available in your environment. These libraries can be installed by the following methods:

Add the below-mentioned entries in the requirements.txt file.

sf-elastic-apm==6.7.2
sf-apm-lib==0.1.5

OR

Install the below libraries using CLI command.

pip install sf-elastic-apm==6.7.2 
pip install sf-apm-lib==0.1.5

Configuration

Make sure that the project and the application are created in the SnappyFlow server. Click here to know how to create a project and an application in SnappyFlow.

Add the following entries in the settings.py file.

  1. Add the following import statements.

    from sf_apm_lib.snappyflow import Snappyflow
    import os
  2. Add the following entry in the INSTALLED_APPS block.

    'elasticapm.contrib.django'
  3. Add the following entry in the MIDDLEWARE block.

    'elasticapm.contrib.django.middleware.TracingMiddleware'
  4. Add the following source code to integrate a Django application with SnappyFlow.

    try: 
    sf = Snappyflow()

    # Add below part to manually configure the initialization
    SF_PROJECT_NAME = os.getenv('SF_PROJECT_NAME')
    SF_APP_NAME = os.getenv('SF_APP_NAME')
    SF_PROFILE_KEY = os.getenv('SF_PROFILE_KEY')
    sf.init(SF_PROFILE_KEY, SF_PROJECT_NAME, SF_APP_NAME)
    # End of manual configuration
    SFTRACE_CONFIG = sf.get_trace_config()

    ELASTIC_APM={
    # Specify your service name for tracing
    'SERVICE_NAME': "custom-service" ,
    'SERVER_URL': SFTRACE_CONFIG.get('SFTRACE_SERVER_URL'),
    'GLOBAL_LABELS': SFTRACE_CONFIG.get('SFTRACE_GLOBAL_LABELS'),
    'VERIFY_SERVER_CERT': SFTRACE_CONFIG.get('SFTRACE_VERIFY_SERVER_CERT'),
    'SPAN_FRAMES_MIN_DURATION': SFTRACE_CONFIG.get('SFTRACE_SPAN_FRAMES_MIN_DURATION'),
    'STACK_TRACE_LIMIT': SFTRACE_CONFIG.get('SFTRACE_STACK_TRACE_LIMIT'),
    'CAPTURE_SPAN_STACK_TRACES': SFTRACE_CONFIG.get('SFTRACE_CAPTURE_SPAN_STACK_TRACES'),
    'DJANGO_TRANSACTION_NAME_FROM_ROUTE': True,
    'CENTRAL_CONFIG': False,
    'METRICS_INTERVAL': '0s'
    }
    except Exception as error:
    print("Error while fetching snappyflow tracing configurations", error)
  5. In the Kubernetes deployment file, add SF_PROFILE_KEY, SF_PROJECT_NAME, and SF_APP_NAME, as environment variables.

    #deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: python-app
    labels:
    app: python-app
    spec:
    containers:
    - name: python-app
    image: imagename/tag:version
    env:
    - name: SF_PROFILE_KEY
    value: <profle-key>
    - name: SF_PROJECT_NAME
    value: <project_name>
    - name: SF_APP_NAME
    value: <app-name>
  6. If the deployment is with Helm Charts, add the environment variables: SF_PROJECT_NAME, SF_APP_NAME, and SF_PROFILE_KEY in the values.yaml file.

    #values.yaml
    global:
    # update the sfappname, sfprojectname and key with the proper values
    sfappname: <app-name>
    sfprojectname: <project-name>
    key: <profile-key>

    replicaCount: 1
    image:
    repository: djangoapp
    pullPolicy: IfNotPresent
    tag: "latest"
  7. In the deployment.yaml file of the Helm Charts, give the key-value from the global section of the value.yaml file.

    #deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    spec:
    containers:
    - name: {{ .Chart.Name }}
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    env:
    - name: SF_PROFILE_KEY
    value: {{ .Values.global.key }}
    - name: SF_PROJECT_NAME
    value: {{ .Values.global.sfprojectname }}
    - name: SF_APP_NAME
    value: {{ .Values.global.sfappname }}
note

If your app is in debug mode (eg: settings.Debug = true), then the agent won’t send any tracing data to the SnappyFlow server. You can override it by adding 'Debug':True configuration in the ELASTIC_APM block.

Verification

Follow the below steps to verify and view the trace data.

  1. Go to the Application tab in SnappyFlow and navigate to your Project > Application > Dashboard.

  2. Navigate to the Tracing section and click the View Transactions button

  3. You can view the traces in the Aggregate and the Real Time tabs.



Troubleshoot

  1. If the trace data is unavailable in the SnappyFlow server, check the trace configuration in the settings.py file.

  2. Add the key-value pair in the ELASTIC_APM block of the settings.py file to enable the debug logs.

    'DEBUG':True
Sample Application Code

Click here to view the sample application for which the configuration mentioned in the above sections enables the tracing feature.

Flask

Prerequisite

To enable tracing for an application that is developed by the Flask framework, sf-elastic-apm and sf-apm-lib must be available in your environment. These libraries can be installed by the following methods:

Add the below-mentioned entries in the requirements.txt file.

sf-elastic-apm[flask]==6.7.2
sf-apm-lib==0.1.5

OR

Install the libraries using CLI commands.

pip install sf-elastic-apm[flask]==6.7.2 
pip install sf-apm-lib==0.1.5

Configuration

Make sure that the project and the application are created in the SnappyFlow Server. Click here to know how to create a project and an application in SnappyFlow.

Add the following entries in the app.py file.

  1. Add the following import statements.

    from elasticapm.contrib.flask import ElasticAPM 
    from sf_apm_lib.snappyflow import Snappyflow
    import os
  2. Add the following source code to integrate a Flask application with SnappyFlow.

       sf = Snappyflow() 
    # Add below part to manually configure the initialization
    SF_PROJECT_NAME = os.getenv('SF_PROJECT_NAME')
    SF_APP_NAME = os.getenv('SF_APP_NAME')
    SF_PROFILE_KEY = os.getenv('SF_PROFILE_KEY')
    sf.init(SF_PROFILE_KEY, SF_PROJECT_NAME, SF_APP_NAME)
    # End of manual configuration
    SFTRACE_CONFIG = sf.get_trace_config()
    app.config['ELASTIC_APM'] = {
    # Specify your service name for tracing
    'SERVICE_NAME': 'flask-service',
    'SERVER_URL': SFTRACE_CONFIG.get('SFTRACE_SERVER_URL'),
    'GLOBAL_LABELS': SFTRACE_CONFIG.get('SFTRACE_GLOBAL_LABELS'),
    'VERIFY_SERVER_CERT': SFTRACE_CONFIG.get('SFTRACE_VERIFY_SERVER_CERT'),
    'SPAN_FRAMES_MIN_DURATION': SFTRACE_CONFIG.get('SFTRACE_SPAN_FRAMES_MIN_DURATION'),
    'STACK_TRACE_LIMIT': SFTRACE_CONFIG.get('SFTRACE_STACK_TRACE_LIMIT'),
    'CAPTURE_SPAN_STACK_TRACES': SFTRACE_CONFIG.get('SFTRACE_CAPTURE_SPAN_STACK_TRACES'),
    'METRICS_INTERVAL': '0s'
    }
    apm = ElasticAPM(app)
  3. In the Kubernetes deployment file, add SF_PROFILE_KEY, SF_PROJECT_NAME, and SF_APP_NAME as environment variables.

    #deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: python-app
    labels:
    app: python-app
    spec:
    containers:
    - name: python-app
    image: imagename/tag:version
    env:
    - name: SF_PROFILE_KEY
    value: <profle-key>
    - name: SF_PROJECT_NAME
    value: <project_name>
    - name: SF_APP_NAME
    value: <app-name>
  4. If the application is deployed using Helm Charts, add the environment variables: SF_APP_NAME, SF_PROJECT_NAME, and SF_PROFILE_KEY in the values.yaml. file

    #values.yaml
    global:
    # update the sfappname, sfprojectname and key with the proper values
    sfappname: <app-name>
    sfprojectname: <project-name>
    key: <profile-key>

    replicaCount: 1
    image:
    repository: djangoapp
    pullPolicy: IfNotPresent
    tag: "latest"
  5. In the deployment.yaml file of the Helm Chart, give the key-value from the global section of the value.yaml file.

    #deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    spec:
    containers:
    - name: {{ .Chart.Name }}
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    env:
    - name: SF_PROFILE_KEY
    value: {{ .Values.global.key }}
    - name: SF_PROJECT_NAME
    value: {{ .Values.global.sfprojectname }}
    - name: SF_APP_NAME
    value: {{ .Values.global.sfappname }}
note

If your app is in debug mode (eg: app.Debug = true), then the agent won’t send any tracing data to the SnappyFlow server. You can override it by adding 'Debug':True configuration in the ELASTIC_APM block.

Verification

Follow the below steps to verify and view the trace data.

  1. Go to the Application tab in SnappyFlow and navigate to your Project > Application > Dashboard.
  2. Navigate to the Tracing section and click the View Transactions button
  3. You can view the traces in the Aggregate and the Real Time tabs.

Troubleshoot

  1. If the trace data is unavailable in the SnappyFlow server, check the trace configuration in the app.py file.

  2. Add the key-value pair in the app.config block of the app.py file to enable the debug logs.

    'DEBUG':True

Sample Application Code

Click here to view the sample application for which the configuration mentioned in the above sections enables the tracing feature.

Odoo 15

Prerequisite

To enable tracing for an application developed using Odoo version 15, sf-elastic-apm and sf-apm-lib must be available in your environment.

Install the libraries using CLI.

pip install sf-elastic-apm==6.7.2 
pip install sf-apm-lib==0.1.4

Configuration

  1. To setup the elastic apm client, add the following code in the http.py file of Odoo application.

    import elasticapm 
    from sf_apm_lib.snappyflow import Snappyflow, begin_transaction, end_transaction
  2. Add the below code in the http.py file to manually configure the initialization. By default, initialization will pick profileKey, projectName and appName from sfagent config.yaml file.

    Class: Root and Method: init

    sf = Snappyflow()
    SF_PROJECT_NAME = '<Snappyflow Project Name>'
    SF_APP_NAME = '<Snappyflow App Name>'
    SF_PROFILE_KEY = '<Snappyflow Profile Key>'
    sf.init(SF_PROFILE_KEY, SF_PROJECT_NAME, SF_APP_NAME)
    # End of manual configuration

    trace_config = sf.get_trace_config() # Returns trace config
    self.client = elasticapm.Client(
    service_name="<Service name> ",# Specify service name for tracing
    server_url=trace_config['SFTRACE_SERVER_URL'],
    verify_cert=trace_config['SFTRACE_VERIFY_SERVER_CERT'],
    global_labels=trace_config['SFTRACE_GLOBAL_LABELS']
    )
    elasticapm.instrument()
  3. Once the request is declared, add the below codes in the http.py file to capture the transaction.

    Class: Root and Method: dispatch


    begin_transaction(elasticapm, self.client, request) #add after self.get request

    end_transaction(elasticapm, self.client, request, response) #add before return response

Click here complete configuration.

Verification

  1. Go to the Application tab in SnappyFlow and navigate to your Project > Application > Dashboard.

  2. Navigate to the Tracing section and click the View Transactions button.

  3. You can view the traces in the Aggregate and the Real Time tabs.


Odoo 16

Prerequisite

To enable tracing for an application developed using Odoo version 16, sf-elastic-apm and sf-apm-lib must be available in your environment.

Install the libraries using CLI.

pip install sf-elastic-apm==6.7.2 
pip install sf-apm-lib==0.1.4

Configuration

  1. To setup the elastic apm client, add the following code at the Root class of http.py file in Odoo application.

    import elasticapm 
    from sf_apm_lib.snappyflow import Snappyflow, begin_transaction, end_transaction
  2. Add the below code in the http.py file to manually configure the initialization. By default, initialization will pick profileKey, projectName and appName from sfagent config.yaml file.

    Class: Application and Method: call

    sf = Snappyflow()
    SF_PROJECT_NAME = '<Snappyflow Project Name>'
    SF_APP_NAME = '<Snappyflow App Name>'
    SF_PROFILE_KEY = '<Snappyflow Profile Key>'
    sf.init(SF_PROFILE_KEY, SF_PROJECT_NAME, SF_APP_NAME)
    # End of manual configuration

    trace_config = sf.get_trace_config() # Returns trace config
    client = elasticapm.Client(
    service_name="<Service name> ",# Specify service name for tracing
    server_url=trace_config['SFTRACE_SERVER_URL'],
    verify_cert=trace_config['SFTRACE_VERIFY_SERVER_CERT'],
    global_labels=trace_config['SFTRACE_GLOBAL_LABELS']
    )
    elasticapm.instrument()

  1. Once the request is declared, add the below codes in the http.py file to capture the transaction.

    Class: Application and Method: call


    begin_transaction(elasticapm, client, request) #add after request
    end_transaction(elasticapm, client, request, response) #add before return response

Click here for complete configuration.

Verification

  1. Go to the Application tab in SnappyFlow and navigate to your Project > Application > Dashboard.

  2. Navigate to the Tracing section and click the View Transactions button.

  3. You can view the traces in the Aggregate and the Real Time tabs.


Celery

note

The Celery configuration explained below is based on redis broker.

Prerequisite

To enable tracing for an application developed by Celery, sf-elastic-apm, redis, and sf-apm-lib must be available in your environment.

Install the following requirements.

pip install sf-elastic-apm==6.7.2 
pip install redis
pip install sf-apm-lib==0.1.5

Configuration

To setup the elastic apm client, add the following code at the beginning of the file where the celery app is initialized.

from sf_apm_lib.snappyflow import Snappyflow 
from elasticapm import Client, instrument
from elasticapm.contrib.celery import register_exception_tracking, register_instrumentation

instrument()

try:
sf = Snappyflow() # Initialize Snappyflow. By default intialization will take profileKey, projectName and appName from sfagent config.yaml

# Add below part to manually configure the initialization
SF_PROJECT_NAME = '<SF_PROJECT_NAME>' # Replace with appropriate Snappyflow project name
SF_APP_NAME = '<SF_APP_NAME>' # Replace with appropriate Snappyflow app name
SF_PROFILE_KEY = '<SF_PROFILE_KEY>' # Replace Snappyflow Profile key
sf.init(SF_PROFILE_KEY, SF_PROJECT_NAME, SF_APP_NAME)
# End of manual configuration

SFTRACE_CONFIG = sf.get_trace_config()
apm_client = Client(service_name= '<Service_Name>', # Specify service name for tracing
server_url= SFTRACE_CONFIG.get('SFTRACE_SERVER_URL'),
global_labels= SFTRACE_CONFIG.get('SFTRACE_GLOBAL_LABELS'),
verify_server_cert= SFTRACE_CONFIG.get('SFTRACE_VERIFY_SERVER_CERT')
)

register_exception_tracking(apm_client)
register_instrumentation(apm_client)
except Exception as error:
print("Error while fetching snappyflow tracing configurations", error)

Verification

Once the instrumentation is done and the celery worker is running, you can see a trace for each celery task in the Snappyflow server. Follow the below steps to verify and view the traces.

  1. Go to the Application tab in SnappyFlow and navigate to your Project > Application > Dashboard.
  2. Navigate to the Tracing section and click the View Transactions button.
  3. You can view the traces in the Aggregate and the Real Time tabs.

Reference Code

Refer complete code: https://github.com/snappyflow/tracing-reference-apps/blob/master/ref-celery/tasks.py