Skip to content

Commit 47a850d

Browse files
authored
Add env for OTLP service name (huggingface#285)
1 parent ce7d5b3 commit 47a850d

File tree

11 files changed

+60
-19
lines changed

11 files changed

+60
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ Options:
252252
253253
[env: OTLP_ENDPOINT=]
254254
255+
--otlp-service-name <OTLP_SERVICE_NAME>
256+
The service name for opentelemetry.
257+
258+
[env: OTLP_SERVICE_NAME=]
259+
[default: text-embeddings-inference.server]
260+
255261
--cors-allow-origin <CORS_ALLOW_ORIGIN>
256262
[env: CORS_ALLOW_ORIGIN=]
257263
```

backends/python/server/text_embeddings_server/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def serve(
2323
logger_level: str = "INFO",
2424
json_output: bool = False,
2525
otlp_endpoint: Optional[str] = None,
26+
otlp_service_name: str = "text-embeddings-inference.server",
2627
):
2728
# Remove default handler
2829
logger.remove()
@@ -42,7 +43,7 @@ def serve(
4243

4344
# Setup OpenTelemetry distributed tracing
4445
if otlp_endpoint is not None:
45-
setup_tracing(otlp_endpoint=otlp_endpoint)
46+
setup_tracing(otlp_endpoint=otlp_endpoint, otlp_service_name=otlp_service_name)
4647

4748
# Downgrade enum into str for easier management later on
4849
dtype = None if dtype is None else dtype.value

backends/python/server/text_embeddings_server/utils/tracing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ def _start_span(self, handler_call_details, context, set_status_on_exception=Fal
5454
)
5555

5656

57-
def setup_tracing(otlp_endpoint: str):
58-
resource = Resource.create(
59-
attributes={"service.name": f"text-embeddings-inference.server"}
60-
)
57+
def setup_tracing(otlp_endpoint: str, otlp_service_name: str):
58+
resource = Resource.create(attributes={"service.name": otlp_service_name})
6159
span_exporter = OTLPSpanExporter(endpoint=otlp_endpoint, insecure=True)
6260
span_processor = BatchSpanProcessor(span_exporter)
6361

backends/python/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl PythonBackend {
2222
model_type: ModelType,
2323
uds_path: String,
2424
otlp_endpoint: Option<String>,
25+
otlp_service_name: String,
2526
) -> Result<Self, BackendError> {
2627
match model_type {
2728
ModelType::Classifier => {
@@ -37,8 +38,13 @@ impl PythonBackend {
3738
}
3839
};
3940

40-
let backend_process =
41-
management::BackendProcess::new(model_path, dtype, &uds_path, otlp_endpoint)?;
41+
let backend_process = management::BackendProcess::new(
42+
model_path,
43+
dtype,
44+
&uds_path,
45+
otlp_endpoint,
46+
otlp_service_name,
47+
)?;
4248
let tokio_runtime = tokio::runtime::Builder::new_current_thread()
4349
.enable_all()
4450
.build()

backends/python/src/management.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ impl BackendProcess {
2121
dtype: String,
2222
uds_path: &str,
2323
otlp_endpoint: Option<String>,
24+
otlp_service_name: String,
2425
) -> Result<Self, BackendError> {
2526
// Get UDS path
2627
let uds = Path::new(uds_path);
@@ -33,21 +34,24 @@ impl BackendProcess {
3334
// Process args
3435
let mut python_server_args = vec![
3536
model_path,
36-
"--dtype".to_string(),
37+
"--dtype".to_owned(),
3738
dtype,
38-
"--uds-path".to_string(),
39-
uds_path.to_string(),
40-
"--logger-level".to_string(),
41-
"INFO".to_string(),
42-
"--json-output".to_string(),
39+
"--uds-path".to_owned(),
40+
uds_path.to_owned(),
41+
"--logger-level".to_owned(),
42+
"INFO".to_owned(),
43+
"--json-output".to_owned(),
4344
];
4445

4546
// OpenTelemetry
4647
if let Some(otlp_endpoint) = otlp_endpoint {
47-
python_server_args.push("--otlp-endpoint".to_string());
48+
python_server_args.push("--otlp-endpoint".to_owned());
4849
python_server_args.push(otlp_endpoint);
4950
}
5051

52+
python_server_args.push("--otlp-service-name".to_owned());
53+
python_server_args.push(otlp_service_name);
54+
5155
// Copy current process env
5256
let envs: Vec<(OsString, OsString)> = env::vars_os().collect();
5357

@@ -64,7 +68,7 @@ impl BackendProcess {
6468
Err(err) => {
6569
if err.kind() == io::ErrorKind::NotFound {
6670
return Err(BackendError::Start(
67-
"python-text-embeddings-server not found in PATH".to_string(),
71+
"python-text-embeddings-server not found in PATH".to_owned(),
6872
));
6973
}
7074
return Err(BackendError::Start(err.to_string()));

backends/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl Backend {
3838
model_type: ModelType,
3939
uds_path: String,
4040
otlp_endpoint: Option<String>,
41+
otlp_service_name: String,
4142
) -> Result<Self, BackendError> {
4243
let (backend_sender, backend_receiver) = mpsc::unbounded_channel();
4344

@@ -47,6 +48,7 @@ impl Backend {
4748
model_type.clone(),
4849
uds_path,
4950
otlp_endpoint,
51+
otlp_service_name,
5052
)?;
5153
let padded_model = backend.is_padded();
5254
let max_batch_size = backend.max_batch_size();
@@ -135,6 +137,7 @@ fn init_backend(
135137
model_type: ModelType,
136138
uds_path: String,
137139
otlp_endpoint: Option<String>,
140+
otlp_service_name: String,
138141
) -> Result<Box<dyn CoreBackend + Send>, BackendError> {
139142
if cfg!(feature = "candle") {
140143
#[cfg(feature = "candle")]
@@ -154,6 +157,7 @@ fn init_backend(
154157
model_type,
155158
uds_path,
156159
otlp_endpoint,
160+
otlp_service_name,
157161
)
158162
})
159163
.join()

docs/source/en/cli_arguments.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ Options:
153153
154154
[env: OTLP_ENDPOINT=]
155155
156+
--otlp-service-name <OTLP_SERVICE_NAME>
157+
The service name for opentelemetry.
158+
159+
[env: OTLP_SERVICE_NAME=]
160+
[default: text-embeddings-inference.server]
161+
156162
--cors-allow-origin <CORS_ALLOW_ORIGIN>
157163
[env: CORS_ALLOW_ORIGIN=]
158164
```

router/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub async fn run(
6060
payload_limit: usize,
6161
api_key: Option<String>,
6262
otlp_endpoint: Option<String>,
63+
otlp_service_name: String,
6364
cors_allow_origin: Option<Vec<String>>,
6465
) -> Result<()> {
6566
let model_id_path = Path::new(&model_id);
@@ -198,6 +199,7 @@ pub async fn run(
198199
backend_model_type,
199200
uds_path.unwrap_or("/tmp/text-embeddings-inference-server".to_string()),
200201
otlp_endpoint.clone(),
202+
otlp_service_name.clone(),
201203
)
202204
.context("Could not create backend")?;
203205
backend

router/src/logging.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ use tracing_subscriber::{EnvFilter, Layer};
1010
/// Init logging using env variables LOG_LEVEL and LOG_FORMAT:
1111
/// - otlp_endpoint is an optional URL to an Open Telemetry collector
1212
/// - LOG_LEVEL may be TRACE, DEBUG, INFO, WARN or ERROR (default to INFO)
13-
pub fn init_logging(otlp_endpoint: Option<&String>, json_output: bool) -> bool {
13+
pub fn init_logging(
14+
otlp_endpoint: Option<&String>,
15+
otlp_service_name: String,
16+
json_output: bool,
17+
) -> bool {
1418
let mut layers = Vec::new();
1519

1620
// STDOUT/STDERR layer
@@ -40,7 +44,7 @@ pub fn init_logging(otlp_endpoint: Option<&String>, json_output: bool) -> bool {
4044
trace::config()
4145
.with_resource(Resource::new(vec![KeyValue::new(
4246
"service.name",
43-
"text-embeddings-inference.router",
47+
otlp_service_name,
4448
)]))
4549
.with_sampler(Sampler::AlwaysOn),
4650
)

router/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ struct Args {
123123
#[clap(long, env)]
124124
otlp_endpoint: Option<String>,
125125

126+
/// The service name for opentelemetry.
127+
/// e.g. `text-embeddings-inference.server`
128+
#[clap(default_value = "text-embeddings-inference.server", long, env)]
129+
otlp_service_name: String,
130+
126131
/// Unused for gRPC servers
127132
#[clap(long, env)]
128133
cors_allow_origin: Option<Vec<String>>,
@@ -134,8 +139,11 @@ async fn main() -> Result<()> {
134139
let args: Args = Args::parse();
135140

136141
// Initialize logging and telemetry
137-
let global_tracer =
138-
text_embeddings_router::init_logging(args.otlp_endpoint.as_ref(), args.json_output);
142+
let global_tracer = text_embeddings_router::init_logging(
143+
args.otlp_endpoint.as_ref(),
144+
args.otlp_service_name.clone(),
145+
args.json_output,
146+
);
139147

140148
tracing::info!("{args:?}");
141149

@@ -158,6 +166,7 @@ async fn main() -> Result<()> {
158166
args.payload_limit,
159167
args.api_key,
160168
args.otlp_endpoint,
169+
args.otlp_service_name,
161170
args.cors_allow_origin,
162171
)
163172
.await?;

router/tests/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub async fn start_server(model_id: String, revision: Option<String>, dtype: DTy
6464
2_000_000,
6565
None,
6666
None,
67+
"text-embeddings-inference.server".to_owned(),
6768
None,
6869
)
6970
});

0 commit comments

Comments
 (0)