Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ActiveSupport::Notifications supporting instrum...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Y.Matsuda
November 08, 2024
Programming
1.1k
1
Share
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
Y.Matsuda
November 08, 2024
More Decks by Y.Matsuda
See All by Y.Matsuda
Accelerating the Feedback Loop of OpenTelemetry Instrumentation with otel-tui
ymtdzzz
1
730
Observability Technology Selection Tips
ymtdzzz
6
1.6k
Building Observability Infrastructure with OpenTelemetry and SaaS
ymtdzzz
2
1.6k
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
5
5k
OIDC仕様に準拠した Makuake ID連携基盤構築の裏側
ymtdzzz
3
2.8k
Other Decks in Programming
See All in Programming
UaaL×Androidアプリのメモリ計測 — Memory Profilerの先へ
rio432
0
160
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
290
Agentic UI in the Frontend: Architectures with Open Standards @JAX 2026 in Mainz
manfredsteyer
PRO
0
120
AI時代になぜ書くのか
mutsumix
0
440
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
120
Import assertionsが消えた日~ECMAScriptの仕様はどう決まり、なぜ覆るのか~
bicstone
2
190
新規プロダクトを高速で生み出すハーネスエンジニアリング
seanchas116
3
220
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
400
ECR拡張スキャンでSBOMを収集して サプライチェーン攻撃の影響調査を 爆速で終わらせてみた
akihisaikeda
1
170
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
1k
AIエージェントの隔離技術の徹底比較
kawayu
0
360
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
4
410
Featured
See All Featured
A better future with KSS
kneath
240
18k
Balancing Empowerment & Direction
lara
6
1.1k
Code Review Best Practice
trishagee
74
20k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
290
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
510
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
180
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.7k
SEO for Brand Visibility & Recognition
aleyda
0
4.5k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
330
Mobile First: as difficult as doing things right
swwweet
225
10k
Transcript
@ymtdzzz OpenTelemetryによるRailsアプリの計装を 支えるActiveSupport::Notifications 2024.11.08 Fri.Kaigi on Rails 2024事後勉強会 @SmartHR
Yosuke MATSUDA (@ymtdzzz) 株式会社SmartHR プロダクトエンジニア 2
お久しぶりです&はじめまして 3 出典:モノリスでも使える! OpenTelemetryでRailsアプリのパフォーマンス分析を始めてみよう from Kaigi on Rails 2024
やっぱり今日も OpenTelemetry (トレース)
おさらい:トレースとは 5
6 トレース:追跡の単位(リクエストなど) おさらい:トレースとは
7 スパン:処理の単位 おさらい:トレースとは
8 トレースを出力すること:計装 • 自動計装: トレースを自動で出力させる(Gem毎に用 意) • 手動計装: トレースを手動で出力する
9 今日の話はこっち • 自動計装: トレースを自動で出力させる(Gem毎に用 意) • 手動計装: トレースを手動で出力する Railsの自動計装の仕組みはどうなって
いるのか?
10 OpenTelemetry:計装の技術標準 • 仕様(OpenTelemetry Specification) • 実装(言語別 SDK、計装ライブラリ) ※他にも色々ありますが、今回は割愛
計装のために利用する OpenTelemetryライブラリ 11
計装のために利用する OpenTelemetryライブラリ 12 普段私たちが使うのは 計装ライブラリ とRuby SDK
Ruby SDK?計装ライブラリ?
どちらも計装のためのライブラリ • Ruby SDK(opentelemetry-ruby) ◦ トレース設定(出力先やサービス名の設定など) ◦ 手動でスパンを出力したい場合に利用 • 計装ライブラリ
(opentelemetry-ruby-contrib/instrumentation) ◦ Gemに合わせたスパンを自動で出力してくれる ◦ たくさんあります(faraday, grape, mongo, pg, …) 14
どちらも計装のためのライブラリ • Ruby SDK(opentelemetry-ruby) ◦ トレース設定(出力先やサービス名の設定など) ◦ 手動でスパンを出力したい場合に利用 • 計装ライブラリ
(opentelemetry-ruby-contrib/instrumentation) ◦ Gemに合わせたスパンを自動で出力してくれる ◦ たくさんあります(faraday, grape, mongo, pg, …) 15 今日のテーマ
ところで:スパンを手動で生成したいケース 16 出典:Tuning GraphQL on Rails (p.37) from Kaigi on
Rails 2024
Railsの計装ライブラリを覗いてみよう
ここの話 18 自動でスパン(トレース) 出してやんよ!
ActionMailerの計装ライブラリ 19 https://github.com/open-telemetry/opentelemetry-ruby-contrib/ tree/main/instrumentation z
セットアップはこれだけ OpenTelemetry::SDK.configure do |c| # サービス名や出力先などの初期設定は省略 c.use 'OpenTelemetry::Instrumentation::ActionMailer' end 20
• これだけで勝手にスパンが出始める ◦ OpenTelemetry::SDK.configure ◦ ブロック内でConfigurator#use 何が起こっている?
前提知識: ActiveSupport::Notifications 21 • Rails内部のイベントを subscribe し、計測を行う仕組み • ActiveSupport::Notifications.sub scribeでRailsのイベントに
hookし て処理を走らせる 出典)Active Support Instrumentation で計測 - Railsガイド
処理を追ってみる (超スピードで)
まずはRailtieでconfigのinitialize直後にsubscribe 23 スパン生成用の Subscription Handlerでsubscribe Subscribeしたいイベント名 (今回は ActionMailer)
計装ライブラリの登録( Ruby SDK) 24 useで、指定した計装ライブラリを 配列にpush 計装ライブラリの installを呼び出し
ActionMailerの計装ライブラリの installを見てみる 25 イベントの payloadをスパンの attributeに詰める処理
計装ライブラリの独自 Subscription Handlerでイベントの開始と終了時に スパン生成処理を差し込む 26 Subscriberが読み込まれる流れについては時間の都合上省略 🙏 transform_payloadも最終的 にここで実行される( payloadの
Span Attributeへのセット) start, finishによりイベントの前 処理、後処理でスパンを生成す る
• Railsのイベントを ActiveSupport::Notificationsでsubscribe • 独自のSubscription Handlerを利用 ◦ イベントのstart, finishでスパンの生成とattributeセット •
Configuratorでuseしたinstrumentation libraryのinstall実行 ◦ hook時の処理(transform_payloadなど)を設定し有効化 Railsの計装ライブラリの実装ざっくりまとめ 27 Railsの仕組みに上手く乗っかっているこ とがわかりました!
モンキーパッチは使ってないの?
使っています(例: ActiveRecord) 29
モンキーパッチといえば …… 30 出典:現実のRuby/Railsアップグレード (p.33) from Kaigi on Rails 2024
Railsの計装ライブラリでもモンキーパッチをやめる動き 31 Github issue: opentelemetry-ruby-contrib #218
発端:ActionPackの計装ライブラリ 32 Github PR: opentelemetry-ruby-contrib #123 計装ライブラリのモンキーパッチが原因でリクエスト内容が意図せ ず書き換わってしまう問題
まとめ 33 • OpenTelemetryの計装で利用するライブラリ ◦ Ruby SDK:トレース設定、手動計装 ◦ 計装ライブラリ:Gem毎の自動計装 •
Railsの計装ライブラリ ◦ 多くはActiveSupport::Notificationsを利用 ◦ モンキーパッチも一部あるけど、危ないので ActiveSupport::Notificationsを利用する流れへ
ありがとうございました!
参考 35 • Ruby on Rails API • OpenTelemetry 公式ドキュメント
• Github - rails v7.2.2 • Github - opentelemetry-ruby • Github - opentelemetry-ruby-contrib ※スライド内に直接出典を記載したものを除く