# Example - Distributed tracing
In this sample, we'll run two Python applications: a service application, which exposes two methods, and a client application which will invoke the methods from the service using Dapr. The code is instrumented with [OpenCensus SDK for Python](https://opencensus.io/guides/grpc/python/).
This sample includes:
- invoke-receiver: Exposes the methods to be remotely accessed
- invoke-caller: Invokes the exposed methods
Also consider [getting started with observability in Dapr](https://github.com/dapr/quickstarts/tree/master/observability).
## Example overview
This sample uses the Client provided in Dapr's Python SDK invoking a remote method and Zipkin to collect and display tracing data.
## Pre-requisites
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started)
- [Install Python 3.7+](https://www.python.org/downloads/)
### Install dependencies
Clone this repository:
```sh
git clone https://github.com/dapr/python-sdk.git
cd python-sdk
```
Then get into the examples directory:
```sh
cd examples/w3c-tracing
```
Install dependencies:
<!-- STEP
name: Install deps
-->
```sh
pip3 install -r requirements.txt
```
<!-- END_STEP -->
### Verify Zipkin is running
Run `docker ps` to see if the container `dapr_zipkin` is running locally:
```bash
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24d043379da2 daprio/dapr "./placement" 2 days ago Up 32 hours 0.0.0.0:6050->50005/tcp dapr_placement
5779a0268159 openzipkin/zipkin "/busybox/sh run.sh" 2 days ago Up 32 hours 9410/tcp, 0.0.0.0:9411->9411/tcp dapr_zipkin
317fef6a8297 redis "docker-entrypoint.s…" 2 days ago Up 32 hours 0.0.0.0:6379->6379/tcp dapr_redis
```
If Zipkin is not working, [install the newest version of Dapr Cli and initialize it](https://docs.dapr.io/getting-started/install-dapr/).
### Run the Demo service sample
The Demo service application exposes two methods that can be remotely invoked. In this example, the service code has two parts:
In the `invoke-receiver.py` file, you will find the OpenCensus tracing and exporter initialization in addition to two methods: `say` and `sleep`. The instrumentation for the service happens automatically via the `OpenCensusServerInterceptor` class.
```python
tracer_interceptor = server_interceptor.OpenCensusServerInterceptor(AlwaysOnSampler())
app = App(
thread_pool=futures.ThreadPoolExecutor(max_workers=10),
interceptors=(tracer_interceptor,))
```
The `say` method prints the incoming payload and metadata in console. See the code snippet below:
```python
@app.method(name='say')
def say(request: InvokeMethodRequest) -> InvokeMethodResponse:
tracer = Tracer(sampler=AlwaysOnSampler())
with tracer.span(name='say') as span:
data = request.text()
span.add_annotation('Request length', len=len(data))
print(request.metadata, flush=True)
print(request.text(), flush=True)
return InvokeMethodResponse(b'SAY', "text/plain; charset=UTF-8")
```
The `sleep` methods simply waits for two seconds to simulate a slow operation.
```python
@app.method(name='sleep')
def sleep(request: InvokeMethodRequest) -> InvokeMethodResponse:
tracer = Tracer(sampler=AlwaysOnSampler())
with tracer.span(name='sleep') as _:
time.sleep(2)
print(request.metadata, flush=True)
print(request.text(), flush=True)
return InvokeMethodResponse(b'SLEEP', "text/plain; charset=UTF-8")
```
Use the following command to execute the service:
<!-- STEP
name: Run app with tracing
expected_stdout_lines:
- "✅ You're up and running! Both Dapr and your app logs will appear here."
- "✅ Exited Dapr successfully"
- "✅ Exited App successfully"
expected_stderr_lines:
background: true
sleep: 5
-->
```sh
dapr run --app-id invoke-receiver --app-protocol grpc --app-port 3001 python3 invoke-receiver.py
```
<!-- END_STEP -->
Once running, the service is now ready to be invoked by Dapr.
### Run the InvokeClient sample
This sample code uses the Dapr SDK for invoking two remote methods (`say` and `sleep`). Again, it is instrumented with OpenCensus with Zipkin exporter. See the code snippet below:
```python
ze = ZipkinExporter(
service_name="python-example",
host_name='localhost',
port=9411,
endpoint='/api/v2/spans')
tracer = Tracer(exporter=ze, sampler=AlwaysOnSampler())
with tracer.span(name="main") as span:
with DaprClient(tracer=tracer) as d:
num_messages = 2
for i in range(num_messages):
# Create a typed message with content type and body
resp = d.invoke_method(
'invoke-receiver',
'say',
data=json.dumps({
'id': i,
'message': 'hello world'
}),
)
# Print the response
print(resp.content_type, flush=True)
print(resp.text(), flush=True)
resp = d.invoke_method('invoke-receiver', 'sleep', data='')
# Print the response
print(resp.content_type, flush=True)
print(resp.text(), flush=True)
```
The class knows the `app-id` for the remote application. It uses `invoke_method` to invoke API calls on the service endpoint. Instrumentation happens automatically in `Dapr` client via the `tracer` argument.
Execute the following command in order to run the caller example, it will call each method twice:
<!-- STEP
name: Run caller app with tracing
expected_stdout_lines:
- "✅ You're up and running! Both Dapr and your app logs will appear here."
- '== APP == text/plain'
- '== APP == SAY'
- '== APP == text/plain'
- '== APP == SLEEP'
- '== APP == text/plain'
- '== APP == SAY'
- "✅ Exited Dapr successfully"
- "✅ Exited App successfully"
expected_stderr_lines:
background: true
sleep: 10
-->
```bash
dapr run --app-id invoke-caller --app-protocol grpc python3 invoke-caller.py
```
<!-- END_STEP -->
<!-- STEP
name: Pause for manual validation
manual_pause_message: "Zipkin tracing running on http://localhost:9411. Please open in your browser and test manually."
-->
<!-- END_STEP -->
Once running, the output should display the messages sent from invoker in the service output as follows:
![exposeroutput](./img/service.png)
Methods have been remotely invoked and display the remote messages.
Now, open Zipkin on [http://localhost:9411/zipkin](http://localhost:9411/zipkin). You should see a screen like the one below:
![zipking-landing](./img/zipkin-landing.png)
Click on the search icon to see the latest query results. You should see a tracing diagram similar to the one below:
![zipking-landing](./img/zipkin-result.png)
Once you click on the tracing event, you will see the details of the call stack starting in the client and then showing the service API calls right below.
![zipking-details](./img/zipkin-details.png)
### Zipkin API
Zipkin also has an API available. See [Zipkin API](https://zipkin.io/zipkin-api/) for more details.
To see traces collected through the API:
<!-- STEP
expected_stdout_lines:
- '"calllocal/invoke-receiver/say"'
- '"calllocal/invoke-receiver/sleep"'
- '"calllocal/invoke-receiver/say"'
- '"calllocal/invoke-receiver/sleep"'
expected_stderr_lines:
name: Curl validate
-->
```bash
curl -s "http://localhost:9411/api/v2/traces?serviceName=invoke-receiver&spanName=calllocal%2Finvoke-receiver%2Fsay&limit=10" -H "accept: application/json" | jq ".[][] | .name, .duration"
```
<!-- END_STEP -->
The `jq` command line utility is used in the above to give you a nice human readable printout of method calls and their duration:
```
"calllocal/invoke-receiver/say"
12711
"calllocal/invoke-receiver/say"
15218
"
没有合适的资源?快使用搜索试试~ 我知道了~
python-sdk:适用于Python的Dapr SDK
共214个文件
py:120个
md:27个
rst:21个
需积分: 50 3 下载量 139 浏览量
2021-04-08
07:34:14
上传
评论
收藏 1.1MB ZIP 举报
温馨提示
适用于Python的Dapr SDK 是一种可移植的,事件驱动的,无服务器的运行时,用于跨云和边缘构建分布式应用程序。 Dapr SDK for Python允许您基于actor设计模式来实现。 该SDK可以在本地,容器和任何分布式系统环境中运行。 这包括以下软件包: 框架 :Dapr构建基块的Dapr客户端 :配置 :序列化器/解串器 :Dapr gRPC自动生成的gRPC客户端 :Dapr的gRPC扩展 :Dapr的FastAPI扩展(actor) flask.dapr : Dapr的烧瓶扩展(演员) 地位 注意:适用于python的Dapr SDK目前处于Alpha阶段的社区开发中。 入门 先决条件 安装Dapr独立模式 安装Python 3.7+ 安装Dapr python sdk 官方套餐 # Install Dapr client sdk pip
资源详情
资源评论
资源推荐
收起资源包目录
python-sdk:适用于Python的Dapr SDK (214个子文件)
make.bat 795B
setup.cfg 985B
setup.cfg 791B
setup.cfg 772B
setup.cfg 703B
CODEOWNERS 116B
Dockerfile 3KB
Dockerfile 238B
Dockerfile 151B
Dockerfile 151B
.gitignore 1KB
tox.ini 2KB
mypy.ini 430B
devcontainer.json 1KB
secrets.json 63B
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
Makefile 634B
README.md 8KB
README.md 6KB
CONTRIBUTING.md 5KB
README.md 4KB
python-client.md 3KB
README.md 3KB
README.md 3KB
README.md 3KB
README.md 2KB
python-grpc.md 2KB
README.md 2KB
README.md 2KB
python-actor.md 2KB
_index.md 2KB
README.md 2KB
python-contributing.md 1KB
python-flask.md 1KB
python-fastapi.md 1KB
README.md 898B
bug_report.md 691B
README.md 606B
feature_request.md 481B
pull_request_template.md 474B
_index.md 150B
question.md 135B
proposal.md 135B
discussion.md 120B
service.png 480KB
zipkin-details.png 395KB
zipkin-result.png 140KB
zipkin-landing.png 29KB
response.proto 114B
dapr_pb2.py 121KB
dapr_pb2_grpc.py 37KB
client.py 30KB
appcallback_pb2.py 28KB
common_pb2.py 21KB
test_state_manager.py 15KB
test_dapr_grpc_client.py 14KB
_response.py 13KB
appcallback_pb2_grpc.py 10KB
_request.py 10KB
test_http_service_invocation_client.py 10KB
state_manager.py 9KB
actor.py 9KB
test_servicier.py 7KB
test_actor.py 7KB
proxy.py 6KB
runtime.py 6KB
fake_dapr_server.py 6KB
_servicier.py 6KB
actor.py 6KB
_helpers.py 5KB
actor.py 5KB
test_actor_manager.py 5KB
manager.py 5KB
dapr_actor_http_client.py 5KB
__init__.py 5KB
context.py 4KB
test_actor_runtime.py 4KB
app.py 4KB
demo_actor.py 4KB
_state.py 4KB
fake_actor_classes.py 4KB
client.py 3KB
_type_utils.py 3KB
test_client_proxy.py 3KB
dapr_invocation_http_client.py 3KB
_state_provider.py 3KB
state_store.py 3KB
json.py 3KB
_reminder_data.py 3KB
test_dapr_grpc_response.py 3KB
response_pb2.py 3KB
_timer_data.py 3KB
config.py 2KB
conf.py 2KB
fake_http_server.py 2KB
test_type_utils.py 2KB
_type_information.py 2KB
共 214 条
- 1
- 2
- 3
缪建明
- 粉丝: 42
- 资源: 4686
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0