owned this note
owned this note
Published
Linked with GitHub
# pprotein導入
## Goのアップデート
https://go.dev/dl/ からバイナリをダウンロードして更新
```
wget https://go.dev/dl/go1.21.3.linux-amd64.tar.gz
sudo rm -rf /home/isucon/local/go
tar -C /home/isucon/local -xzf go1.21.3.linux-amd64.tar.gz
```
更新されたのを確認
```
$ go version
go version go1.21.3 linux/amd64
```
## 必要なツールのインストール
alpとslpをインストール
```
go install github.com/tkuchiki/alp/cmd/alp@latest
go install github.com/tkuchiki/slp/cmd/slp@latest
```
## ミドルウェアに設定追加
### nginx
https://github.com/tkuchiki/alp#nginx
```
sudo vim /etc/nginx/nginx.conf
```
`log_format` のところを追加
```conf
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format ltsv "time:$time_local"
"\thost:$remote_addr"
"\tforwardedfor:$http_x_forwarded_for"
"\treq:$request"
"\tstatus:$status"
"\tmethod:$request_method"
"\turi:$request_uri"
"\tsize:$body_bytes_sent"
"\treferer:$http_referer"
"\tua:$http_user_agent"
"\treqtime:$request_time"
"\tcache:$upstream_http_x_cache"
"\truntime:$upstream_http_x_runtime"
"\tapptime:$upstream_response_time"
"\tvhost:$host";
access_log /var/log/nginx/access.log ltsv;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
}
```
設定を反映
```
sudo systemctl reload nginx
```
権限を付ける
```
sudo chmod +rx /var/log/nginx
sudo chmod +r /var/log/nginx/access.log
```
## mysql
```
$ sudo mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 36
Server version: 10.3.38-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set global slow_query_log_file = '/var/log/mysql/mysql-slow.log';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> set global long_query_time = 0;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> set global slow_query_log = ON;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> exit
Bye
```
:::warning
この設定は再起動すると消えることに注意!
再起動しても設定を永続化させたい場合は、my.cnfなどに設定を追加する。
:::
権限を付ける
```
sudo chmod +rx /var/log/mysql
sudo chmod +r /var/log/mysql/mysql-slow.log
```
## コードの修正
```go
package main
import (
// 省略
"github.com/kaz/pprotein/integration/echov4" // 追加
)
// 省略
func main() {
e := echo.New()
e.Debug = true
e.Logger.SetLevel(log.DEBUG)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
echov4.EnableDebugHandler(e) // 追加
// 省略
func postInitialize(c echo.Context) error {
// 省略
// 追加
go func() {
if _, err := http.Get("http://localhost:9000/api/group/collect"); err != nil {
log.Printf("failed to communicate with pprotein: %v", err)
}
}()
return c.JSON(http.StatusOK, InitializeResponse{
Language: "go",
})
}
```
```
go mod tidy
go build -o isucondition main.go
sudo systemctl restart isucondition.go
```
## pproteinのダウンロード・起動
https://github.com/kaz/pprotein/releases
```
wget https://github.com/kaz/pprotein/releases/download/1.2.1/pprotein_1.2.1_linux_amd64.tar.gz
tar -xzf pprotein_1.2.1_linux_amd64.tar.gz
./pprotein
```
これで起動します。
```
ssh isucon@<IPアドレス> -L 9000:localhost:9000
```
などでポートフォワードすると、手元の http://localhost:9000 でpproteinを開けるはずです。
## pproteinの設定変更
http://localhost:9000/#/setting/ にアクセスして、設定を変更します。
```
[
{
"Type": "pprof",
"Label": "localhost",
"URL": "http://localhost:3000/debug/pprof/profile",
"Duration": 60
},
{
"Type": "httplog",
"Label": "localhost",
"URL": "http://localhost:3000/debug/log/httplog",
"Duration": 60
},
{
"Type": "slowlog",
"Label": "localhost",
"URL": "http://localhost:3000/debug/log/slowlog",
"Duration": 60
}
]
```
:::info
これで最低限のセットアップは完了です。
ベンチマーカーインスタンスから
```
./bench -all-addresses 192.168.0.11 -target 192.168.0.11:443 -tls -jia-service-url http://192.168.0.10:4999
```
などを投げると
![](https://hackmd.io/_uploads/HkBTN5lfp.png)
のように計測結果を見られるはずです。
:::
## おまけ:alpの設定
ISUCON11予選問題向けの僕のalpの設定を貼っておきます。
```yaml
matching_groups:
- ^/api/condition/.+[0-9a-f\-]$
- ^/api/isu/.+[0-9a-f\-]$
- ^/api/isu/.+[0-9a-f\-]/icon$
- ^/api/isu/.+[0-9a-f\-]/graph$
- ^/isu/.+[0-9a-f\-]/condition$
- ^/isu/.+[0-9a-f\-]/graph$
- ^/isu/.+[0-9a-f\-]$
```
これをhttplog/configに設定すると良い感じにログが見られるはずです。
![](https://hackmd.io/_uploads/BJo8rqgM6.png)