6
6

More than 5 years have passed since last update.

microservicesの調査に向けて〜railsのサンプルアプリにzipkinのtracerを仕込んで動作確認をしてみよう!

Last updated at Posted at 2016-03-09

1. やりたいこと

以前の投稿docker-composeを利用して、OpenZipkinのサービスをシングルインスタンスで構築しました。

どうやって、ここにtrace対象のアプリケーションの情報を登録していくのかな、というところが気になったので、Rubyのgemを利用してみることにしました。

2. 利用してみたリポジトリ

zipkin tracerのIPをconfig/application.rbに記載しているので、application.rbはコミットしていないです。マスキングして後述します。

3. 実施手順

3-1. railsアプリを作成

rails new app
cd app

3-2. Gemfileを編集してbundle installを実行

vim Gemfile
bundle install

Gemfileには以下の2行を追加しました。

gem 'zipkin-tracer'
gem 'faraday', '~> 0.8'

3-3. config/application.rbにzipkin用に、service_name, service_port, json_api_hostを登録する

service_nameは [ hoge app ] としました。

module App
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    zipkin_tracer_config = {service_name: 'hoge app', service_port: 3000, sample_rate: 1, json_api_host: 'http://*.*.*.*:9411'}

    config.middleware.use ZipkinTracer::RackHandler, zipkin_tracer_config


    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
  end
end

ここがちょっとわかりづらかったのですが、

json_api_portの9411は、zipkin docker-compose.yml にある、zipkin-queryが提供しているサービスのポートになります。

query:
  image: openzipkin/zipkin-query:1.33.2
  environment:
    # Remove TRANSPORT_TYPE to disable tracing
    - TRANSPORT_TYPE=http
    - STORAGE_TYPE=cassandra
  expose:
    # The http api is mounted at /api/v1
    - 9411
    # Admin interface is under the http path /admin
    # https://twitter.github.io/twitter-server/Features.html#http-admin-interface
    - 9901
  ports:
    - 9411:9411
    - 9901:9901
  links:
    - cassandra:storage

service_portは、trace対象のサービスのポートになります。
rails sで起動させるので、3000を指定しました。

3-4. rails s でデバッギングモードでrailsアプリを起動する

rails s

3-5. ウェブブラウザで、localhost:3000 にアクセスする

railsのウェルカムページが表示されます。

スクリーンショット 2016-03-09 10.58.50.png

3-6. zipkinの8080ポートのWebUIを見てみる。

スクリーンショット 2016-03-09 11.01.24.png

[ hoge app ]が登録されました。
こちらを選択して、Find Tracesボタンを押下します。

スクリーンショット 2016-03-09 11.02.20.png

レコードが登録されているようです。一つを選択して表示させてみました。

スクリーンショット 2016-03-09 11.02.59.png

実行時間も表示されてますね!

4. 所感

  • Rails以外のweb frameworkでも利用できるのか確認してみたい。play, akkaといったscala系はGitHubにサンプル等ありそうだった。Golangはどうだろ。go-kitを使ったサンプルがあるかもしれないから探してみる
  • http以外のプロトコルで提供している自前サービスに対してHandler実装できるのかな
  • 複数サービスが跨って連携した時のtraceってどうやるんだろうか
  • elixirのphoenixとかdynamoでも使えないのかしら
  • cassandraの内部のデータ構造どうなってるんだろ

面白そうなネタがまだまだありそうな印象です^^


本日は以上となります。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6