Skip to main content

servers/otlp/
utils.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
15use api::v1::ColumnDataType;
16use api::v1::value::ValueData;
17use common_telemetry::warn;
18use jsonb::{Number as JsonbNumber, Value as JsonbValue};
19use opentelemetry_proto::tonic::common::v1::{KeyValue, any_value};
20
21pub fn bytes_to_hex_string(bs: &[u8]) -> String {
22    hex::encode(bs)
23}
24
25pub fn any_value_to_jsonb(value: any_value::Value) -> JsonbValue<'static> {
26    match value {
27        any_value::Value::StringValue(s) => JsonbValue::String(s.into()),
28        any_value::Value::IntValue(i) => JsonbValue::Number(JsonbNumber::Int64(i)),
29        any_value::Value::DoubleValue(d) => JsonbValue::Number(JsonbNumber::Float64(d)),
30        any_value::Value::BoolValue(b) => JsonbValue::Bool(b),
31        any_value::Value::ArrayValue(a) => {
32            let values = a
33                .values
34                .into_iter()
35                .map(|v| match v.value {
36                    Some(value) => any_value_to_jsonb(value),
37                    None => JsonbValue::Null,
38                })
39                .collect();
40            JsonbValue::Array(values)
41        }
42        any_value::Value::KvlistValue(kv) => key_value_to_jsonb(kv.values),
43        any_value::Value::BytesValue(b) => JsonbValue::String(bytes_to_hex_string(&b).into()),
44        // `StringValueStrindex` references the Profiling signal's
45        // `ProfilesDictionary.string_table`, which is unavailable to logs/traces.
46        // Per the OTLP spec, non-Profiling receivers must treat it as a non-fatal
47        // issue and process the value as if it were absent.
48        any_value::Value::StringValueStrindex(_) => {
49            warn!(
50                "encountered a profiling-only `StringValueStrindex` value in a non-profiling signal; ignoring"
51            );
52            JsonbValue::Null
53        }
54    }
55}
56
57pub fn key_value_to_jsonb(key_values: Vec<KeyValue>) -> JsonbValue<'static> {
58    JsonbValue::Object(
59        key_values
60            .into_iter()
61            .map(|kv| {
62                (
63                    kv.key,
64                    kv.value
65                        .and_then(|v| v.value)
66                        .map_or(JsonbValue::Null, any_value_to_jsonb),
67                )
68            })
69            .collect(),
70    )
71}
72
73#[inline]
74pub(crate) fn make_string_column_data(
75    name: &str,
76    value: Option<String>,
77) -> (String, ColumnDataType, Option<ValueData>) {
78    make_column_data(
79        name,
80        ColumnDataType::String,
81        value.map(ValueData::StringValue),
82    )
83}
84
85#[inline]
86pub(crate) fn make_column_data(
87    name: &str,
88    data_type: ColumnDataType,
89    value: Option<ValueData>,
90) -> (String, ColumnDataType, Option<ValueData>) {
91    (name.to_string(), data_type, value)
92}