# ==============================================================================
# ADTLAS DAI MANAGEMENT SYSTEM - GRAFANA DATASOURCES CONFIGURATION
# ==============================================================================
#
# This file configures Grafana datasources for the Adtlas DAI Management System,
# providing connections to Prometheus, Loki, and other monitoring data sources.
#
# Features:
# - Prometheus metrics datasource
# - Loki logs datasource (if available)
# - PostgreSQL datasource for business metrics
# - Redis datasource for cache metrics
# - Alertmanager datasource for alert management
#
# Author: Adtlas Development Team
# Version: 2.0.0
# Last Updated: 2025-01-20
# ==============================================================================

apiVersion: 1

# ==============================================================================
# DATASOURCES CONFIGURATION
# ==============================================================================

datasources:
  # ==============================================================================
  # PROMETHEUS DATASOURCE - PRIMARY METRICS SOURCE
  # ==============================================================================
  
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://prometheus:9090
    isDefault: true
    editable: true
    
    # Basic authentication (if required)
    # basicAuth: false
    # basicAuthUser: ''
    # basicAuthPassword: ''
    
    # HTTP settings
    httpMethod: POST
    
    # JSON data for additional configuration
    jsonData:
      # Query timeout
      timeInterval: '15s'
      queryTimeout: '60s'
      
      # HTTP method for queries
      httpMethod: POST
      
      # Custom headers
      customQueryParameters: ''
      
      # Exemplars configuration
      exemplarTraceIdDestinations:
        - name: 'Jaeger'
          datasourceUid: 'jaeger-uid'
          url: 'http://jaeger:16686/trace/${__value.raw}'
      
      # Prometheus type and version
      prometheusType: 'Prometheus'
      prometheusVersion: '2.40.0'
      
      # Cache level
      cacheLevel: 'High'
      
      # Disable metrics lookup
      disableMetricsLookup: false
      
      # Custom query parameters
    
    # Secure JSON data (for sensitive configuration)
    secureJsonData: {}
    
    # UID for referencing this datasource
    uid: 'prometheus-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # ALERTMANAGER DATASOURCE - ALERT MANAGEMENT
  # ==============================================================================
  
  - name: Alertmanager
    type: alertmanager
    access: proxy
    url: http://alertmanager:9093
    editable: true
    
    # JSON data for Alertmanager configuration
    jsonData:
      # Implementation type
      implementation: 'prometheus'
      
      # Handle Grafana managed alerts
      handleGrafanaManagedAlerts: false
      
      # Timeout settings
      timeout: 60
    
    # UID for referencing this datasource
    uid: 'alertmanager-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # LOKI DATASOURCE - LOG AGGREGATION (OPTIONAL)
  # ==============================================================================
  
  - name: Loki
    type: loki
    access: proxy
    url: http://loki:3100
    editable: true
    
    # JSON data for Loki configuration
    jsonData:
      # Maximum number of lines to display
      maxLines: 1000
      
      # Timeout for queries
      timeout: 60
      
      # Derived fields for linking to traces
      derivedFields:
        - datasourceUid: 'jaeger-uid'
          matcherRegex: '(?:trace_id|traceID)=(\w+)'
          name: 'TraceID'
          url: 'http://jaeger:16686/trace/${__value.raw}'
    
    # UID for referencing this datasource
    uid: 'loki-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # POSTGRESQL DATASOURCE - BUSINESS METRICS
  # ==============================================================================
  
  - name: PostgreSQL
    type: postgres
    access: proxy
    url: postgres:5432
    database: '${POSTGRES_DB}'
    user: '${POSTGRES_USER}'
    editable: true
    
    # JSON data for PostgreSQL configuration
    jsonData:
      # SSL mode
      sslmode: 'disable'
      
      # Maximum open connections
      maxOpenConns: 10
      
      # Maximum idle connections
      maxIdleConns: 2
      
      # Connection max lifetime
      connMaxLifetime: 14400
      
      # PostgreSQL version
      postgresVersion: 1300
      
      # Time column
      timescaledb: false
    
    # Secure JSON data for password
    secureJsonData:
      password: '${POSTGRES_PASSWORD}'
    
    # UID for referencing this datasource
    uid: 'postgresql-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # REDIS DATASOURCE - CACHE METRICS (USING PROMETHEUS EXPORTER)
  # ==============================================================================
  
  - name: Redis
    type: redis-datasource
    access: proxy
    url: redis://redis:6379
    editable: true
    
    # JSON data for Redis configuration
    jsonData:
      # Client type
      client: 'standalone'
      
      # Pool size
      poolSize: 5
      
      # Timeout
      timeout: 10
      
      # Ping interval
      pingInterval: 0
      
      # Pipeline window
      pipelineWindow: 0
    
    # Secure JSON data for password (if Redis has auth)
    secureJsonData:
      password: '${REDIS_PASSWORD}'
    
    # UID for referencing this datasource
    uid: 'redis-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # JAEGER DATASOURCE - DISTRIBUTED TRACING (OPTIONAL)
  # ==============================================================================
  
  - name: Jaeger
    type: jaeger
    access: proxy
    url: http://jaeger:16686
    editable: true
    
    # JSON data for Jaeger configuration
    jsonData:
      # Trace to logs configuration
      tracesToLogs:
        datasourceUid: 'loki-uid'
        tags: ['job', 'instance', 'pod', 'namespace']
        mappedTags: [{'key': 'service.name', 'value': 'service'}]
        mapTagNamesEnabled: false
        spanStartTimeShift: '1h'
        spanEndTimeShift: '1h'
        filterByTraceID: false
        filterBySpanID: false
      
      # Trace to metrics configuration
      tracesToMetrics:
        datasourceUid: 'prometheus-uid'
        tags: [{'key': 'service.name', 'value': 'service'}, {'key': 'job'}]
        queries:
          - name: 'Sample query'
            query: 'sum(rate(traces_spanmetrics_latency_bucket{$$__tags}[5m]))'
      
      # Node graph configuration
      nodeGraph:
        enabled: true
    
    # UID for referencing this datasource
    uid: 'jaeger-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # TESTDATA DATASOURCE - FOR TESTING AND DEVELOPMENT
  # ==============================================================================
  
  - name: TestData
    type: testdata
    access: proxy
    editable: true
    
    # UID for referencing this datasource
    uid: 'testdata-uid'
    
    # Version
    version: 1
  
  # ==============================================================================
  # MIXED DATASOURCE - FOR COMBINING MULTIPLE SOURCES
  # ==============================================================================
  
  - name: Mixed
    type: mixed
    access: proxy
    editable: false
    
    # UID for referencing this datasource
    uid: 'mixed-uid'
    
    # Version
    version: 1

