1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/// Supported tracing backends and their configuration.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
#[serde(tag = "backend", content = "options", deny_unknown_fields)]
pub enum Config {
    /// The `Noop` tracer (default).
    ///
    /// A tracer that discards all spans.
    /// Used when integration with distributed tracing is not needed.
    #[serde(rename = "noop")]
    Noop,

    /// [Zipkin] tracer backend.
    ///
    /// Spans are sent to [Zipkin] over the [Kafka] collector.
    ///
    /// [Kafka]: https://kafka.apache.org/
    /// [Zipkin]: https://zipkin.io/
    #[serde(rename = "zipkin")]
    Zipkin(ZipkinConfig),
}

impl Default for Config {
    fn default() -> Config {
        Config::Noop
    }
}


/// Zipkin specific configuration options.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
pub struct ZipkinConfig {
    /// Value for the [Zipkin] service name field.
    /// 
    /// [Zipkin]: https://zipkin.io/
    pub service_name: String,

    /// List of URLs to seed the [Kafka] client.
    ///
    /// [Kafka]: https://kafka.apache.org/
    pub kafka: Vec<String>,

    /// [Kafka] topic to publish spans to (defaults to `zipkin`).
    ///
    /// [Kafka]: https://kafka.apache.org/
    #[serde(default = "ZipkinConfig::default_topic")]
    pub topic: String,
}

impl ZipkinConfig {
    fn default_topic() -> String { String::from("zipkin") }
}


#[cfg(test)]
mod tests {
    mod noop {
        use serde_yaml;
        use super::super::Config;

        #[test]
        fn deserialise() {
            let text = "backend: noop";
            let config: Config = serde_yaml::from_str(text).unwrap();
            assert_eq!(config, Config::Noop);
        }

        #[test]
        fn serialise() {
            let config = Config::Noop;
            let text = serde_yaml::to_string(&config).unwrap();
            assert_eq!(text, "---\nbackend: noop");
        }
    }

    mod zipkin {
        use serde_yaml;
        use super::super::Config;
        use super::super::ZipkinConfig;

        #[test]
        fn deserialise() {
            let text = r#"backend: zipkin
options:
    service_name: abc
    kafka:
        - def
        - ghi
    topic: test"#;
            let config: Config = serde_yaml::from_str(text).unwrap();
            assert_eq!(config, Config::Zipkin(ZipkinConfig {
                service_name: String::from("abc"),
                kafka: vec![String::from("def"), String::from("ghi")],
                topic: String::from("test"),
            }));
        }

        #[test]
        fn deserialise_defaults() {
            let text = r#"backend: zipkin
options:
    service_name: abc
    kafka:
        - def
        - ghi"#;
            let config: Config = serde_yaml::from_str(text).unwrap();
            assert_eq!(config, Config::Zipkin(ZipkinConfig {
                service_name: String::from("abc"),
                kafka: vec![String::from("def"), String::from("ghi")],
                topic: String::from("zipkin"),
            }));
        }

        #[test]
        #[should_panic(expected = "missing field `kafka`")]
        fn deserialise_fails() {
            let text = r#"backend: zipkin
options:
    service_name: abc"#;
            let _config: Config = serde_yaml::from_str(text).unwrap();
        }

        #[test]
        fn serialise() {
            let config = Config::Zipkin(ZipkinConfig {
                service_name: String::from("abc"),
                kafka: vec![String::from("def"), String::from("ghi")],
                topic: String::from("test"),
            });
            let text = serde_yaml::to_string(&config).unwrap();
            assert_eq!(text, r#"---
backend: zipkin
options:
  service_name: abc
  kafka:
    - def
    - ghi
  topic: test"#);
        }
    }
}