Skip to main content

frontend/service_config/
otlp.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 serde::{Deserialize, Serialize};
16
17const DEFAULT_TRACE_INGEST_CHUNK_SIZE: usize = 512;
18
19#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
20#[serde(default)]
21pub struct OtlpOptions {
22    pub enable: bool,
23    pub experimental_enable_exponential_histogram: bool,
24    /// Maximum spans per trace ingest chunk. Set to 0 to disable splitting.
25    pub trace_ingest_chunk_size: usize,
26    /// Whether to synthesize the `greptime_otel_resource_info` descriptor
27    /// table from the resource attributes of OTLP metrics, so metrics-only
28    /// services reach the semantic graph. Off by default: it creates and
29    /// writes a table the user did not send.
30    pub experimental_enable_resource_info: bool,
31}
32
33impl Default for OtlpOptions {
34    fn default() -> Self {
35        Self {
36            enable: true,
37            experimental_enable_exponential_histogram: false,
38            trace_ingest_chunk_size: DEFAULT_TRACE_INGEST_CHUNK_SIZE,
39            experimental_enable_resource_info: false,
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_otlp_options() {
50        let default = OtlpOptions::default();
51        assert!(default.enable);
52        assert!(!default.experimental_enable_exponential_histogram);
53        assert_eq!(default.trace_ingest_chunk_size, 512);
54        assert!(!default.experimental_enable_resource_info);
55
56        let options: OtlpOptions = toml::from_str("enable = false").unwrap();
57        assert!(!options.enable);
58        assert!(!options.experimental_enable_exponential_histogram);
59        assert_eq!(
60            options.trace_ingest_chunk_size,
61            DEFAULT_TRACE_INGEST_CHUNK_SIZE
62        );
63
64        let options: OtlpOptions = toml::from_str("trace_ingest_chunk_size = 0").unwrap();
65        assert!(options.enable);
66        assert!(!options.experimental_enable_exponential_histogram);
67        assert_eq!(options.trace_ingest_chunk_size, 0);
68
69        let options: OtlpOptions =
70            toml::from_str("experimental_enable_exponential_histogram = true").unwrap();
71        assert!(options.experimental_enable_exponential_histogram);
72
73        let serialized = toml::to_string(&options).unwrap();
74        assert!(serialized.contains("experimental_enable_exponential_histogram = true"));
75        assert_eq!(toml::from_str::<OtlpOptions>(&serialized).unwrap(), options);
76    }
77}