@@ -7,32 +7,52 @@ use std::sync::Arc;
7
7
8
8
#[ derive( Clone , Debug , Default ) ]
9
9
pub struct TelemetryOptions {
10
- application_id : Option < String > ,
11
- }
12
-
13
- impl TelemetryOptions {
14
- pub fn new ( application_id : Option < String > ) -> Self {
15
- Self { application_id }
16
- }
10
+ pub application_id : Option < String > ,
17
11
}
18
12
19
13
#[ derive( Clone , Debug ) ]
20
14
pub struct TelemetryPolicy {
21
15
header : String ,
22
16
}
23
17
24
- impl TelemetryPolicy {
25
- pub fn new ( options : TelemetryOptions ) -> Self {
26
- Self :: with_environment :: < Env > ( options)
18
+ /// Sets the User-Agent header with useful information in a typical format for Azure SDKs.
19
+ ///
20
+ /// Client libraries should create a `TelemetryPolicy` using `option_env!()` like so:
21
+ /// ```
22
+ /// use azure_core::policies::{TelemetryOptions, TelemetryPolicy};
23
+ /// let policy = TelemetryPolicy::new(option_env!("CARGO_PKG_NAME"), option_env!("CARGO_PKG_VERSION"), &TelemetryOptions::default());
24
+ /// ```
25
+ impl < ' a > TelemetryPolicy {
26
+ pub fn new (
27
+ crate_name : Option < & ' a str > ,
28
+ crate_version : Option < & ' a str > ,
29
+ options : & TelemetryOptions ,
30
+ ) -> Self {
31
+ Self :: new_with_rustc_version (
32
+ crate_name,
33
+ crate_version,
34
+ option_env ! ( "AZSDK_RUSTC_VERSION" ) ,
35
+ options,
36
+ )
27
37
}
28
38
29
- fn with_environment < T : Environment > ( options : TelemetryOptions ) -> Self {
39
+ fn new_with_rustc_version (
40
+ crate_name : Option < & ' a str > ,
41
+ crate_version : Option < & ' a str > ,
42
+ rustc_version : Option < & ' static str > ,
43
+ options : & TelemetryOptions ,
44
+ ) -> Self {
30
45
const UNKNOWN : & ' static str = "unknown" ;
31
- let crate_name = T :: crate_name ( ) . unwrap_or ( UNKNOWN ) ;
32
- let crate_version = T :: crate_version ( ) . unwrap_or ( UNKNOWN ) ;
33
- let rustc_version = T :: rustc_version ( ) . unwrap_or ( UNKNOWN ) ;
46
+ let mut crate_name = crate_name. unwrap_or ( UNKNOWN ) ;
47
+ let crate_version = crate_version. unwrap_or ( UNKNOWN ) ;
48
+ let rustc_version = rustc_version. unwrap_or ( UNKNOWN ) ;
34
49
let platform_info = format ! ( "({}; {}; {})" , rustc_version, OS , ARCH , ) ;
35
- let header = match options. application_id {
50
+
51
+ if let Some ( name) = crate_name. strip_prefix ( "azure_" ) {
52
+ crate_name = name;
53
+ }
54
+
55
+ let header = match & options. application_id {
36
56
Some ( application_id) => format ! (
37
57
"{} azsdk-rust-{}/{} {}" ,
38
58
application_id, crate_name, crate_version, platform_info
@@ -47,29 +67,6 @@ impl TelemetryPolicy {
47
67
}
48
68
}
49
69
50
- impl Default for TelemetryPolicy {
51
- fn default ( ) -> Self {
52
- TelemetryPolicy :: new ( TelemetryOptions :: default ( ) )
53
- }
54
- }
55
-
56
- trait Environment {
57
- fn crate_name ( ) -> Option < & ' static str > {
58
- option_env ! ( "CARGO_PKG_NAME" )
59
- }
60
-
61
- fn crate_version ( ) -> Option < & ' static str > {
62
- option_env ! ( "CARGO_PKG_VERSION" )
63
- }
64
-
65
- fn rustc_version ( ) -> Option < & ' static str > {
66
- option_env ! ( "AZSDK_RUSTC_VERSION" )
67
- }
68
- }
69
-
70
- struct Env ;
71
- impl Environment for Env { }
72
-
73
70
#[ async_trait:: async_trait]
74
71
impl Policy for TelemetryPolicy {
75
72
async fn send (
@@ -90,54 +87,42 @@ impl Policy for TelemetryPolicy {
90
87
mod test {
91
88
use super :: * ;
92
89
93
- // tests assume cargo + rustc
94
- const CRATE_NAME : & ' static str = env ! ( "CARGO_PKG_NAME" ) ;
95
- const CRATE_VERSION : & ' static str = env ! ( "CARGO_PKG_VERSION" ) ;
96
- const RUSTC_VERSION : & ' static str = env ! ( "AZSDK_RUSTC_VERSION" ) ;
97
-
98
- struct EmptyEnv ;
99
- impl Environment for EmptyEnv {
100
- fn crate_name ( ) -> Option < & ' static str > {
101
- None
102
- }
103
-
104
- fn crate_version ( ) -> Option < & ' static str > {
105
- None
106
- }
107
-
108
- fn rustc_version ( ) -> Option < & ' static str > {
109
- None
110
- }
111
- }
112
-
113
90
#[ test]
114
- fn test_default ( ) {
115
- let policy = TelemetryPolicy :: default ( ) ;
91
+ fn test_without_application_id ( ) {
92
+ let policy = TelemetryPolicy :: new_with_rustc_version (
93
+ Some ( "azure_test" ) , // Tests that "azure_" is removed.
94
+ Some ( "1.2.3" ) ,
95
+ Some ( "4.5.6" ) ,
96
+ & TelemetryOptions :: default ( ) ,
97
+ ) ;
116
98
assert_eq ! (
117
99
policy. header,
118
- format!(
119
- "azsdk-rust-{}/{} ({}; {}; {})" ,
120
- CRATE_NAME , CRATE_VERSION , RUSTC_VERSION , OS , ARCH
121
- )
100
+ format!( "azsdk-rust-test/1.2.3 (4.5.6; {}; {})" , OS , ARCH )
122
101
) ;
123
102
}
124
103
125
104
#[ test]
126
105
fn test_with_application_id ( ) {
127
- let options = TelemetryOptions :: new ( Some ( "test" . to_string ( ) ) ) ;
128
- let policy = TelemetryPolicy :: new ( options) ;
106
+ let options = TelemetryOptions {
107
+ application_id : Some ( "my_app" . to_string ( ) ) ,
108
+ } ;
109
+ let policy = TelemetryPolicy :: new_with_rustc_version (
110
+ Some ( "test" ) ,
111
+ Some ( "1.2.3" ) ,
112
+ Some ( "4.5.6" ) ,
113
+ & options,
114
+ ) ;
129
115
assert_eq ! (
130
116
policy. header,
131
- format!(
132
- "test azsdk-rust-{}/{} ({}; {}; {})" ,
133
- CRATE_NAME , CRATE_VERSION , RUSTC_VERSION , OS , ARCH
134
- )
117
+ format!( "my_app azsdk-rust-test/1.2.3 (4.5.6; {}; {})" , OS , ARCH )
135
118
) ;
136
119
}
137
120
138
121
#[ test]
139
122
fn test_missing_env ( ) {
140
- let policy = TelemetryPolicy :: with_environment :: < EmptyEnv > ( TelemetryOptions :: default ( ) ) ;
123
+ // Would simulate if option_env!("CARGO_PKG_NAME"), for example, returned None.
124
+ let policy =
125
+ TelemetryPolicy :: new_with_rustc_version ( None , None , None , & TelemetryOptions :: default ( ) ) ;
141
126
assert_eq ! (
142
127
policy. header,
143
128
format!( "azsdk-rust-unknown/unknown (unknown; {}; {})" , OS , ARCH )
0 commit comments