Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ActiveSupport::Notifications supporting instrum...
Search
Y.Matsuda
November 08, 2024
Programming
1.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
930
Observability Technology Selection Tips
ymtdzzz
6
1.8k
Building Observability Infrastructure with OpenTelemetry and SaaS
ymtdzzz
2
2.1k
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
5
5.4k
OIDC仕様に準拠した Makuake ID連携基盤構築の裏側
ymtdzzz
3
2.9k
Other Decks in Programming
See All in Programming
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
130
Simple Storage Service(S3) is not simple
iwatsukayura
0
250
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
110
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
650
SREの越境 / SRE Collaboration
y0hgi
2
300
JRuby: Past, Present, and Future
headius
0
230
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
380
AWSに止められる覚悟してますか?
morizo_1984
2
530
CodeRabbitの効果検証と過ごしてみた3ヶ月
armondando
0
220
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
140
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
360
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
620
Featured
See All Featured
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
620
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.3k
RailsConf 2023
tenderlove
30
1.6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Making the Leap to Tech Lead
cromwellryan
135
10k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Rails Girls Zürich Keynote
gr2m
96
14k
Code Review Best Practice
trishagee
74
20k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
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 ※スライド内に直接出典を記載したものを除く