Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(enterprise): configurable app name #18554

Merged
merged 15 commits into from
Sep 22, 2023
5 changes: 3 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ where
let proxy_connector = build_proxy_connector(tls_settings.into(), proxy_config)?;
let client = client_builder.build(proxy_connector.clone());

let app_name = crate::get_app_name();
let version = crate::get_version();
let user_agent = HeaderValue::from_str(&format!("Vector/{}", version))
.expect("Invalid header value for version!");
let user_agent = HeaderValue::from_str(&format!("{}/{}", app_name, version))
.expect("Invalid header value for user-agent!");

Ok(HttpClient {
client,
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ pub use source_sender::SourceSender;
pub use vector_common::{shutdown, Error, Result};
pub use vector_core::{event, metrics, schema, tcp, tls};

/// The name used to identify this Vector application.
///
/// This can be set at compile-time through the VECTOR_APP_NAME env variable.
/// Defaults to "Vector".
pub fn get_app_name() -> &'static str {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I can see this coming in handy in other places too in the future.

option_env!("VECTOR_APP_NAME").unwrap_or("Vector")
}

/// The current version of Vector in simplified format.
/// `<version-number>-nightly`.
pub fn vector_version() -> impl std::fmt::Display {
Expand Down
29 changes: 22 additions & 7 deletions src/sinks/datadog/logs/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub struct LogApiService {
client: HttpClient,
uri: Uri,
user_provided_headers: IndexMap<HeaderName, HeaderValue>,
default_headers: IndexMap<HeaderName, HeaderValue>,
}

impl LogApiService {
Expand All @@ -103,12 +104,25 @@ impl LogApiService {
uri: Uri,
headers: IndexMap<String, String>,
) -> crate::Result<Self> {
let headers = validate_headers(&headers)?;
let user_provided_headers = validate_headers(&headers)?;

let default_headers = &[
(CONTENT_TYPE.to_string(), "application/json".to_string()),
(
"DD-EVP-ORIGIN".to_string(),
crate::get_app_name().to_lowercase(),
),
("DD-EVP-ORIGIN-VERSION".to_string(), crate::get_version()),
]
.into_iter()
.collect();
let default_headers = validate_headers(&default_headers)?;

Ok(Self {
client,
uri,
user_provided_headers: headers,
user_provided_headers,
default_headers,
})
}
}
Expand All @@ -126,11 +140,8 @@ impl Service<LogApiRequest> for LogApiService {
// Emission of Error internal event is handled upstream by the caller
fn call(&mut self, mut request: LogApiRequest) -> Self::Future {
let mut client = self.client.clone();
let http_request = Request::post(&self.uri)
.header(CONTENT_TYPE, "application/json")
.header("DD-EVP-ORIGIN", "vector")
.header("DD-EVP-ORIGIN-VERSION", crate::get_version())
.header("DD-API-KEY", request.api_key.to_string());
let http_request =
Request::post(&self.uri).header("DD-API-KEY", request.api_key.to_string());

let http_request = if let Some(ce) = request.compression.content_encoding() {
http_request.header(CONTENT_ENCODING, ce)
Expand All @@ -145,6 +156,10 @@ impl Service<LogApiRequest> for LogApiService {
let mut http_request = http_request.header(CONTENT_LENGTH, request.body.len());

if let Some(headers) = http_request.headers_mut() {
for (name, value) in &self.default_headers {
dsmith3197 marked this conversation as resolved.
Show resolved Hide resolved
headers.insert(name, value.clone());
}

for (name, value) in &self.user_provided_headers {
// Replace rather than append to any existing header values
headers.insert(name, value.clone());
Expand Down