Skip to content

最後のメトリクスを取得するAPIを実装する#24

Merged
masa23 merged 7 commits into
masa23:masterfrom
ophum:feat/last-metrics-api
Mar 17, 2026
Merged

最後のメトリクスを取得するAPIを実装する#24
masa23 merged 7 commits into
masa23:masterfrom
ophum:feat/last-metrics-api

Conversation

@ophum

@ophum ophum commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

このPRのBodyはRooCode+AI Engine+Qwen3-Coder-480B-A35B-Instruct-FP8にて生成しました。

PR概要

logreportに内部API機能を追加し、リアルタイムでのメトリックデータ取得を可能にしました。設定により有効化でき、Unix Domain Socketを使用して通信します。

変更内容の詳細説明

  1. APIサーバーの起動処理:

    • 設定ファイルでAPIが有効になっている場合、Unix Domain Socketを介してAPIサーバーが起動します。
    • サーバー起動時にソケットファイルのパーミッションが0700に設定され、セキュリティが考慮されています。
  2. メトリックデータの更新処理:

    • ログから収集されたメトリックデータは、APIのSetLastMetricsメソッドを通じて更新されます。
    • 同時実行制御のためにsync.RWMutexが使用され、データの一貫性が保たれます。
    • 最新のタイムスタンプを持つメトリックのみが保持され、古いデータは削除されます。
    • singleflightグループを使用して、SetLastMetricsメソッドでブロッキングされないようにします。
  3. メトリックデータの保存構造:

    • メトリックデータはキーと値のペアとして保存されます。
    • キーは"ItemName"と"ItemValue"を結合したもので、階層的な構造を表現します。
    • データの整合性を保つためにバージョン管理が行われています。

使用方法

APIエンドポイント

キー一覧の取得

利用可能なメトリックキーの一覧を取得します。

curl --unix-socket /var/run/logreport-api.sock http://localhost/keys

レスポンス例:

["bytes_sent","hit-count.HIT","hit-count.MISS","http.request","http.status.205","http.status.300","http.status.301","http.status.410","http.status.414","http.status.426","http.status.431","http.status.504","https.request","https.status.429","upstream_request_time_max"]

メトリックの取得

指定されたキーのメトリック値を取得します。キーはカンマ区切りで複数指定可能です。

curl --unix-socket /var/run/logreport-api.sock http://localhost/metrics?keys=http.request,https.request,http.status.200,https.status.200

レスポンス例:

{"http.request":5,"http.status.200":0,"https.request":5,"https.status.200":1,"time":"2026-03-16T21:29:42+09:00"}

設定例

APIを有効化する最小限の設定例:

API:
  Enabled: true
  SocketPath: "/var/run/logreport-api.sock"

@ophum ophum force-pushed the feat/last-metrics-api branch from 23b93c2 to 9b42f6d Compare March 16, 2026 12:33
@ophum ophum marked this pull request as ready for review March 16, 2026 12:46
@masa23 masa23 requested a review from Copilot March 16, 2026 16:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

logreport に Unix Domain Socket 経由の内部 API を追加し、直近のメトリクス(キー一覧 / 値)をリアルタイム取得できるようにする PR です。設定で API を有効化し、集計処理から API 用の「最後のメトリクス」を更新します。

Changes:

  • internal/api/keys/metrics を提供する API サーバー実装を追加
  • 設定 (API.Enabled, API.SocketPath) とドキュメント/サンプル設定を追加
  • cmd/logreport でソケット listen・サーバ起動・メトリクス更新の呼び出しを追加

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
internal/api/api.go 内部 API サーバーと last metrics 保持・取得ロジックを追加
cmd/logreport/main.go API サーバー起動(UDS)と集計結果の API 反映処理を追加
config.go API 設定項目(Enabled/SocketPath)を追加
cmd/logreport/config.sample.yaml API 設定例を追加
README.md 内部 API の利用方法を追記
go.mod / go.sum golang.org/x/sync を追加

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread internal/api/api.go
Comment thread internal/api/api.go Outdated
Comment thread cmd/logreport/main.go
Comment thread config.go
Comment thread internal/api/api.go
Comment thread internal/api/api.go
Comment thread internal/api/api.go
Comment thread cmd/logreport/main.go Outdated
Comment thread cmd/logreport/main.go
Comment thread README.md Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

logreport に Unix Domain Socket 経由の内部 API を追加し、最新のメトリクス値をリアルタイムに取得できるようにする PR です(/keys, /metrics)。

Changes:

  • internal/api に最新メトリクスを保持し提供する HTTP API を新規追加
  • 設定 (API.Enabled, API.SocketPath) を追加し、起動時に UDS で API サーバを立ち上げ
  • サンプル設定と README に内部 API の利用方法を追記

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
internal/api/api.go 最新メトリクスの保持と /keys /metrics 提供を追加
cmd/logreport/main.go API サーバ起動(UDS)と集計ループからの最新メトリクス反映を追加
config.go API 設定項目(Enabled/SocketPath)を追加
cmd/logreport/config.sample.yaml API 設定のサンプルを追加
README.md 内部 API の設定例と curl 例を追記
go.mod / go.sum golang.org/x/sync(singleflight)を追加

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/logreport/main.go
Comment thread internal/api/api.go Outdated
Comment thread internal/api/api.go Outdated
Comment thread internal/api/api.go Outdated
Comment thread internal/api/api.go Outdated
Comment thread cmd/logreport/main.go
Comment thread internal/api/api.go
Comment thread internal/api/api.go Outdated
Comment thread internal/api/api.go
Comment thread config.go

@masa23 masa23 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

@masa23 masa23 merged commit 69e958f into masa23:master Mar 17, 2026
1 check passed
@github-actions github-actions Bot mentioned this pull request Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants