> For the complete documentation index, see [llms.txt](https://haleyryu.gitbook.io/engineer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://haleyryu.gitbook.io/engineer/open-tracing/python-opentracing-example.md).

# Python OpenTracing Example

Python 앱 opentracing 라이브러리 사용 예제

## ⌨️  Python 에서 opentracing 라이브러리 사용해보기 &#x20;

✔️ <https://github.com/yurishkuro/opentracing-tutorial/tree/master/python> 코드를 참고하여 작성했습니다.

### Install

```bash
$ pip install opentracing
```

### Example&#x20;

```python
import opentracing
tracer = opentracing.tracer

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

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

    ## Span 끝
    span.finish()
```

### Basic features of OpenTracing API&#x20;

* `start_span` : 새로운 span 시작

  * span은 operation name 을 가집니다.

* `span.finish()` : span 종료&#x20;

* 해당 span 의 start/end Timestamp 는 tracer implementation 에 의해 자동으로 측정됩니다.

### Context manger 로 span 사용하기

```python
def open_tracing_example_with_context_manager(hello_to):
    with tracer.start_span('say-hello') as span:
        hello_str = 'Hello, %s!' % hello_to
        print(hello_str)
```

이렇게까지하면 `opentracing.tracer` 의 포인트가 no-op tracer 에 있기 때문에, Tracing UI 에서는 확인할 수 없습니다.

이제 실제 Tracer 인스턴스를 생성해봅시다.

(다음 포스팅에 계속..)
