##############################################################
# GMLM Platform — Loki Configuration
# Log aggregation and storage backend
##############################################################

auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096
  grpc_listen_address: 0.0.0.0

# ── Ingestion ────────────────────────────────────────────────
ingester:
  lifecycler:
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
  chunk_idle_period:    3m
  chunk_block_size:     262144   # 256KB
  chunk_retain_period:  1m
  max_transfer_retries: 0

# ── Schema and storage ───────────────────────────────────────
schema_config:
  configs:
    - from: 2024-01-01
      store:    boltdb-shipper
      object_store: filesystem
      schema:   v12
      index:
        prefix: index_
        period: 24h

storage_config:
  boltdb_shipper:
    active_index_directory: /loki/boltdb-shipper-active
    cache_location:         /loki/boltdb-shipper-cache
    cache_ttl:              24h
    shared_store:           filesystem
  filesystem:
    directory: /loki/chunks

# ── Compactor (merges and deduplicate old index) ─────────────
compactor:
  working_directory:  /loki/boltdb-shipper-compactor
  shared_store:       filesystem
  compaction_interval: 10m

# ── Query range (how to break up range queries) ──────────────
query_range:
  results_cache:
    cache:
      embedded_cache:
        enabled:   true
        max_size_mb: 100

# ── Limits — prevent single tenant overloading ───────────────
limits_config:
  retention_period:              744h    # 31 days
  reject_old_samples:            true
  reject_old_samples_max_age:    168h    # 7 days
  ingestion_rate_mb:             16
  ingestion_burst_size_mb:       32
  max_query_series:              5000
  max_entries_limit_per_query:   5000

# ── Ruler (alert rules from logs — e.g. error rate spikes) ──
ruler:
  storage:
    type: local
    local:
      directory: /loki/rules
  rule_path: /loki/rules
  ring:
    kvstore:
      store: inmemory
  enable_api: true

---
##############################################################
# Grafana Datasources Provisioning
# Auto-connects Grafana to Prometheus and Loki on startup
##############################################################
apiVersion: 1

datasources:
  - name:      Prometheus
    type:      prometheus
    access:    proxy
    url:       http://prometheus:9090
    isDefault: true
    jsonData:
      timeInterval:      15s
      queryTimeout:      30s
      httpMethod:        POST
      exemplarTraceIdDestinations:
        - name: traceID
          datasourceUid: tempo

  - name:   Loki
    type:   loki
    access: proxy
    url:    http://loki:3100
    jsonData:
      maxLines:   1000
      derivedFields:
        - datasourceUid: prometheus
          matcherRegex:  '"request_id":"([^"]+)"'
          name:          RequestId
          url:           '$${__value.raw}'
