# Python Jaeger Tutorial

## 1. Jaeger 사용하기 - Basic Tutorial&#x20;

### Install jaeger-client

* <https://github.com/jaegertracing/jaeger-client-python>

```bash
$ pip install jaeger-client
```

### Initialize a Real Tracer

```python
import sys
import logging
from jaeger_client import Config
import time


def open_tracing_example(hello_to):
    ## Span 시작
    span = tracer.start_span('say-hello')

    hello_str = 'Hello, %s!' % hello_to
    print(hello_str)

    ## Span 끝
    span.finish()


def init_tracer(service):
    logging.getLogger('').handlers = []
    logging.basicConfig(format='%(message)s', level=logging.DEBUG)

    config = Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': True,
        },
        service_name=service,
    )

    # this call also sets opentracing.tracer
    return config.initialize_tracer()


tracer = init_tracer('hello-world')

hello_to = sys.argv[1]
open_tracing_example(hello_to)

# yield to IOLoop to flush the spans
time.sleep(2)
tracer.close()
```

#### ✔️ tracer = init\_tracer('hello-world')

* 파라미터로 넘겨주는 String `hello-world`의 의미:
  * tracer에 의해 방출된 모든 `span` 들을 `hello-world` 서비스로부터 비롯된것으로 표시하는데 사용됩니다.
*

#### ✔️  time.sleep(2)

* Jaeger Backend 에 spans 를 임의로 플러쉬하기 위해.
*

#### 💻  logs output

```bash
$ python -m jaeger_tutorial Bryan

Initializing Jaeger Tracer with UDP reporter
Using sampler ConstSampler(True)
opentracing.tracer initialized to <jaeger_client.tracer.Tracer object at 0x102a7b6a0>[app_name=hello-world]
Hello, Bryan!
Reporting span 618c339cb1ca725a:deddc16d2e0305c7:0:1 hello-world.say-hello
```

## 2. Tags, Logs 를 사용하여 Trace Annotate 하기&#x20;

* 각각 다른 Argument 로 프로그램을 호출할때, 각각을 식별하기 위해 Program Arguments 를 포착해둘 필요가 있습니다.

### 방법 1) Tag 사용하기

* `Tag` 는 span 에 대한 metadata 를 제공하는 key-value 쌍입니다.

### 방법 2) Log 사용하기

* `Log` 는 보통의 log statement 와 유사하게, 로깅된 span 에 대한 timestamp 및 여러 데이터들을 포함합니다.

### Tags vs Logs

* `Tag` 는  span 의 전체 기간에 적용되는 속성들을 나타내기 위한 것입니다.
* 예를들어, 어떤 span 이 HTTP request 를 나타내고 있다면,&#x20;

Python 에서도 Java-Spring 처럼 method 단위로 자동 span 생성해주는게 있는지 찾아보기.

Annotation 형태로 하는게 없다면 직접 만들어보기.

*
