@@ -7,32 +7,52 @@ use std::sync::Arc;
77
88#[ derive( Clone , Debug , Default ) ]
99pub 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 > ,
1711}
1812
1913#[ derive( Clone , Debug ) ]
2014pub struct TelemetryPolicy {
2115 header : String ,
2216}
2317
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+ )
2737 }
2838
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 {
3045 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 ) ;
3449 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 {
3656 Some ( application_id) => format ! (
3757 "{} azsdk-rust-{}/{} {}" ,
3858 application_id, crate_name, crate_version, platform_info
@@ -47,29 +67,6 @@ impl TelemetryPolicy {
4767 }
4868}
4969
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-
7370#[ async_trait:: async_trait]
7471impl Policy for TelemetryPolicy {
7572 async fn send (
@@ -90,54 +87,42 @@ impl Policy for TelemetryPolicy {
9087mod test {
9188 use super :: * ;
9289
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-
11390 #[ 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+ ) ;
11698 assert_eq ! (
11799 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 )
122101 ) ;
123102 }
124103
125104 #[ test]
126105 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+ ) ;
129115 assert_eq ! (
130116 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 )
135118 ) ;
136119 }
137120
138121 #[ test]
139122 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 ( ) ) ;
141126 assert_eq ! (
142127 policy. header,
143128 format!( "azsdk-rust-unknown/unknown (unknown; {}; {})" , OS , ARCH )
0 commit comments