# ==============================================================================
# DATASOURCE CONFIGURATION NOTES
# ==============================================================================
#
# This configuration provides:
# 1. Prometheus - Primary metrics collection and alerting
# 2. Alertmanager - Alert management and routing
# 3. Loki - Log aggregation and analysis (optional)
# 4. PostgreSQL - Business metrics and custom queries
# 5. Redis - Cache performance metrics
# 6. Jaeger - Distributed tracing (optional)
# 7. TestData - Development and testing
# 8. Mixed - Combining multiple datasources
#
# Environment Variables Used:
# - POSTGRES_DB: PostgreSQL database name
# - POSTGRES_USER: PostgreSQL username
# - POSTGRES_PASSWORD: PostgreSQL password
# - REDIS_PASSWORD: Redis password (if authentication enabled)
#
# Datasource Features:
# 1. Automatic provisioning on Grafana startup
# 2. Cross-datasource linking (traces to logs to metrics)
# 3. Optimized query settings for performance
# 4. Security configurations for sensitive data
# 5. Custom query parameters for advanced features
#
# Usage Guidelines:
# 1. Prometheus is set as the default datasource
# 2. Use UIDs to reference datasources in dashboards
# 3. Configure authentication based on security requirements
# 4. Adjust timeout and connection settings based on load
# 5. Enable/disable optional datasources based on deployment
# 6. Monitor datasource health in Grafana admin panel
#
# Security Considerations:
# 1. Passwords stored in secureJsonData
# 2. Use environment variables for sensitive configuration
# 3. Configure SSL/TLS for production deployments
# 4. Limit datasource access based on user roles
# 5. Regular rotation of credentials
# 6. Monitor datasource access logs
# ==============================================================================