通過OpenTelemetry上報Ruby應(yīng)用數(shù)據(jù)
通過OpenTelemetry為應(yīng)用埋點(diǎn)并上報鏈路數(shù)據(jù)至可觀測鏈路 OpenTelemetry 版后,可觀測鏈路 OpenTelemetry 版即可開始監(jiān)控應(yīng)用,您可以查看應(yīng)用拓?fù)洹⒄{(diào)用鏈路、異常事務(wù)、慢事務(wù)和SQL分析等一系列監(jiān)控數(shù)據(jù)。本文介紹如何使用OpenTelemetry為Ruby應(yīng)用埋點(diǎn)并上報數(shù)據(jù)。
前提條件
新版控制臺
登錄可觀測鏈路 OpenTelemetry 版控制臺,在左側(cè)導(dǎo)航欄單擊接入中心。
在開源框架區(qū)域單擊OpenTelemetry卡片。
在彈出的OpenTelemetry面板中選擇數(shù)據(jù)需要上報的地域。
說明初次接入的地域?qū)詣舆M(jìn)行資源初始化。
選擇連接方式和上報方式,然后復(fù)制接入點(diǎn)信息。
連接方式:若您的服務(wù)部署在阿里云上,且所屬地域與選擇的接入地域一致,推薦使用阿里云內(nèi)網(wǎng)方式,否則選擇公網(wǎng)方式。
上報方式:根據(jù)客戶端支持的協(xié)議類型選擇HTTP或gRPC協(xié)議上報數(shù)據(jù)。
舊版控制臺
在左側(cè)導(dǎo)航欄單擊集群配置,然后在右側(cè)頁面單擊接入點(diǎn)信息頁簽。
在頁面頂部選擇需要接入的地域,然后在集群信息區(qū)域打開顯示Token開關(guān)。
在客戶端采集工具區(qū)域單擊OpenTelemetry。
在相關(guān)信息列中,獲取接入點(diǎn)信息。
說明如果應(yīng)用部署于阿里云生產(chǎn)環(huán)境,則選擇阿里云VPC網(wǎng)絡(luò)接入點(diǎn),否則選擇公網(wǎng)接入點(diǎn)。
背景信息
Ruby版本限制:MRI Ruby ≥ 3.0,jruby ≥ 9.3.2.0,或者truffleruby ≥ 22.1。
OpenTelemetry Ruby支持手動埋點(diǎn)和半自動埋點(diǎn)(無需手動創(chuàng)建Span,但需添加一個配置文件),其中半自動埋點(diǎn)支持的框架請參見OpenTelemetry官方文檔。
action_pack
action_view
active_job
active_model_serializers
active_record
active_support
aws_sdk
bunny
concurrent_ruby
dalli
delayed_job
ethon
excon
faraday
grape
graphql
gruf
http
http_client
httpx
koala
lmdb
mongo
mysql2
net_http
pg
que
racecar
rack
rails
rake
rdkafka
redis
resque
restclient
rspec
ruby_kafka
sidekiq
sinatra
trilogy
示例Demo
示例代碼倉庫地址:ruby-opentelemetry-demo
方法一:使用HTTP協(xié)議上報數(shù)據(jù)
安裝手動埋點(diǎn)所需的OpenTelemetry相關(guān)依賴。
gem install opentelemetry-api gem install opentelemetry-sdk gem install opentelemetry-exporter-otlp
OpenTelemetry初始化。
添加導(dǎo)出觀測數(shù)據(jù)的組件,將
<endpoint>
替換成前提條件中獲取的對應(yīng)地域的Endpoint。require 'opentelemetry/sdk' require 'opentelemetry-exporter-otlp' OpenTelemetry::SDK.configure do |c| c.add_span_processor( OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( OpenTelemetry::Exporter::OTLP::Exporter.new( endpoint: '<endpoint>' # HTTP方式接入 ) ) ) c.resource = OpenTelemetry::SDK::Resources::Resource.create({ OpenTelemetry::SemanticConventions::Resource::SERVICE_NAMESPACE => 'tracing', OpenTelemetry::SemanticConventions::Resource::SERVICE_NAME => 'ruby_demo', # 通過OpenTelemetry上報的Ruby應(yīng)用名 OpenTelemetry::SemanticConventions::Resource::SERVICE_VERSION => '0.0.1', }) end
獲取Tracer并創(chuàng)建新的Span。
tracer = OpenTelemetry.tracer_provider.tracer('<your_tracer_name>', '0.1.0') tracer.in_span('parent_span') do |parent_span| # ... end
獲取當(dāng)前Span,向當(dāng)前Span添加信息并獲取Trace ID和Span ID。
# ... tracer.in_span('parent_span') do |parent_span| # current_span即parent_span current_span = OpenTelemetry::Trace::current_span current_span.set_attribute('key', 'value') pp current_span.context.trace_id pp current_span.context.span_id end
創(chuàng)建嵌套的Span。
# ... tracer.in_span('parent_span') do |parent_span| # ... tracer.in_span('child_span') do |child_span| # 此時current_span是child_span current_span = OpenTelemetry::Trace::current_span pp current_span end end
require 'opentelemetry/sdk' require 'opentelemetry-exporter-otlp' OpenTelemetry::SDK.configure do |c| c.add_span_processor( OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( OpenTelemetry::Exporter::OTLP::Exporter.new( endpoint: '<endpoint>' # HTTP方式接入 ) ) ) c.resource = OpenTelemetry::SDK::Resources::Resource.create({ OpenTelemetry::SemanticConventions::Resource::SERVICE_NAMESPACE => 'tracing', OpenTelemetry::SemanticConventions::Resource::SERVICE_NAME => 'ruby_demo', # 通過OpenTelemetry上報的Ruby應(yīng)用名 OpenTelemetry::SemanticConventions::Resource::SERVICE_VERSION => '0.0.1', }) # 不使用OpenTelemetry Resources API來設(shè)置應(yīng)用名 # c.service_name = 'ruby_demo' end tracer = OpenTelemetry.tracer_provider.tracer('instrumentation_library_name', '0.1.0') tracer.in_span('parent_span') do |parent_span| # 設(shè)置Attribute parent_span.set_attribute('language', 'ruby') parent_span.set_attribute("attribute_key", ["attribute_value1", "attribute_value1", "attribute_value1"]) # 添加Event parent_span.add_event("event", attributes: { "pid" => 1234, "signal" => "SIGHUP" }) # 獲取Trace ID與當(dāng)前Span的Span ID current_span = OpenTelemetry::Trace::current_span pp current_span.context.trace_id pp current_span.context.span_id tracer.in_span('child_span') do |child_span| child_span.add_attributes({ "key1" => "value1", "key2" => "value2" }) child_span.add_event("mock exception here") begin raise 'An error has occurred' rescue # 發(fā)生異常時,將child_span的status設(shè)置為error child_span.status = OpenTelemetry::Trace::Status.error("error in child span") end pp child_span end end sleep 10
運(yùn)行程序。
ruby manual.rb
方法二:自動上報
OpenTelemetry Ruby也可以自動在應(yīng)用程序中埋點(diǎn),實(shí)現(xiàn)自動觀測。下面以基于Rails框架的Ruby Web應(yīng)用為例,演示使用OpenTelemetry自動追蹤鏈路并上報數(shù)據(jù)。
下載開源Web應(yīng)用框架Rails。
gem install rails
使用Rails創(chuàng)建Web項(xiàng)目。
rails new <your-project-name>
請將
<your-project-name>
替換為應(yīng)用名,例如:rails new auto-demo
。如果運(yùn)行命令后出現(xiàn)
Rails is not currently installed on this system.
的報錯,請關(guān)閉終端并重新打開,然后在新打開的終端中重新輸入命令。
在應(yīng)用目錄下的Gemfile文件中添加以下內(nèi)容。
gem 'opentelemetry-sdk' gem 'opentelemetry-exporter-otlp' gem 'opentelemetry-instrumentation-all'
下載此Web應(yīng)用所需的第三方依賴。
進(jìn)入項(xiàng)目根目錄。
cd <your-project-name>
下載Ruby依賴管理工具Bundler。
gem install bundler
下載Gemfile中的依賴。
bundle install
在<your-project-name>/config/initializers目錄下創(chuàng)建opentelemetry.rb文件,并添加以下內(nèi)容。
# config/initializers/opentelemetry.rb require 'opentelemetry/sdk' require 'opentelemetry/exporter/otlp' require 'opentelemetry/instrumentation/all' OpenTelemetry::SDK.configure do |c| c.add_span_processor( OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( OpenTelemetry::Exporter::OTLP::Exporter.new( endpoint: '<endpoint>' # HTTP方式接入 ) ) ) c.resource = OpenTelemetry::SDK::Resources::Resource.create({ OpenTelemetry::SemanticConventions::Resource::HOST_NAME => '<your-host-name>', # 主機(jī)名 }) c.service_name = '<your-service-name>' # 服務(wù)名 c.use_all() # 自動觀測opentelemetry支持的所有庫, end
將
<endpoint>
替換成前提條件中獲取的對應(yīng)地域的Endpoint。根據(jù)實(shí)際情況替換
<your-host-name>
和<your-service-name>
。
運(yùn)行項(xiàng)目。
rails server
如果輸出以下內(nèi)容,則表示運(yùn)行成功。
* Puma version: 5.6.5 (ruby 2.7.2-p137) ("Birdie's Version") * Min threads: 5 * Max threads: 5 * Environment: development * PID: 79842 * Listening on http://127.0.0.1:3000 * Listening on http://[::1]:3000 Use Ctrl-C to stop
在瀏覽器中訪問
http://127.0.0.1:3000
,終端輸出以下內(nèi)容則說明數(shù)據(jù)已上報至可觀測鏈路 OpenTelemetry 版控制臺。Started GET "/" for 127.0.0.1 at 2023-01-01 10:00:00 +0800 Processing by Rails::WelcomeController#index as HTML Rendering /Users/username/.rvm/gems/ruby-2.7.2/gems/railties-7.0.4.3/lib/rails/templates/rails/welcome/index.html.erb Rendered /Users/username/.rvm/gems/ruby-2.7.2/gems/railties-7.0.4.3/lib/rails/templates/rails/welcome/index.html.erb (Duration: 0.8ms | Allocations: 665) Completed 200 OK in 6ms (Views: 2.1ms | ActiveRecord: 0.0ms | Allocations: 5440)
查看監(jiān)控數(shù)據(jù)
在可觀測鏈路 OpenTelemetry 版控制臺的應(yīng)用列表頁面選擇目標(biāo)應(yīng)用,查看鏈路數(shù)據(jù)。