feat: improve plugin logging interface ()

* feat: improve plugin logging interface

Updates the plugin log functions so they can take any number of Python objects which will be converted into a single log line string.

Closes 

* refactor: udpate on PR feedback
pull/25861/head
Paul Dix 2025-01-16 18:02:14 -05:00 committed by GitHub
parent daa5cbd270
commit eb0b1eb8c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 8 deletions
influxdb3/tests/server
influxdb3_py_api/src

View File

@ -949,6 +949,9 @@ def process_writes(influxdb3_local, table_batches, args=None):
query_params = {"host": args["host"]}
query_result = influxdb3_local.query("SELECT host, region, usage FROM cpu where host = $host", query_params)
influxdb3_local.info("query result: " + str(query_result))
influxdb3_local.info("i", query_result, args["host"])
influxdb3_local.warn("w:", query_result)
influxdb3_local.error("err", query_result)
for table_batch in table_batches:
influxdb3_local.info("table: " + table_batch["table_name"])
@ -1018,6 +1021,9 @@ def process_writes(influxdb3_local, table_batches, args=None):
"log_lines": [
"INFO: arg1: arg1_value",
"INFO: query result: [{'host': 's2', 'region': 'us-east', 'usage': 0.89}]",
"INFO: i [{'host': 's2', 'region': 'us-east', 'usage': 0.89}] s2",
"WARN: w: [{'host': 's2', 'region': 'us-east', 'usage': 0.89}]",
"ERROR: err [{'host': 's2', 'region': 'us-east', 'usage': 0.89}]",
"INFO: table: test_input",
"INFO: row: {'tag1': 'tag1_value', 'tag2': 'tag2_value', 'field1': 1, 'time': 500}",
"INFO: done"

View File

@ -17,7 +17,7 @@ use observability_deps::tracing::{error, info, warn};
use parking_lot::Mutex;
use pyo3::exceptions::{PyException, PyValueError};
use pyo3::prelude::{PyAnyMethods, PyModule};
use pyo3::types::{PyDict, PyList};
use pyo3::types::{PyDict, PyList, PyTuple};
use pyo3::{
create_exception, pyclass, pymethods, pymodule, Bound, IntoPyObject, Py, PyAny, PyObject,
PyResult, Python,
@ -72,16 +72,19 @@ impl std::fmt::Debug for LogLine {
#[pymethods]
impl PyPluginCallApi {
fn info(&self, line: &str) -> PyResult<()> {
#[pyo3(signature = (*args))]
fn info(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
let line = self.log_args_to_string(args)?;
info!("processing engine: {}", line);
self.return_state
.lock()
.log_lines
.push(LogLine::Info(line.to_string()));
self.return_state.lock().log_lines.push(LogLine::Info(line));
Ok(())
}
fn warn(&self, line: &str) -> PyResult<()> {
#[pyo3(signature = (*args))]
fn warn(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
let line = self.log_args_to_string(args)?;
warn!("processing engine: {}", line);
self.return_state
.lock()
@ -90,7 +93,10 @@ impl PyPluginCallApi {
Ok(())
}
fn error(&self, line: &str) -> PyResult<()> {
#[pyo3(signature = (*args))]
fn error(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
let line = self.log_args_to_string(args)?;
error!("processing engine: {}", line);
self.return_state
.lock()
@ -99,6 +105,15 @@ impl PyPluginCallApi {
Ok(())
}
fn log_args_to_string(&self, args: &Bound<'_, PyTuple>) -> PyResult<String> {
let line = args
.try_iter()?
.map(|arg| arg?.str()?.extract::<String>())
.collect::<Result<Vec<String>, _>>()?
.join(" ");
Ok(line)
}
fn write(&self, line_builder: &Bound<'_, PyAny>) -> PyResult<()> {
// Get the built line from the LineBuilder object
let line = line_builder.getattr("build")?.call0()?;