# Ruby Apps Import Integration

Treasure Dataは、サーバーサイドのログとイベントを収集し、Rubyアプリケーションからシームレスにデータをインポートするために[td-agent](https://docs.treasuredata.com/smart/project-product-documentation/about-treasure-data-s-server-side-agent)を提供しています。

## 前提条件

- Ruby、Gems、およびBundlerの基本知識
- Treasure Dataの基本知識
- Ruby 1.9以上（ローカルテスト用）


## td-agentのインストール

アプリケーションサーバーに`td-agent`をインストールします。td-agentはアプリケーションサーバー内に配置され、アプリケーションログのクラウドへのアップロードに専念します。

![](/assets/image2020-12-2_13-22-16.1754f4e3154420d54cc976b16d454ef773c83ad5fc4b8580d99a87302c614932.82c084a7.png)

[td-logger-ruby](http://github.com/treasure-data/td-logger-ruby)ライブラリにより、Rubyアプリケーションはローカルのtd-agentにレコードを投稿できます。td-agentは、レコードを受信し、バッファリングし、5分ごとにデータをクラウドにアップロードします。デーモンはローカルノードで実行されるため、ロギングのレイテンシーは無視できます。

## td-agentのインストールオプション

`td-agent`をインストールするには、環境に応じて以下のコマンドのいずれかを実行します。エージェントプログラムは、rpm/deb/dmgなどの各プラットフォームのパッケージ管理ソフトウェアを使用して自動的にインストールされます。

### RHEL/CentOS 5,6,7


```
$ curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh
```

### Ubuntu and Debian


```
# 18.04 Bionic
$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-bionic-td-agent3.sh | sh
# 16.04 Xenial (64bit only)
$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-xenial-td-agent3.sh | sh
```

### EOLバージョンのレガシーサポートはまだ利用可能です


```
# 14.04 Trusty
$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-trusty-td-agent3.sh | sh
# 12.04 Precise
$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-precise-td-agent3.sh | sh
# Debian Stretch (64-bit only) $ curl -L https://toolbelt.treasuredata.com/sh/install-debian-stretch-td-agent3.sh | sh
# Debian Jessie (64-bit only)
$ curl -L https://toolbelt.treasuredata.com/sh/install-debian-jessie-td-agent3.sh | sh
# Debian Squeeze (64-bit only)
$ curl -L https://toolbelt.treasuredata.com/sh/install-debian-squeeze-td-agent2.sh | sh
```

## Amazon Linux

Amazon Linux 1またはAmazon Linux 2を選択できます。[Installing td-agent on Amazon Linux.](https://docs.treasuredata.com/smart/project-product-documentation/installing-td-agent-on-aws-linux)を参照してください。

## MacOS X 10.11+


```
$ open 'https://td-agent-package-browser.herokuapp.com/3/macosx/td-agent-3.1.1-0.dmg'
```

MacOS X 10.11.1（El Capitan）では、いくつかのセキュリティ変更が導入されました。td-agentがインストールされた後、**/Library/LaunchDaemons/td-agent.plist**ファイルを編集して、**/usr/sbin/td-agent**を**/opt/td-agent/usr/sbin/td-agent**に変更してください。

### Windows Server 2012+

Windowsのインストールには、以下の詳細な手順が必要です:

- [Installing Treasure Agent using .msi Installer (Windows)](https://docs.fluentd.org/installation/install-by-msi)


### Opscode Chef Repository


```
$ echo 'cookbook "td-agent"' >> Berksfile
$ berks install
```

[AWS Elastic Beanstalk](https://github.com/treasure-data/elastic-beanstalk-td-agent)もサポートされています。Windowsはサポートされていません。

## /etc/td-agent/td-agent.confの変更

`/etc/td-agent/td-agent.conf`ファイルで`apikey`オプションを設定して、APIキーを指定します。


```conf
# Input from Logging Libraries
<source>
  type forward
  port 24224
</source>

# Treasure Data Output
<match td.*.*>
  type tdlog
  endpoint api.treasuredata.com
  apikey YOUR_API_KEY
  auto_create_table
  buffer_type file
  buffer_path /var/log/td-agent/buffer/td
  use_ssl true
</match>
```

`YOUR_API_KEY` should be your actual apikey string. You can retrieve your API key from your profiles in Treasure Console. Using a [write-only API key](/products/my-settings/getting-your-api-keys) is recommended.

以下の行が設定されたら、エージェントを再起動してください。


```
# Linux
$ sudo /etc/init.d/td-agent restart

# MacOS X
$ sudo launchctl unload /Library/LaunchDaemons/td-agent.plist
$ sudo launchctl load /Library/LaunchDaemons/td-agent.plist
```

td-agentは、ポート24224経由でデータを受け入れ、データをバッファリング（*var/log/td-agent/buffer/td*）し、自動的にクラウドにデータをアップロードします。

## td-logger-rubyの使用

Gemfileに'td' gemを追加します。


```
gem 'td', "~> 0.10.6"
```

レコードを初期化して投稿します。


```
# Initialize
require 'td'
TreasureData::Logger.open_agent('td.test_db', :host=>'localhost', :port=>24224)

# Example1: login event
TD.event.post('login', {:uid=>123})

# Example2: follow event
TD.event.post('follow', {:uid=>123, :from=>'TD', :to=>'Heroku'})

# Example3: pay event
TD.event.post('pay',
              {:uid=>123, :item_name=>'Stone of Jordan',
               :category=>'ring', :price=>100, :count=>1})
```

## データインポートの確認

プログラムを実行します。


```
$ ruby test.rb
```

SIGUSR1シグナルを送信すると、td-agentのバッファがフラッシュされます。アップロードは即座に開始されます。


```
# Linux
$ kill -USR1 `cat /var/run/td-agent/td-agent.pid`

# MacOS X
$ sudo kill -USR1 `sudo launchctl list | grep td-agent | cut -f 1`
```

### Treasure コンソールを使用

データが正常にアップロードされたことを確認するには、データセットを確認してください。

### CLIから

または、TD toolbelt用の[CLI](/tools/cli-and-sdks/td-toolbelt)がある場合は、*td tables*コマンドを発行します。


```
$ td tables
+------------+------------+------+-----------+
| Database   | Table      | Type | Count     |
+------------+------------+------+-----------+
| test_db    | login      | log  | 1         |
| test_db    | follow     | log  | 1         |
| test_db    | pay        | log  | 1         |
+------------+------------+------+-----------+
```

# 本番デプロイ

## Rackベースのサーバーデプロイを使用

*unicorn*、*thin*、*mongrel*などの使用を推奨します。他のセットアップは完全には検証されていません。

## td-agentの高可用性構成

高トラフィックのウェブサイト（5つ以上のアプリケーションノード）の場合、td-agentの高可用性構成を使用して、データ転送の信頼性とクエリパフォーマンスを向上させます。

- [High-Availability Configurations of td-agent](https://docs.treasuredata.com/smart/project-product-documentation/configuring-td-agent-for-high-availability)


## td-agentの監視

td-agent自体の監視も重要です。td-agentの一般的な監視方法については、[Monitoring td-agent](https://docs.treasuredata.com/smart/project-product-documentation/monitoring-td-agent)を参照してください。

td-agentは、[Fluentdプロジェクト](http://fluentd.org/)の下で完全にオープンソース化されています。