servers/otlp/
trace.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod attributes;
16pub mod span;
17pub mod v0;
18pub mod v1;
19
20use api::v1::RowInsertRequests;
21pub use common_catalog::consts::{
22    PARENT_SPAN_ID_COLUMN, SPAN_ID_COLUMN, SPAN_NAME_COLUMN, TRACE_ID_COLUMN,
23};
24use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
25use pipeline::{GreptimePipelineParams, PipelineWay};
26use session::context::QueryContextRef;
27
28use crate::error::{NotSupportedSnafu, Result};
29use crate::query_handler::PipelineHandlerRef;
30
31// column names
32pub const SERVICE_NAME_COLUMN: &str = "service_name";
33pub const TIMESTAMP_COLUMN: &str = "timestamp";
34pub const DURATION_NANO_COLUMN: &str = "duration_nano";
35pub const SPAN_KIND_COLUMN: &str = "span_kind";
36pub const SPAN_STATUS_CODE: &str = "span_status_code";
37pub const SPAN_ATTRIBUTES_COLUMN: &str = "span_attributes";
38pub const SPAN_EVENTS_COLUMN: &str = "span_events";
39pub const SCOPE_NAME_COLUMN: &str = "scope_name";
40pub const SCOPE_VERSION_COLUMN: &str = "scope_version";
41pub const RESOURCE_ATTRIBUTES_COLUMN: &str = "resource_attributes";
42
43// const keys
44pub const KEY_SERVICE_NAME: &str = "service.name";
45pub const KEY_SPAN_KIND: &str = "span.kind";
46
47// jaeger const keys, not sure if they are general
48pub const KEY_OTEL_SCOPE_NAME: &str = "otel.scope.name";
49pub const KEY_OTEL_SCOPE_VERSION: &str = "otel.scope.version";
50pub const KEY_OTEL_STATUS_CODE: &str = "otel.status_code";
51
52/// The span kind prefix in the database.
53/// If the span kind is `server`, it will be stored as `SPAN_KIND_SERVER` in the database.
54pub const SPAN_KIND_PREFIX: &str = "SPAN_KIND_";
55
56// The span status code prefix in the database.
57pub const SPAN_STATUS_PREFIX: &str = "STATUS_CODE_";
58pub const SPAN_STATUS_UNSET: &str = "STATUS_CODE_UNSET";
59
60/// Convert SpanTraces to GreptimeDB row insert requests.
61/// Returns `InsertRequests` and total number of rows to ingest
62pub fn to_grpc_insert_requests(
63    request: ExportTraceServiceRequest,
64    pipeline: PipelineWay,
65    pipeline_params: GreptimePipelineParams,
66    table_name: String,
67    query_ctx: &QueryContextRef,
68    pipeline_handler: PipelineHandlerRef,
69) -> Result<(RowInsertRequests, usize)> {
70    match pipeline {
71        PipelineWay::OtlpTraceDirectV0 => v0::v0_to_grpc_insert_requests(
72            request,
73            pipeline,
74            pipeline_params,
75            table_name,
76            query_ctx,
77            pipeline_handler,
78        ),
79        PipelineWay::OtlpTraceDirectV1 => v1::v1_to_grpc_insert_requests(
80            request,
81            pipeline,
82            pipeline_params,
83            table_name,
84            query_ctx,
85            pipeline_handler,
86        ),
87        _ => NotSupportedSnafu {
88            feat: "Unsupported pipeline for trace",
89        }
90        .fail(),
91    }
92}