Setelah mengaktifkan Managed Service for OpenTelemetry, Function Compute mencatat waktu yang dikonsumsi dalam sistem, termasuk durasi cold start, waktu eksekusi Initializer hooks, dan waktu eksekusi fungsi. Untuk mencatat waktu yang dikonsumsi oleh fungsi di sisi bisnis, Anda dapat membuat rentang kustom. Sebagai contoh, Anda dapat mencatat waktu yang diperlukan untuk mengakses layanan seperti ApsaraDB RDS dan File Storage NAS (NAS) di dalam fungsi. Topik ini menjelaskan cara menggunakan Serverless Devs untuk membuat rentang kustom.
Prasyarat
Deskripsi proses
Anda dapat membuat rentang kustom berdasarkan jejak dari Function Compute dan menghubungkan rentang tersebut ke jejak dari Function Compute. Untuk Function Compute, Managed Service for OpenTelemetry menyediakan dua metode berikut untuk membuat rentang kustom berdasarkan implementasi Jaeger dari spesifikasi OpenTracing:
Untuk informasi lebih lanjut tentang Jaeger dan OpenTelemetry, lihat Jaeger dan OpenTelemetry.
Kedua metode tersebut melibatkan langkah-langkah berikut:
Tambahkan dependensi Jaeger atau OpenTelemetry ke fungsi. Untuk informasi lebih lanjut tentang cara menginstal dependensi, lihat Instal Dependensi Pihak Ketiga untuk Fungsi.
Dapatkan SpanContext dari jejak Function Compute dari fungsi, buat rentang kustom berdasarkan SpanContext dari Function Compute, lalu tambahkan instrumentasi sebelum dan sesudah fragmen kode yang waktu eksekusinya perlu dicatat.
Lihat informasi jejak pada tab Tracing Analysis halaman Detail Fungsi atau di konsol Managed Service for OpenTelemetry.
Gunakan Jaeger SDK
Topik berikut memberikan kode contoh yang digunakan untuk membuat rentang kustom dengan menggunakan Jaeger SDK dalam runtime yang sesuai:
Bagian ini menjelaskan cara menggunakan Serverless Devs untuk membuat rentang kustom, serta membuat dan menerapkan fungsi dalam runtime Node.js.
Anda dapat menggunakan Serverless Devs untuk menginstal dependensi dan mengemas serta menerapkan kode ke Function Compute. Anda juga dapat mengemas dan menerapkan kode dengan cara lain. Untuk informasi lebih lanjut tentang Serverless Devs, lihat Apa itu Serverless Devs?.
Buat direktori kode.
mkdir jaeger-demoInisialisasi template runtime Node.js.
Jalankan perintah berikut untuk masuk ke direktori kode:
cd jaeger-demoJalankan perintah berikut di direktori kode untuk menginisialisasi proyek Node.js 12:
s init devsapp/start-fc-event-nodejs12Contoh keluaran adalah sebagai berikut:
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome Please input your project name (init dir) (start-fc-event-nodejs12)Tentukan nama untuk proyek, lalu tekan tombol Enter.
Secara default, Serverless Devs menghasilkan proyek bernama start-fc-event-nodejs12. Anda dapat memodifikasi nama proyek sesuai kebutuhan. Dalam contoh ini, nama proyek default digunakan.
Contoh keluaran adalah sebagai berikut:
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) ❯ defaultPilih alias dan tekan tombol Enter.
Tentukan apakah akan menerapkan proyek.
Dependensi harus diinstal untuk proyek dalam contoh ini. Oleh karena itu, ketika
Do you want to deploy the project immediately? (Y/n)muncul di output perintah, masukkann.
Jalankan perintah berikut untuk masuk ke direktori kode proyek:
cd start-fc-event-nodejs12/codeJalankan perintah berikut di direktori kode untuk menginisialisasi file package.json:
npm init -yContoh keluaran adalah sebagai berikut:
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" }Jalankan perintah berikut untuk menginstal dependensi Jaeger:
npm i jaeger-client --saveContoh keluaran adalah sebagai berikut:
added 15 packages in 1sEdit kode fungsi.
Kode berikut digunakan:
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: { // Provide the traces endpoint; this forces the client to connect directly to the Collector and send // spans over HTTP 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') }Edit file s.yaml.
Jalankan perintah berikut untuk masuk ke direktori proyek:
cd ..Jalankan perintah berikut untuk mengedit file s.yaml:
vim s.yamlContoh kode berikut memberikan contoh file:
edition: 1.0.0 name: hello-world-app access: default vars: # Variabel global. region: "cn-hangzhou" service: name: "hello-world-service" description: 'hello world by serverless devs' tracingConfig: Enable # Aktifkan Managed Service for OpenTelemetry. services: helloworld: # Nama layanan atau modul. component: fc # Nama komponen. Serverless Devs dapat dianggap sebagai konsol game, memerlukan modul tambahan untuk memperluas kemampuannya. Setiap komponen bertindak seperti kartu game, memungkinkan kemampuan bisnis yang berbeda. Dengan menggabungkan komponen yang berbeda, Anda dapat membuka berbagai fungsi untuk Serverless Devs, mirip dengan bagaimana konsol game dapat memainkan game yang berbeda dengan kartu yang berbeda. props: region: ${vars.region} # Untuk informasi lebih lanjut tentang cara menggunakan variabel, kunjungi: https://www.serverless-devs.com/serverless-devs/yaml. 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
Jalankan perintah berikut untuk menginstal dependensi:
s build --use-dockerContoh keluaran adalah sebagai berikut:
[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: buildJalankan perintah berikut untuk menerapkan proyek:
s deploy -yContoh keluaran adalah sebagai berikut:
[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: 60Verifikasi hasilnya.
Lihat jejak di konsol Function Compute. Rentang kustom yang Anda buat terhubung ke rentang dari Function Compute.

Gunakan OpenTelemetry
Topik berikut memberikan kode contoh yang digunakan untuk membuat rentang kustom dengan menggunakan OpenTelemetry dalam runtime yang sesuai:
Bagian ini menjelaskan cara menggunakan Serverless Devs untuk membuat rentang kustom, serta membuat dan menerapkan fungsi dalam runtime Python.
Anda dapat menggunakan Serverless Devs untuk menginstal dependensi dan mengemas serta menerapkan kode ke Function Compute. Anda juga dapat mengemas dan menerapkan kode dengan cara lain. Untuk informasi lebih lanjut tentang Serverless Devs, lihat Apa itu Serverless Devs?.
Buat direktori kode.
mkdir opentelemetry-demoMasuk ke direktori kode.
cd opentelemeytry-demoInisialisasi template runtime Python.
Jalankan perintah berikut untuk menginisialisasi proyek Python 3:
s init devsapp/start-fc-event-python3Contoh keluaran adalah sebagai berikut:
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome Please input your project name (init dir) (start-fc-event-python3)Tentukan nama untuk proyek, lalu tekan tombol Enter.
Secara default, Serverless Devs menghasilkan proyek bernama start-fc-event-python3. Anda dapat memodifikasi nama proyek sesuai kebutuhan. Dalam contoh ini, nama proyek default digunakan.
Contoh keluaran adalah sebagai berikut:
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) ❯ defaultPilih alias dan tekan tombol Enter.
Tentukan apakah akan menerapkan proyek.
Dependensi harus diinstal untuk proyek dalam contoh ini. Oleh karena itu, saat
Do you want to deploy the project immediately? (Y/n)muncul di output perintah, masukkann.
Instal dependensi OpenTelemetry.
Jalankan perintah berikut untuk masuk ke direktori kode:
cd start-fc-event-python3/codeBuat file requirements.txt di direktori kode, lalu modifikasi file tersebut. File ini berisi informasi berikut:
opentelemetry-api==1.12.0 opentelemetry-sdk==1.12.0 opentelemetry-exporter-jaeger==1.12.0
Jalankan perintah berikut untuk mengedit kode fungsi:
vim index.pyKode berikut digunakan:
# -*- coding: utf-8 -*- 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): # create a JaegerExporter jaeger_exporter = JaegerExporter( collector_endpoint=endpoint ) # Create a SimpleSpanProcessor and add the exporter to it span_processor = SimpleSpanProcessor(jaeger_exporter) # add to the tracer 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)Setelah Anda mengedit kode fungsi, masukkan :wq dan tekan tombol Enter untuk keluar dari halaman pengeditan.
Edit file s.yaml.
Jalankan perintah berikut untuk masuk ke direktori proyek:
cd ..Jalankan perintah berikut untuk mengedit file s.yaml:
vim s.yamlContoh kode berikut memberikan contoh file:
edition: 1.0.0 name: hello-world-app access: default vars: # Variabel global. region: "cn-hangzhou" service: name: "hello-world-service" description: 'hello world by serverless devs' tracingConfig: Enable # Aktifkan Managed Service for OpenTelemetry. services: helloworld: # Nama layanan atau modul. component: fc # Nama komponen. Serverless Devs dapat dianggap sebagai konsol game, memerlukan modul tambahan untuk memperluas kemampuannya. Setiap komponen bertindak seperti kartu game, memungkinkan kemampuan bisnis yang berbeda. Dengan menggabungkan komponen yang berbeda, Anda dapat membuka berbagai fungsi untuk Serverless Devs, mirip dengan bagaimana konsol game dapat memainkan game yang berbeda dengan kartu yang berbeda. props: region: ${vars.region} # Untuk informasi lebih lanjut tentang cara menggunakan variabel, kunjungi: https://www.serverless-devs.com/serverless-devs/yaml#Nilai Variabel. 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: 60Setelah Anda mengedit kode, masukkan :wq dan tekan tombol Enter untuk keluar dari halaman pengeditan.
Instal dependensi.
s build --use-dockerContoh keluaran adalah sebagai berikut:
[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: buildJalankan perintah berikut untuk menerapkan proyek:
s deploy -yContoh keluaran adalah sebagai berikut:
[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: 60Verifikasi hasilnya.
Lihat jejak di konsol Function Compute. Rentang kustom yang Anda buat terhubung ke rentang dari Function Compute.
