Skip to content

Commit

Permalink
fix: use app name as appname header (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Jul 19, 2024
1 parent 5281287 commit c1a3acd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
8 changes: 5 additions & 3 deletions server/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async fn get_data_source(args: &EdgeArgs) -> Option<Arc<dyn EdgePersistence>> {
None
}

async fn build_edge(args: &EdgeArgs) -> EdgeResult<EdgeInfo> {
async fn build_edge(args: &EdgeArgs, app_name: &str) -> EdgeResult<EdgeInfo> {
if !args.strict {
if !args.dynamic {
error!("You should explicitly opt into either strict or dynamic behavior. Edge has defaulted to dynamic to preserve legacy behavior, however we recommend using strict from now on. Not explicitly opting into a behavior will return an error on startup in a future release");
Expand Down Expand Up @@ -190,6 +190,7 @@ async fn build_edge(args: &EdgeArgs) -> EdgeResult<EdgeInfo> {
Duration::seconds(args.upstream_request_timeout),
Duration::seconds(args.upstream_socket_timeout),
args.token_header.token_header.clone(),
app_name.into(),
)
})
.map(|c| c.with_custom_client_headers(args.custom_client_headers.clone()))
Expand All @@ -209,6 +210,7 @@ async fn build_edge(args: &EdgeArgs) -> EdgeResult<EdgeInfo> {
Duration::seconds(args.features_refresh_interval_seconds.try_into().unwrap()),
persistence.clone(),
args.strict,
app_name,
));
let _ = token_validator.register_tokens(args.tokens.clone()).await;

Expand Down Expand Up @@ -245,7 +247,7 @@ pub async fn build_caches_and_refreshers(args: CliArgs) -> EdgeResult<EdgeInfo>
EdgeMode::Offline(offline_args) => {
build_offline(offline_args).map(|cache| (cache, None, None, None))
}
EdgeMode::Edge(edge_args) => build_edge(&edge_args).await,
EdgeMode::Edge(edge_args) => build_edge(&edge_args, &args.app_name).await,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -296,7 +298,7 @@ mod tests {
token_revalidation_interval_seconds: Default::default(),
};

let result = build_edge(&args).await;
let result = build_edge(&args, &"test-app").await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
Expand Down
2 changes: 1 addition & 1 deletion server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub struct CliArgs {
pub instance_id: String,

/// App name. Used for metrics reporting.
#[clap(short, long, env, default_value = "unleash-edge")]
#[clap(short, long, env, global = true, default_value = "unleash-edge")]
pub app_name: String,

#[arg(long, hide = true, global = true)]
Expand Down
1 change: 1 addition & 0 deletions server/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ mod tests {
refresh_interval: Duration::seconds(6000),
persistence: None,
strict: false,
app_name: "test-app".into(),
});
let token_validator = Arc::new(TokenValidator {
unleash_client: unleash_client.clone(),
Expand Down
23 changes: 20 additions & 3 deletions server/src/http/feature_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct FeatureRefresher {
pub refresh_interval: chrono::Duration,
pub persistence: Option<Arc<dyn EdgePersistence>>,
pub strict: bool,
pub app_name: String,
}

impl Default for FeatureRefresher {
Expand All @@ -113,13 +114,18 @@ impl Default for FeatureRefresher {
engine_cache: Default::default(),
persistence: None,
strict: true,
app_name: "unleash_edge".into(),
}
}
}

fn client_application_from_token(token: EdgeToken, refresh_interval: i64) -> ClientApplication {
fn client_application_from_token_and_name(
token: EdgeToken,
refresh_interval: i64,
app_name: &str,
) -> ClientApplication {
ClientApplication {
app_name: "unleash_edge".into(),
app_name: app_name.into(),
connect_via: None,
environment: token.environment,
instance_id: None,
Expand All @@ -143,6 +149,7 @@ impl FeatureRefresher {
features_refresh_interval: chrono::Duration,
persistence: Option<Arc<dyn EdgePersistence>>,
strict: bool,
app_name: &str,
) -> Self {
FeatureRefresher {
unleash_client,
Expand All @@ -152,6 +159,7 @@ impl FeatureRefresher {
refresh_interval: features_refresh_interval,
persistence,
strict,
app_name: app_name.into(),
}
}

Expand Down Expand Up @@ -269,9 +277,10 @@ impl FeatureRefresher {
self.unleash_client
.register_as_client(
token.token.clone(),
client_application_from_token(
client_application_from_token_and_name(
token.clone(),
self.refresh_interval.num_seconds(),
&self.app_name,
),
)
.await
Expand Down Expand Up @@ -484,6 +493,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -516,6 +526,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -552,6 +563,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -595,6 +607,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -647,6 +660,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -703,6 +717,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -744,6 +759,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down Expand Up @@ -780,6 +796,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let features_cache = Arc::new(DashMap::default());
let engine_cache = Arc::new(DashMap::default());
Expand Down
11 changes: 10 additions & 1 deletion server/src/http/unleash_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fn new_reqwest_client(
upstream_certificate_file: Option<PathBuf>,
connect_timeout: Duration,
socket_timeout: Duration,
app_name: String,
) -> EdgeResult<Client> {
build_identity(client_identity)
.and_then(|builder| {
Expand All @@ -148,7 +149,8 @@ fn new_reqwest_client(
let mut header_map = HeaderMap::new();
header_map.insert(
UNLEASH_APPNAME_HEADER,
header::HeaderValue::from_static("unleash-edge"),
header::HeaderValue::from_str(app_name.as_str())
.expect("Could not add app name as a header"),
);
header_map.insert(
UNLEASH_INSTANCE_ID_HEADER,
Expand Down Expand Up @@ -184,6 +186,7 @@ impl UnleashClient {
connect_timeout: Duration,
socket_timeout: Duration,
token_header: String,
app_name: String,
) -> Self {
Self {
urls: UnleashUrls::from_base_url(server_url),
Expand All @@ -194,6 +197,7 @@ impl UnleashClient {
upstream_certificate_file,
connect_timeout,
socket_timeout,
app_name,
)
.unwrap(),
custom_headers: Default::default(),
Expand All @@ -215,6 +219,7 @@ impl UnleashClient {
None,
Duration::seconds(5),
Duration::seconds(5),
"test-client".into(),
)
.unwrap(),
custom_headers: Default::default(),
Expand All @@ -235,6 +240,7 @@ impl UnleashClient {
None,
Duration::seconds(5),
Duration::seconds(5),
"test-client".into(),
)
.unwrap(),
custom_headers: Default::default(),
Expand Down Expand Up @@ -806,6 +812,7 @@ mod tests {
None,
Duration::seconds(5),
Duration::seconds(5),
"test-client".into(),
);
assert!(client.is_ok());
}
Expand All @@ -827,6 +834,7 @@ mod tests {
None,
Duration::seconds(5),
Duration::seconds(5),
"test-client".into(),
);
assert!(client.is_err());
}
Expand All @@ -848,6 +856,7 @@ mod tests {
None,
Duration::seconds(5),
Duration::seconds(5),
"test-client".into(),
);
assert!(client.is_ok());
}
Expand Down
1 change: 1 addition & 0 deletions server/src/middleware/client_token_from_frontend_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ mod tests {
Duration::seconds(5),
Duration::seconds(5),
"Authorization".to_string(),
"test-client".into(),
);
let local_features_cache: Arc<DashMap<String, ClientFeatures>> =
Arc::new(DashMap::default());
Expand Down

0 comments on commit c1a3acd

Please sign in to comment.