Skip to main content

client.go

Filename: client.go

  1. Overview

This file implements a custom Prometheus remote read client for ClickHouse. It handles Prometheus queries, translates them into ClickHouse queries, fetches data from ClickHouse, and converts the results back into Prometheus time series data. It also supports executing raw SQL queries against ClickHouse.

  1. Detailed Documentation

type client

type client struct {
settings factory.ScopedProviderSettings
telemetryStore telemetrystore.TelemetryStore
}
  • Purpose: Defines the structure for the ClickHouse Prometheus client. It holds the settings and telemetry store required for database interaction.
  • Parameters: None
  • Returns: None

func NewReadClient

func NewReadClient(settings factory.ScopedProviderSettings, telemetryStore telemetrystore.TelemetryStore) remote.ReadClient {
return &client{
settings: settings,
telemetryStore: telemetryStore,
}
}
  • Purpose: Creates a new instance of the ClickHouse Prometheus client.
  • Parameters:
    • settings (factory.ScopedProviderSettings): Settings for the client.
    • telemetryStore (telemetrystore.TelemetryStore): Telemetry store to interact with ClickHouse.
  • Returns: remote.ReadClient: A new ClickHouse Prometheus client.

func (*client) Read

func (client *client) Read(ctx context.Context, query *prompb.Query, sortSeries bool) (storage.SeriesSet, error) {
  • Purpose: Implements the Read method for the remote.ReadClient interface. It translates a Prometheus query into a ClickHouse query, retrieves the data, and formats it into a storage.SeriesSet. It also handles raw SQL queries passed through the Prometheus query mechanism.
  • Parameters:
    • ctx (context.Context): Context for the request.
    • query (*prompb.Query): Prometheus query to execute.
    • sortSeries (bool): Whether to sort the series.
  • Returns:
    • storage.SeriesSet: The result set as a Prometheus series set.
    • error: Any error encountered during the process.

func (*client) queryToClickhouseQuery

func (client *client) queryToClickhouseQuery(_ context.Context, query *prompb.Query, metricName string, subQuery bool) (string, []any, error) {
  • Purpose: Translates a Prometheus query into a ClickHouse SQL query. It constructs the WHERE clause based on the query's matchers and specifies the appropriate table.
  • Parameters:
    • _ (context.Context): Context (unused).
    • query (*prompb.Query): Prometheus query.
    • metricName (string): Name of the metric being queried.
    • subQuery (bool): Indicates whether this is a subquery to fetch fingerprints.
  • Returns:
    • string: ClickHouse query.
    • []any: Arguments for the ClickHouse query.
    • error: Any error during query construction.

func (*client) getFingerprintsFromClickhouseQuery

func (client *client) getFingerprintsFromClickhouseQuery(ctx context.Context, query string, args []any) (map[uint64][]prompb.Label, error) {
  • Purpose: Executes a ClickHouse query to retrieve fingerprints and their corresponding labels.
  • Parameters:
    • ctx (context.Context): Context for the request.
    • query (string): ClickHouse query to execute.
    • args ([]any): Arguments for the query.
  • Returns:
    • map[uint64][]prompb.Label: A map of fingerprints to labels.
    • error: Any error during query execution or data processing.

func (*client) querySamples

func (client *client) querySamples(ctx context.Context, start int64, end int64, fingerprints map[uint64][]prompb.Label, metricName string, subQuery string, args []any) ([]*prompb.TimeSeries, error) {
  • Purpose: Retrieves samples from ClickHouse for a given set of fingerprints and time range.
  • Parameters:
    • ctx (context.Context): Context for the request.
    • start (int64): Start timestamp in milliseconds.
    • end (int64): End timestamp in milliseconds.
    • fingerprints (map[uint64][]prompb.Label): Map of fingerprints to labels.
    • metricName (string): Name of the metric.
    • subQuery (string): Subquery to filter fingerprints.
    • args ([]any): Arguments for the ClickHouse query.
  • Returns:
    • []*prompb.TimeSeries: A slice of Prometheus time series.
    • error: Any error during query execution or data processing.

func (*client) queryRaw

func (client *client) queryRaw(ctx context.Context, query string, ts int64) (*prompb.QueryResult, error) {
  • Purpose: Executes a raw SQL query against ClickHouse and converts the results into a Prometheus QueryResult.
  • Parameters:
    • ctx (context.Context): Context for the request.
    • query (string): Raw SQL query to execute.
    • ts (int64): Timestamp to use for the samples.
  • Returns:
    • *prompb.QueryResult: The result of the query as a Prometheus QueryResult.
    • error: Any error during query execution or data processing.
  1. Code Examples

(No specific examples are needed, as the code is relatively self-explanatory.)

Include in Getting Started: NO