流程說明
您可以基於Function Compute的鏈路建立自訂Span,將自訂的Span串聯到Function Compute的調用鏈中。Function Compute的鏈路分析基於OpenTracing協議的Jaeger實現,提供以下兩種方式建立自訂Span。
關於Jaeger和OpenTelemetry的詳細資料,請分別參見Jaeger和OpenTelemetry。
兩種方式的流程說明如下。
在函數中引入Jaeger或OpenTelemetry依賴包。引入三方依賴的具體操作,請參見為函數安裝第三方依賴。
在函數中擷取Function Compute調用鏈的Span Context,根據Function Compute的Span Context,建立自訂Span,在需要記錄耗時的程式碼片段前後添加埋點。
完成後,您可以在函數詳情頁面的鏈路追蹤頁簽或鏈路追蹤控制台查看調用鏈資訊。
使用Jaeger SDK
目前,支援使用Jaeger SDK方式建立自訂Span的運行環境及其範例程式碼如下所示。
本文以Node.js運行時為例,介紹如何通過Serverless Devs建立自訂Span以及建立並部署函數。
說明
您可以使用Serverless Devs安裝依賴包,並打包部署到Function Compute,您也可以通過其他方式打包並部署。Serverless Devs的更多資訊,請參見什麼是Serverless Devs。
建立代碼目錄。
初始化Node.js運行時模板。
執行以下命令,進入代碼目錄。
在代碼目錄中執行以下命令,初始化Node.js 12的專案。
s init devsapp/start-fc-event-nodejs12
輸出樣本如下。
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
Please input your project name (init dir) (start-fc-event-nodejs12)
設定專案名稱,然後按斷行符號鍵。
Serverless Devs已預設為您產生一個名為start-fc-event-nodejs12的專案,您可按需修改專案名稱,本文以預設的專案名稱為例。
輸出樣本如下。
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
Please input your project name (init dir) start-fc-event-nodejs12
file decompression completed
please select credential alias (Use arrow keys)
❯ default
按需選擇別名,然後按斷行符號鍵。
按需選擇是否部署該專案。
由於本樣本介紹的專案還需安裝依賴,所以在是否立即部署該專案?(Y/n)
後輸入n
選擇不部署該專案。
執行以下命令,進入專案的代碼目錄。
cd start-fc-event-nodejs12/code
在代碼目錄中,執行以下命令,初始化package.json檔案。
輸出樣本如下。
Wrote to /test/jjyy/start-fc-event-nodejs12/code/package.json:
{
"name": "code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
執行以下命令,安裝Jaeger依賴。
npm i jaeger-client --save
輸出樣本如下。
編輯函數代碼。
代碼內容如下所示。
var initTracer = require('jaeger-client').initTracer;
var spanContext = require('jaeger-client').SpanContext;
module.exports.handler = function(event, context, callback)
{
console.log('invoking...',context.tracing);
var config = {
serviceName: 'e2e-test',
reporter: {
collectorEndpoint: context.tracing.jaegerEndpoint,
flushIntervalMs: 10,
},
sampler: {
type: "const",
param: 1
},
};
var options = {
tags: {
'version': 'fc-e2e-tags',
},
};
var tracer = initTracer(config, options);
var invokSpanContextStr = context.tracing.openTracingSpanContext;
console.log('spanContext', invokSpanContextStr);
var invokSpanContext = spanContext.fromString(invokSpanContextStr);
var span = tracer.startSpan("CustomSpan", {
childOf: invokSpanContext
});
span.finish();
var params = {
openTracingSpanContext: context.tracing.openTracingSpanContext,
openTracingSpanBaggages: context.tracing.openTracingSpanBaggages,
jaegerEndpoint: context.tracing.jaegerEndpoint
}
callback(null,'success')
}
編輯s.yaml檔案。
執行以下命令,進入專案目錄。
執行以下命令,編輯s.yaml檔案。
檔案樣本如下。
edition: 1.0.0
name: hello-world-app
access: default
vars:
region: "cn-hangzhou"
service:
name: "hello-world-service"
description: 'hello world by serverless devs'
tracingConfig: Enable
services:
helloworld:
component: fc
props:
region: ${vars.region}
service: ${vars.service}
function:
name: "start-fc-event-nodejs14"
description: 'hello world by serverless devs'
runtime: nodejs12
codeUri: ./code
handler: index.handler
memorySize: 128
timeout: 60
執行以下命令,安裝依賴。
輸出樣本如下。
[2021-11-12T18:33:43.856] [INFO ] [S-CLI] - Start ...
[2021-11-12T18:33:44.677] [INFO ] [FC-BUILD] - Build artifact start...
[2021-11-12T18:33:44.701] [INFO ] [FC-BUILD] - Use docker for building.
[2021-11-12T18:33:44.988] [INFO ] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs12:build-1.9.21
[2021-11-12T18:33:45.011] [INFO ] [FC-BUILD] - skip pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs12:build-1.9.21...
[2021-11-12T18:33:47.915] [INFO ] [FC-BUILD] - Build artifact successfully.
Tips for next step
======================
* Invoke Event Function: s local invoke
* Invoke Http Function: s local start
* Deploy Resources: s deploy
End of method: build
執行以下命令,部署專案。
輸出樣本如下。
[2021-11-12T18:35:26.015] [INFO ] [S-CLI] - Start ...
[2021-11-12T18:35:26.633] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
[2021-11-12T18:35:26.634] [INFO ] [FC-DEPLOY] - Using access alias: default
[2021-11-12T18:35:26.634] [INFO ] [FC-DEPLOY] - Using accessKeyID: yourAccessKeyID
......
Make service fc-deploy-service success.
Make function fc-deploy-service/event-nodejs12 success.
[2021-11-12T18:35:37.661] [INFO ] [FC-DEPLOY] - Checking Service fc-deploy-service exists
[2021-11-12T18:35:37.718] [INFO ] [FC-DEPLOY] - Checking Function event-nodejs12 exists
Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function
* Remove Trigger: s remove trigger
* Remove CustomDomain: s remove domain
fc-deploy-test:
region: cn-hangzhou
service:
name: fc-deploy-service
function:
name: event-nodejs12
runtime: nodejs12
handler: index.handler
memorySize: 128
timeout: 60
結果驗證。
在Function Compute控制台查看調用鏈,可以看到您剛建立的自訂Span已與Function Compute的系統Span串連起來。

使用OpenTelemetry
目前,支援使用OpenTelemetry方式建立自訂Span的運行環境及其範例程式碼如下所示。
本文以Python運行時為例,介紹如何通過Serverless Devs建立自訂Span以及建立並部署函數。
說明
您可以使用Serverless Devs安裝依賴包,並打包部署到Function Compute,您也可以通過其他方式打包並部署。Serverless Devs的更多資訊,請參見什麼是Serverless Devs。
建立代碼目錄。
進入代碼目錄。
初始化Python運行時模板。
執行以下命令,初始化Python 3的專案。
s init devsapp/start-fc-event-python3
輸出樣本:
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
Please input your project name (init dir) (start-fc-event-python3)
設定專案名稱,然後按斷行符號鍵。
Serverless Devs已預設為您產生一個名為start-fc-event-python3的專案,您可按需修改該名稱,本文以預設的專案名稱為例。
輸出樣本如下。
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
Please input your project name (init dir) start-fc-event-python3
file decompression completed
please select credential alias (Use arrow keys)
❯ default
按需選擇別名,然後按斷行符號鍵。
按需選擇是否部署該專案。
由於本樣本介紹的專案還需安裝依賴,所以在是否立即部署該專案?(Y/n)
後輸入n
選擇不部署該專案。
安裝OpenTelemetry依賴。
執行以下命令,進入代碼目錄。
cd start-fc-event-python3/code
在代碼目錄中建立requirements.txt檔案,然後編輯該檔案,檔案內容如下所示。
opentelemetry-api==1.12.0
opentelemetry-sdk==1.12.0
opentelemetry-exporter-jaeger==1.12.0
執行以下命令,編輯函數代碼。
函數代碼內容如下所示。
import time
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.trace import NonRecordingSpan
trace.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "my-helloworld-service"})
)
)
tracer = trace.get_tracer(__name__)
def handler(event, context):
init_tracer(context.tracing.jaeger_endpoint)
span_context = get_fc_span(context.tracing.span_context)
start_my_span(trace.set_span_in_context(NonRecordingSpan(span_context)))
return 'hello world'
def init_tracer(endpoint):
jaeger_exporter = JaegerExporter(
collector_endpoint=endpoint
)
span_processor = SimpleSpanProcessor(jaeger_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
def get_fc_span(jaeger_span_context):
jaeger_span_context_arr = jaeger_span_context.split(":")
tid = int(jaeger_span_context_arr[0], 16)
sid = int(jaeger_span_context_arr[1], 16)
span_context = trace.SpanContext(
trace_id=tid,
span_id=sid,
is_remote=True,
trace_flags=trace.TraceFlags(0x01),
)
return span_context
def start_my_span(context):
with tracer.start_as_current_span(name="fc-operation", context=context):
time.sleep(0.15)
with tracer.start_as_current_span("child"):
time.sleep(0.1)
編輯完函數代碼後,輸入:wq,然後按斷行符號鍵,退出編輯頁面。
編輯s.yaml檔案。
執行以下命令,進入專案目錄。
執行以下命令,編輯s.yaml檔案。
檔案樣本如下。
edition: 1.0.0
name: hello-world-app
access: default
vars:
region: "cn-hangzhou"
service:
name: "hello-world-service"
description: 'hello world by serverless devs'
tracingConfig: Enable
services:
helloworld:
component: fc
props:
region: ${vars.region}
service: ${vars.service}
function:
name: "start-fc-event-python3"
description: 'hello world by serverless devs'
runtime: python3.9
codeUri: ./code
handler: index.handler
memorySize: 128
timeout: 60
編輯完成代碼後,輸入:wq,然後按斷行符號鍵,退出編輯頁面。
安裝依賴。
輸出樣本如下。
[2021-11-12T18:53:05.818] [INFO ] [S-CLI] - Start ...
[2021-11-12T18:53:06.638] [INFO ] [FC-BUILD] - Build artifact start...
[2021-11-12T18:53:06.659] [INFO ] [FC-BUILD] - Use docker for building.
[2021-11-12T18:53:06.888] [INFO ] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.21
[2021-11-12T18:53:06.942] [INFO ] [FC-BUILD] - skip pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.21...
[2021-11-12T18:53:10.570] [INFO ] [FC-BUILD] - Build artifact successfully.
Tips for next step
======================
* Invoke Event Function: s local invoke
* Invoke Http Function: s local start
* Deploy Resources: s deploy
End of method: build
執行以下命令,部署專案。
輸出樣本如下。
[2021-11-12T18:55:02.640] [INFO ] [S-CLI] - Start ...
[2021-11-12T18:55:03.455] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
[2021-11-12T18:55:03.456] [INFO ] [FC-DEPLOY] - Using access alias: default
[2021-11-12T18:55:03.456] [INFO ] [FC-DEPLOY] - Using accessKeyID: yourAccessKeyID
[2021-11-12T18:55:03.457] [INFO ] [FC-DEPLOY] - Using accessKeySecret: yourAccessKeySecret
Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
[2021-11-12T18:55:04.142] [INFO ] [FC-DEPLOY] - Checking Service fc-deploy-service exists
[2021-11-12T18:55:04.722] [INFO ] [FC-DEPLOY] - Checking Function event-py3 exists
[2021-11-12T18:55:04.831] [INFO ] [FC-DEPLOY] - Fc detects that you have run build command for function: event-py3.
[2021-11-12T18:55:04.831] [INFO ] [FC-DEPLOY] - Using codeUri: /test/jjyy/opentelemetry-demo/start-fc-event-python3/.s/build/artifacts/fc-deploy-service/event-py3
[2021-11-12T18:55:04.835] [INFO ] [FC-DEPLOY] - Fc add/append some content to your origin environment variables for finding dependencies generated by build command.
{
"LD_LIBRARY_PATH": "/code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib",
"PATH": "/code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin",
"NODE_PATH": "/code/node_modules:/usr/local/lib/node_modules",
"PYTHONUSERBASE": "/code/.s/python"
}
Make service fc-deploy-service success.
Make function fc-deploy-service/event-py3 success.
[2021-11-12T18:55:10.693] [INFO ] [FC-DEPLOY] - Checking Service fc-deploy-service exists
[2021-11-12T18:55:10.737] [INFO ] [FC-DEPLOY] - Checking Function event-py3 exists
Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function
* Remove Trigger: s remove trigger
* Remove CustomDomain: s remove domain
fc-deploy-test:
region: cn-hangzhou
service:
name: fc-deploy-service
function:
name: event-py3
runtime: python3 handler: index.handler
memorySize: 128
timeout: 60
結果驗證。
在Function Compute控制台查看調用鏈,可以看到您剛建立的自訂Span已與Function Compute的系統Span串連起來。
