# Python

### 1) CRD installation

You can create one by searching for Infrastructure - Custom Resources - '**instrumentations**'.

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FPvzIFxmokmCgKkIiyHu4%2Fimage.png?alt=media&#x26;token=f5b88388-f7b9-40d9-bb68-25b36093b1b7" alt=""><figcaption></figcaption></figure>

You can create it by clicking the Create button, selecting the namespace where you want to collect logs, and modifying the form below.

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FgYyvskXlyva9dtnOEYFO%2Fimage.png?alt=media&#x26;token=e7c97c64-0a15-4147-b24f-b5ccdbdb6776" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The above CRD is applied on a namespace basis, and automatic container logs can be collected for other languages ​​in the same namespace.
{% endhint %}

```yaml
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: {Instrumentation name}
spec:
  exporter:
    endpoint: {log-agent Service address}:4318
  python:
    image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-python:0.44b0
    env:
      - name: OTEL_LOGS_EXPORTER
        value: otlp_proto_http
      - name: OTEL_EXPORTER_OTLP_LOGS_ENDPOINT
        value: {log-agent Service address}:4318/v1/logs
      - name: OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED
        value: 'true'
        
      # Used when you want to collect all Java applications for which the anotation value of the namespace is 'true'.
      - name: OTEL_EXPORTER_OTLP_LOGS_HEADERS
        value: app_token={Application Token},app_name={Application Name}
```

**`log-agent Service Address`** : Infrastructure - Cluster - Add-ons - Click 'log-agent' and check the service name.

( http port = 4318 , grpc port = 4317)

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FKRsOe34AOAWbKugKWxOj%2Fimage.png?alt=media&#x26;token=1d105819-143f-49ba-a1c3-357b518f9d3d" alt=""><figcaption></figcaption></figure>

## 2) Add annotations to the applications you want to collect

Add annotations to the workloads in the namespace for which you want to collect logs.

Application - Service Map - Service Map to collect logs - Workload - Select the application to collect logs - Click the "Settings" button.

Change the Yaml view and add the following annotations to the template - metadata - annotations section.

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FEtkoBaBS8UlKXRsgIhIY%2Fimage.png?alt=media&#x26;token=c328ebf0-7081-4251-b136-b92cbc7353e1" alt=""><figcaption></figcaption></figure>

```yaml
instrumentation.opentelemetry.io/inject-python: 'true'
```

## 3) Add service name and token value to environment variables.

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2Fa81KopeEUvHBFcPa9yEO%2Fimage.png?alt=media&#x26;token=f654addc-1ce6-4eb2-8553-8d561b4a8b90" alt=""><figcaption></figcaption></figure>

```yaml
apiVersion: apps/v1
kind: Deployment
...
spec:
...
  template:
    spec:
      containers:
      - env:

	# Application name
	- name: OTEL_SERVICE_NAME
          value: {Application name}
        
        # Settings for authentication
        - name: OTEL_EXPORTER_OTLP_LOGS_HEADERS
          value: app_token={Application token},app_name={application name}     
        
        image: {python-application image}
        imagePullPolicy: Always
        ...
```

## 4. Check Application log

### 1) Logging - Application Log - Search for the application you set in the application list.

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2F5MPtKWlLU7inGuhnPZMw%2Fimage.png?alt=media&#x26;token=894704e5-ce90-4ee4-b01d-e9fbc2d3bfa9" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cocktailcloud.gitbook.io/cocktail-cloud-online-en/log-service/application-logs/application-logging/automatic-instrumentation-of-container-logs/python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
