zipkin_types/annotation.rs
1// Copyright 2017 Palantir Technologies, Inc.
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
15//! Annotations.
16use std::time::SystemTime;
17
18/// Associates an event that explains latency with a timestamp.
19///
20/// Unlike log statements, annotations are often codes, e.g. "ws" for WireSend.
21///
22/// Zipkin v1 core annotations such as "cs" and "sr" have been replaced with
23/// `Span::kind`, which interprets timestamp and duration.
24#[derive(Debug, Clone)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
27pub struct Annotation {
28 #[cfg_attr(feature = "serde", serde(with = "crate::time_micros"))]
29 timestamp: SystemTime,
30 value: String,
31}
32
33impl Annotation {
34 /// Creates a new `Annotation`.
35 #[inline]
36 pub fn new(timestamp: SystemTime, value: &str) -> Annotation {
37 Annotation {
38 timestamp,
39 value: value.to_string(),
40 }
41 }
42
43 /// Creates a new `Annotation` at the current time.
44 #[inline]
45 pub fn now(value: &str) -> Annotation {
46 Annotation::new(SystemTime::now(), value)
47 }
48
49 /// Returns the time at which the annotated event occurred.
50 #[inline]
51 pub fn timestamp(&self) -> SystemTime {
52 self.timestamp
53 }
54
55 /// Returns the value of the annotation.
56 #[inline]
57 pub fn value(&self) -> &str {
58 &self.value
59 }
60}