---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Datadog Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for sending shop floor metrics and logs from
  Connectware to Datadog (Example)

metadata:
  name: Datadog Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  datadogSite:
    description: Datadog site of your account, for example datadoghq.com or datadoghq.eu
    type: string
    default: datadoghq.com

  datadogApiKey:
    description: Datadog API key used for the metrics and logs intake
    type: string

  metricName:
    description: Name of the submitted gauge metric
    type: string
    default: machine.temperature

  serviceName:
    description: Value of the service tag on metrics and logs
    type: string
    default: shop-floor

  topicRoot:
    description: Root of the MQTT topic hierarchy
    type: string
    default: enterprise

resources:
  # Connection to the Datadog metrics intake
  datadogMetricsConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !sub 'api.${datadogSite}'
        port: 443
        headers:
          DD-API-KEY: !ref datadogApiKey

  # Connection to the Datadog logs intake (separate host)
  datadogLogsConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !sub 'http-intake.logs.${datadogSite}'
        port: 443
        headers:
          DD-API-KEY: !ref datadogApiKey

  # Submits one gauge metric point per machine reading
  metricsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref datadogMetricsConnection
      write:
        path: /api/v2/series

  metricsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/temperature'
          publish:
            endpoint: !ref metricsEndpoint
          rules:
            - transform:
                # Type 3 is a gauge, timestamps are in epoch seconds
                expression: !sub |
                  {
                    "body": {
                      "series": [
                        {
                          "metric": "${metricName}",
                          "type": 3,
                          "points": [
                            {
                              "timestamp": $floor($millis() / 1000),
                              "value": value
                            }
                          ],
                          "resources": [
                            { "name": $context.vars.cell, "type": "host" }
                          ],
                          "tags": [
                            "site:" & $context.vars.site,
                            "area:" & $context.vars.area,
                            "line:" & $context.vars.line,
                            "service:${serviceName}"
                          ]
                        }
                      ]
                    }
                  }

  # Ships one log event per machine event message
  logsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref datadogLogsConnection
      write:
        path: /api/v2/logs

  logsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/events'
          publish:
            endpoint: !ref logsEndpoint
          rules:
            - transform:
                # ddtags is a single comma-separated string
                expression: !sub |
                  {
                    "body": [
                      {
                        "message": message,
                        "status": status,
                        "ddsource": "connectware",
                        "service": "${serviceName}",
                        "hostname": $context.vars.cell,
                        "ddtags": "site:" & $context.vars.site
                          & ",area:" & $context.vars.area
                          & ",line:" & $context.vars.line
                      }
                    ]
                  }
