Project: ClickHouse (App Definition and Development / Database)
File: opentelemetry.md

slug: /en/operations/opentelemetry sidebar_position: 62 sidebar_label: Tracing ClickHouse with OpenTelemetry title: "[experimental] Tracing ClickHouse with OpenTelemetry" is an open standard for collecting traces and metrics from the distributed application. ClickHouse has some support for OpenTelemetry. :::note This is an experimental feature that will change in backwards-incompatible ways in future releases. ::: ClickHouse accepts trace context HTTP headers, as described by the . It also accepts trace context over a native protocol that is used for communication between ClickHouse servers or between the client and server. For manual testing, trace context headers conforming to the Trace Context recommendation can be supplied to `clickhouse-client` using `--opentelemetry-traceparent` and `--opentelemetry-tracestate` flags. If no parent trace context is supplied or the provided trace context does not comply with W3C standard above, ClickHouse can start a new trace, with probability controlled by the setting. The trace context is propagated to downstream services in the following cases: Queries to remote ClickHouse servers, such as when using table engine. table function. Trace context information is sent in HTTP headers. ClickHouse creates `trace spans` for each query and some of the query execution stages, such as query planning or distributed queries. To be useful, the tracing information has to be exported to a monitoring system that supports OpenTelemetry, such as or . ClickHouse avoids a dependency on a particular monitoring system, instead only providing the tracing data through a system table. OpenTelemetry trace span information is stored in the table. The table must be enabled in the server configuration, see the `opentelemetryspanlog` element in the default config file `config.xml`. It is enabled by default. The tags or attributes are saved as two parallel arrays, containing the keys and values. Use to work with them. At the moment, there is no ready tool that can export the tracing data from ClickHouse to a monitoring system. For testing, it is possible to setup the export using a materialized view with the engine over the table, which would push the arriving log data to an HTTP endpoint of a trace collector. For example, to push the minimal span data to a Zipkin instance running at `http://localhost:9411`, in Zipkin v2 JSON format: ```sql CREATE MATERIALIZED VIEW default.zipkin_spans ENGINE = URL('http://127.0.0.1:9411/api/v2/spans', 'JSONEachRow') SETTINGS outputformatjsonnamedtuplesasobjects = 1, outputformatjsonarrayof_rows = 1 AS SELECT lower(hex(trace_id)) AS traceId, case when parentspanid = 0 then '' else lower(hex(parentspanid)) end AS parentId, lower(hex(span_id)) AS id, operation_name AS name, starttimeus AS timestamp, finishtimeus - starttimeus AS duration, cast(tuple('clickhouse'), 'Tuple(serviceName text)') AS localEndpoint, cast(tuple( attribute.values[indexOf(attribute.names, 'db.statement')]), 'Tuple("db.statement" text)') AS tags FROM system.opentelemetryspanlog ``` In case of any errors, the part of the log data for which the error has occurred will be silently lost. Check the server log for error messages if the data does not arrive. Blog: