logreport

package module
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: MIT Imports: 8 Imported by: 0

README

logreport

LTSV形式およびJSON形式(ネスト不可)のログを収集・集計し、その結果をGraphiteに送信するツールです。

概要

logreportは、ログデータを収集・集計し、その結果をGraphiteプロトコルを使用してGraphiteサーバーに送信するためのツールです。主に、LTSV形式とJSON形式(ネスト不可)のログに対応しています。

対応形式

  • LTSV形式
  • JSON形式(ネスト不可)

インストール

go install github.com/masa23/logreport/cmd/logreport@latest

または

git clone https://github.com/masa23/logreport.git
cd logreport
go build -o logreport ./cmd/logreport

使用方法

logreport -config config.yaml
設定ファイル例
# デバッグモード
Debug: false

# PIDファイルのパス
PidFile: "/var/run/logreport.pid"

# エラーログファイルのパス
ErrorLogFile: "/var/log/logreport.log"

# ログバッファサイズ
LogBufferSize: 4096

# 内部API
API:
  # 内部API 有効/無効
  Enabled: false
  # 内部APIを有効にする場合にListenするUnix Domain Socket
  # SocketPath: "/var/run/logreport-api.sock"

# 読み込むログファイルのパス
LogFile: "/var/log/nginx/access.log"

# 読み込み位置を記録するファイルのパス
PosFile: "/var/log/nginx/access.log.pos"

# タイムスタンプのカラム名
TimeColumn: "time_local"

# タイムスタンプのフォーマット
TimeParse: "02/Jan/2006:15:04:05 -0700"

# ログフォーマット (ltsv または json)
LogFormat: ltsv

# レポート設定
Report:
  # 集計間隔
  Interval: 1s
  # 遅延時間
  Delay: 10s

# 出力先設定
Exporters:
  # Graphiteへの出力設定
  Graphite:
    Host: localhost
    Port: 2003
    Prefix: "sec.logreport.local"
    SendBuffer: 1000
    MaxRetryCount: 5
    RetryWait: 1s
  
  # OTLP/gRPCへの出力設定(オプション)
  #OtlpGrpc:
  #  URL: addrs:///localhost:4317
  #  TLS:
  #    Insecure: true
  #  SendBuffer: 1000
  #  MaxRetryCount: 10
  #  RetryWait: 1s
  #  ResourceAttributes:
  #    service.name: example-service
  #    server.address: localhost

# 収集するメトリクスの定義
Metrics:
  # ステータスコードのカウント(HTTP)
  - Description: "status count"
    ItemName: "http.status"
    Type: "itemCount"
    LogColumn: "status"
    Filter:
      - LogColumn: "scheme"
        Values:
          - "http"
        Bool: true

  # ステータスコードのカウント(HTTPS)
  - Description: "status count"
    ItemName: "https.status"
    Type: "itemCount"
    LogColumn: "status"
    Filter:
      - LogColumn: "scheme"
        Values:
          - "https"
        Bool: true

  # HTTPリクエスト数
  - Description: "http request count"
    ItemName: "http.request"
    Type: "count"
    LogColumn: "scheme"
    Filter:
      - LogColumn: "scheme"
        Values:
          - "http"
        Bool: true

  # HTTPSリクエスト数
  - Description: "https request count"
    ItemName: "https.request"
    Type: "count"
    LogColumn: "scheme"
    Filter:
      - LogColumn: "scheme"
        Values:
          - "https"
        Bool: true

  # キャッシュヒット数
  - Description: "hit counter"
    ItemName: "hit-count"
    Type: "itemCount"
    LogColumn: "upstream_cache_status"

  # 送信バイト数の合計
  - Description: "bytes sent"
    ItemName: "bytes_sent"
    Type: "sum"
    DataType: "int"
    LogColumn: "bytes_sent"

  # 最大アップストリームリクエスト時間
  - Description: "max upstream request time"
    ItemName: "upstream_request_time_max"
    Type: "max"
    DataType: "float"
    LogColumn: "upstream_request_time"

  # 最小アップストリームリクエスト時間
  - Description: "min upstream request time"
    ItemName: "upstream_request_time_min"
    Type: "min"
    DataType: "float"
    LogColumn: "upstream_request_time"
内部API

設定ファイルで以下のように設定することで有効にすることができます。

API:
  Enabled: true
  SocketPath: "/var/run/logreport-api.sock"
最新のメトリックのキー一覧
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"]
最新のメトリックを取得

keysにキーを指定することで取得可能です。

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"}

ライセンス

MIT

Documentation

Index

Constants

View Source
const (
	MetricTypeCount     = "count"
	MetricTypeSum       = "sum"
	MetricTypeMax       = "max"
	MetricTypeMin       = "min"
	MetricTypeItemCount = "itemCount"
	DataTypeInt         = "int"
	DataTypeFloat       = "float"
	DataTypeString      = "string"
)

Metric type

Variables

This section is empty.

Functions

func ParseLog

func ParseLog(buf []byte, columns []logColumn, format string) (parsedLogs, error)

ParseLog format json, ltsv

Types

type Config

type Config struct {
	Debug         bool            `yaml:"Debug"`
	PidFile       string          `yaml:"PidFile"`
	ErrorLogFile  string          `yaml:"ErrorLogFile"`
	LogFile       string          `yaml:"LogFile"`
	PosFile       string          `yaml:"PosFile"`
	LogBufferSize int             `yaml:"LogBufferSize"`
	LogFormat     string          `yaml:"LogFormat"`
	Graphite      *configGraphite `yaml:"Graphite"`
	Report        configReport    `yaml:"Report"`
	Metrics       []configMetrics `yaml:"Metrics"`
	TimeColumn    string          `yaml:"TimeColumn"`
	TimeParse     string          `yaml:"TimeParse"`
	LogColumns    []logColumn
	Exporters     configExporters `yaml:"Exporters"`
	API           configAPI       `yaml:"API"`
}

Config is confiure struct

func ConfigLoad

func ConfigLoad(file string) (*Config, error)

ConfigLoad is loading yaml config

Directories

Path Synopsis
cmd
loggen command
logreport command
internal
api

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL