# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This will parse a textual representation of a duration. The formats
# accepted are based on the ISO-8601 duration format {@code PnDTnHnMn.nS}
# with days considered to be exactly 24 hours.
# <p>
# Examples:
# <pre>
#    "PT20.345S" -- parses as "20.345 seconds"
#    "PT15M"     -- parses as "15 minutes" (where a minute is 60 seconds)
#    "PT10H"     -- parses as "10 hours" (where an hour is 3600 seconds)
#    "P2D"       -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
#    "P2DT3H4M"  -- parses as "2 days, 3 hours and 4 minutes"
#    "P-6H3M"    -- parses as "-6 hours and +3 minutes"
#    "-P6H3M"    -- parses as "-6 hours and -3 minutes"
#    "-P-6H+3M"  -- parses as "+6 hours and -3 minutes"
# </pre>
filter: "{ tags -> tags.job_name == 'clickhouse-monitoring' }" # The OpenTelemetry job name
expSuffix:  tag({tags -> tags.host_name = 'clickhouse::' + tags.host_name}).service(['host_name'] , Layer.CLICKHOUSE)
metricPrefix: meter_clickhouse
metricsRules:
  # Number of files opened per minute.
  - name: file_open
    exp: ClickHouseProfileEvents_FileOpen.sum(['host_name','service_instance_id']).increase('PT1M')
  # Network
  # Number of connections to TCP server.
  - name: tcp_connections
    exp: ClickHouseMetrics_TCPConnection.sum(['host_name','service_instance_id'])
  # Number of client connections using MySQL protocol.
  - name: mysql_connections
    exp: ClickHouseMetrics_MySQLConnection.sum(['host_name','service_instance_id'])
  # Number of connections to HTTP server.
  - name: http_connections
    exp: ClickHouseMetrics_HTTPConnection.sum(['host_name','service_instance_id'])
  # Number of connections from other replicas to fetch parts.
  - name: interserver_connections
    exp: ClickHouseMetrics_InterserverConnection.sum(['host_name','service_instance_id'])
  # Number of client connections using PostgreSQL protocol.
  - name: postgresql_connections
    exp: ClickHouseMetrics_PostgreSQLConnection.sum(['host_name','service_instance_id'])
  # Total number of bytes received from network.
  - name: network_receive_bytes
    exp: ClickHouseProfileEvents_NetworkReceiveBytes.sum(['host_name','service_instance_id']).increase('PT1M')
  # Total number of bytes send to network.
  - name: network_send_bytes
    exp: ClickHouseProfileEvents_NetworkSendBytes.sum(['host_name','service_instance_id']).increase('PT1M')
  # Query
  # Number of executing queries.
  - name: query
    exp: ClickHouseProfileEvents_Query.sum(['host_name','service_instance_id']).increase('PT1M')
  # Number of executing queries, but only for SELECT queries.
  - name: query_select
    exp: ClickHouseProfileEvents_SelectQuery.sum(['host_name','service_instance_id']).increase('PT1M')
  # Number of executing queries, but only for INSERT queries.
  - name: query_insert
    exp: ClickHouseProfileEvents_InsertQuery.sum(['host_name','service_instance_id']).increase('PT1M')
  # Number of SELECT queries per second.
  - name: query_select_rate
    exp: ClickHouseProfileEvents_SelectQuery.sum(['host_name','service_instance_id']).rate('PT1M')
  # Number of INSERT queries per second.
  - name: query_insert_rate
    exp: ClickHouseProfileEvents_InsertQuery.sum(['host_name','service_instance_id']).rate('PT1M')
  # Total time of all queries
  - name: querytime_microseconds
    exp: ClickHouseProfileEvents_QueryTimeMicroseconds.sum(['host_name','service_instance_id']).increase('PT1M')
  # Total time of SELECT queries.
  - name: querytime_select_microseconds
    exp: ClickHouseProfileEvents_SelectQueryTimeMicroseconds.sum(['host_name','service_instance_id']).increase('PT1M')
  # Total time of INSERT queries.
  - name: querytime_insert_microseconds
    exp: ClickHouseProfileEvents_InsertQueryTimeMicroseconds.sum(['host_name','service_instance_id']).increase('PT1M')
  # Total time of queries that are not SELECT or INSERT.
  - name: querytime_other_microseconds
    exp: ClickHouseProfileEvents_OtherQueryTimeMicroseconds.sum(['host_name','service_instance_id']).increase('PT1M')
  # Number of reads from a file that were slow.
  - name: query_slow
    exp: ClickHouseProfileEvents_SlowRead.sum(['host_name','service_instance_id']).rate('PT1M')
  # Insertion
  # Number of rows INSERTed to all tables.
  - name: inserted_rows
    exp: ClickHouseProfileEvents_InsertedRows.sum(['host_name','service_instance_id']).rate('PT1M')
  # Number of bytes INSERTed to all tables.
  - name: inserted_bytes
    exp: ClickHouseProfileEvents_InsertedBytes.sum(['host_name','service_instance_id']).rate('PT1M')
  # Number of times the INSERT of a block to a MergeTree table was throttled due to high number of active data parts for partition.
  - name: delayed_inserts
    exp: ClickHouseProfileEvents_DelayedInserts.sum(['host_name','service_instance_id']).rate('PT1M')
  # Replicas
  # Number of data parts checking for consistency.
  - name: replicated_checks
    exp: ClickHouseMetrics_ReplicatedChecks.sum(['host_name','service_instance_id'])
  # Number of data parts being fetched from replica.
  - name: replicated_fetch
    exp: ClickHouseMetrics_ReplicatedFetch.sum(['host_name','service_instance_id'])
  # Number of data parts being sent to replicas.
  - name: replicated_send
    exp: ClickHouseMetrics_ReplicatedSend.sum(['host_name','service_instance_id'])
  # MergeTree
  # Number of executing background merges.
  - name: background_merge
    exp: ClickHouseMetrics_Merge.sum(['host_name','service_instance_id'])
  # Rows read for background merges. This is the number of rows before merge.
  - name: merge_rows
    exp: ClickHouseProfileEvents_MergedRows.sum(['host_name','service_instance_id']).increase('PT1M')
  # Uncompressed bytes (for columns as they stored in memory) that was read for background merges. This is the number before merge.
  - name: merge_uncompressed_bytes
    exp: ClickHouseProfileEvents_MergedUncompressedBytes.sum(['host_name','service_instance_id']).increase('PT1M')
  # Number of currently executing moves.
  - name: move
    exp: ClickHouseMetrics_Move.sum(['host_name','service_instance_id'])
  # Active data part, used by current and upcoming SELECTs.
  - name: parts_active
    exp: ClickHouseMetrics_PartsActive.sum(['host_name','service_instance_id'])
  # Number of mutations (ALTER DELETE/UPDATE)
  - name: mutations
    exp: ClickHouseMetrics_PartMutation.sum(['host_name','service_instance_id'])
  # Kafka Table Engine
  # Number of Kafka messages already processed by ClickHouse.
  - name: kafka_messages_read
    exp: ClickHouseProfileEvents_KafkaMessagesRead.sum(['host_name','service_instance_id']).rate('PT1M')
  # Number of writes (inserts) to Kafka tables.
  - name: kafka_writes
    exp: ClickHouseProfileEvents_KafkaWrites.sum(['host_name','service_instance_id']).rate('PT1M')
  # Number of active Kafka consumers.
  - name: kafka_consumers
    exp: ClickHouseMetrics_KafkaConsumers.sum(['host_name','service_instance_id'])
  # Number of active Kafka producer created.
  - name: kafka_producers
    exp: ClickHouseMetrics_KafkaProducers.sum(['host_name','service_instance_id'])
  # Zookeeper
  # Number of sessions (connections) to ZooKeeper. Should be no more than one, because using more than one connection to ZooKeeper may lead to bugs due to lack of linearizability (stale reads) that ZooKeeper consistency model allows
  - name: zookeeper_session
    exp: ClickHouseMetrics_ZooKeeperSession.sum(['host_name','service_instance_id'])
  # Number of watches (event subscriptions) in ZooKeeper.
  - name: zookeeper_watch
    exp: ClickHouseMetrics_ZooKeeperWatch.sum(['host_name','service_instance_id'])
  # Number of bytes send over network while communicating with ZooKeeper.
  - name: zookeeper_bytes_sent
    exp: ClickHouseProfileEvents_ZooKeeperBytesSent.sum(['host_name','service_instance_id']).rate('PT1M')
  #  Number of bytes received over network while communicating with ZooKeeper.
  - name: zookeeper_bytes_received
    exp: ClickHouseProfileEvents_ZooKeeperBytesReceived.sum(['host_name','service_instance_id']).rate('PT1M')
  # ClickHouse Keeper
  # Number of alive connections for embedded ClickHouse Keeper.
  - name: keeper_connections_alive
    exp: ClickHouseMetrics_KeeperAliveConnections.sum(['host_name','service_instance_id'])
  # Number of outstanding requests for embedded ClickHouse Keeper.
  - name: keeper_outstanding_requests
    exp: ClickHouseMetrics_KeeperOutstandingRequets.sum(['host_name','service_instance_id'])
