Skip to content

Commit

Permalink
fix: drop batch stdout support
Browse files Browse the repository at this point in the history
  • Loading branch information
erichulburd committed May 31, 2024
1 parent c9a86ce commit a74851d
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 202 deletions.
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
allowed-duplicate-crates = [
"base64",
"bitflags",
"hashbrown",
"indexmap",
"regex-automata",
Expand Down
2 changes: 1 addition & 1 deletion crates/tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ opentelemetry = { workspace = true }
opentelemetry-otlp = { version = "0.16.0", features = ["grpc-tonic", "trace", "tls-roots"], optional = true }
opentelemetry-proto = { version = "0.6.0", optional = true, features = ["tonic"] }
opentelemetry-stdout = { version = "0.4.0", optional = true, features = ["trace"] }
opentelemetry_sdk = { workspace = true, features = ["rt-tokio-current-thread"] }
opentelemetry_sdk = { workspace = true, features = ["rt-tokio", "rt-tokio-current-thread"] }
pyo3-asyncio = { workspace = true, features = ["tokio-runtime"] }
serde = { workspace = true }
thiserror = { workspace = true }
Expand Down
69 changes: 2 additions & 67 deletions crates/tracing-subscriber/src/contextmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ mod test {

use crate::{
contextmanager::{CurrentThreadTracingConfig, GlobalTracingConfig, TracingConfig},
export_process::{BatchConfig, ExportProcess, ExportProcessConfig, SimpleConfig},
export_process::{ExportProcess, ExportProcessConfig, SimpleConfig},
subscriber::TracingSubscriberRegistryConfig,
};

Expand Down Expand Up @@ -236,71 +236,6 @@ mod test {
end_time_unix_nano: u128,
}

#[test]
/// Test that a global batch process can be started and stopped and that it exports
/// accurate spans as configured.
fn test_global_batch() {
let temporary_file_path = get_tempfile("test_global_batch");
let layer_config = Box::new(crate::layers::otel_otlp_file::Config {
file_path: Some(temporary_file_path.as_os_str().to_str().unwrap().to_owned()),
filter: Some("error,pyo3_tracing_subscriber=info".to_string()),
});
let subscriber = Box::new(TracingSubscriberRegistryConfig { layer_config });
let config = TracingConfig::Global(GlobalTracingConfig {
export_process: ExportProcessConfig::Batch(BatchConfig {
subscriber: crate::subscriber::PyConfig {
subscriber_config: subscriber,
},
}),
});
let export_process = ExportProcess::start(config).unwrap();
let rt2 = Builder::new_current_thread().enable_time().build().unwrap();
let _guard = rt2.enter();
let export_runtime = rt2
.block_on(tokio::time::timeout(Duration::from_secs(1), async move {
for _ in 0..N_SPANS {
example();
}
export_process.shutdown().await
}))
.unwrap()
.unwrap()
.unwrap();
drop(export_runtime);

let reader = std::io::BufReader::new(std::fs::File::open(temporary_file_path).unwrap());
let lines = reader.lines();
let spans = lines
.flat_map(|line| {
let line = line.unwrap();
let span_data: SpanData = serde_json::from_str(line.as_str()).unwrap();
span_data
.resource_spans
.iter()
.flat_map(|resource_span| {
resource_span
.scope_spans
.iter()
.flat_map(|scope_span| scope_span.spans.clone())
})
.collect::<Vec<Span>>()
})
.collect::<Vec<Span>>();
assert_eq!(spans.len(), N_SPANS);

let span_grace = Duration::from_millis(200);
for span in spans {
assert_eq!(span.name, "example");
assert!(
span.end_time_unix_nano - span.start_time_unix_nano >= SPAN_DURATION.as_nanos()
);
assert!(
(span.end_time_unix_nano - span.start_time_unix_nano)
<= (SPAN_DURATION.as_nanos() + span_grace.as_nanos())
);
}
}

#[test]
/// Test that a global simple export process can be started and stopped and that it
/// exports accurate spans as configured.
Expand Down Expand Up @@ -352,7 +287,7 @@ mod test {
.collect::<Vec<Span>>();
assert_eq!(spans.len(), N_SPANS);

let span_grace = Duration::from_millis(10);
let span_grace = Duration::from_millis(50);
for span in spans {
assert_eq!(span.name, "example");
assert!(
Expand Down
3 changes: 1 addition & 2 deletions crates/tracing-subscriber/src/layers/otel_otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::{collections::HashMap, time::Duration};

use opentelemetry_otlp::{TonicExporterBuilder, WithExportConfig};
use opentelemetry_sdk::{
runtime::TokioCurrentThread,
trace::{Sampler, SpanLimits},
Resource,
};
Expand Down Expand Up @@ -103,7 +102,7 @@ impl Config {
);

let tracer = if batch {
pipeline.install_batch(TokioCurrentThread {})
pipeline.install_batch(opentelemetry_sdk::runtime::Tokio {})
} else {
pipeline.install_simple()
}
Expand Down
20 changes: 9 additions & 11 deletions crates/tracing-subscriber/src/layers/otel_otlp_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use crate::create_init_submodule;
use opentelemetry_sdk::runtime::TokioCurrentThread;
use pyo3::prelude::*;
use tracing_subscriber::Layer;

Expand Down Expand Up @@ -52,16 +51,13 @@ impl crate::layers::Config for Config {
}
None => exporter_builder,
};
let provider = if batch {
opentelemetry_sdk::trace::TracerProvider::builder()
.with_batch_exporter(exporter_builder.build(), TokioCurrentThread)
.build()
} else {
opentelemetry_sdk::trace::TracerProvider::builder()
.with_simple_exporter(exporter_builder.build())
.build()
};
let tracer = provider.tracer("pyo3-opentelemetry-stdout");
if batch {
return Err(BuildError::BatchModeNotSupported)?;
}
let provider = opentelemetry_sdk::trace::TracerProvider::builder()
.with_simple_exporter(exporter_builder.build())
.build();
let tracer = provider.tracer("pyo3_tracing_subscriber");
let env_filter = build_env_filter(self.filter.clone())?;
let layer = tracing_opentelemetry::layer()
.with_tracer(tracer)
Expand All @@ -77,6 +73,8 @@ impl crate::layers::Config for Config {
pub(crate) enum BuildError {
#[error("failed to initialize file span exporter for specified file path: {0}")]
InvalidFile(#[from] std::io::Error),
#[error("batch mode is not supported for stdout layer")]
BatchModeNotSupported,
}

create_init_submodule! {
Expand Down
17 changes: 8 additions & 9 deletions examples/pyo3-opentelemetry-lib/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@
# separate process; that proved non-trivial on a first attempt, as the tests ran into sevaral unexpected
# failures.
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export[<lambda>2]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export[<lambda>3]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export_async[<lambda>0]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export_async[<lambda>1]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export[config2]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export[config3]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export_async[config0]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export_async[config1]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export[02]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export[03]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_file_export_asynchronous[00]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export[02]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export[03]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export_asynchronous[00]' --with-global-tracing-configuration
poetry run pytest 'pyo3_opentelemetry_lib/test/tracing_test.py::test_otlp_export_asynchronous[01]' --with-global-tracing-configuration
'''

[tasks.python-check-all]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@

from pyo3_opentelemetry_lib import _tracing_subscriber


__doc__ = _tracing_subscriber.__doc__
__all__ = getattr(_tracing_subscriber, "__all__", [])
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@

from pyo3_opentelemetry_lib._tracing_subscriber import layers


__doc__ = layers.__doc__
__all__ = getattr(layers, "__all__", [])

Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
# *****************************************************************************

from __future__ import annotations

from typing import TYPE_CHECKING

from . import file as file
from . import otel_otlp_file as otel_otlp_file
from . import file as file
from . import otel_otlp as otel_otlp
from . import otel_otlp_file as otel_otlp_file

if TYPE_CHECKING:
from typing import Union
from typing import Union

Config = Union[
file.Config,
otel_otlp_file.Config,
otel_otlp.Config,
]
"""
Config = Union[
file.Config,
otel_otlp_file.Config,
otel_otlp.Config,
]
"""
One of the supported layer configurations that may be set on the subscriber configuration.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,5 @@

from pyo3_opentelemetry_lib._tracing_subscriber.layers import file


__doc__ = file.__doc__
__all__ = getattr(file, "__all__", [])


Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ class Config:
:param json: Whether or not to format the output as JSON. Defaults to `True`.
"""
...

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@

from pyo3_opentelemetry_lib._tracing_subscriber.layers import otel_otlp


__doc__ = otel_otlp.__doc__
__all__ = getattr(otel_otlp, "__all__", [])

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# *****************************************************************************

from __future__ import annotations
from typing import Dict, Optional, TYPE_CHECKING, final

from typing import TYPE_CHECKING, Dict, Optional, final

@final
class SpanLimits:
Expand Down Expand Up @@ -47,8 +48,6 @@ class Resource:
schema_url: Optional[str] = None,
) -> "Resource": ...



@final
class Config:
"""
Expand Down Expand Up @@ -98,14 +97,14 @@ class Config:
...

if TYPE_CHECKING:
from typing import List, Union
from typing import List, Union

ResourceValueArray = Union[List[bool], List[int], List[float], List[str]]
"""
An array of `ResourceValue`s. This array is homogenous, so all values must be of the same type.
"""

ResourceValue= Union[bool, int, float, str, ResourceValueArray]
ResourceValue = Union[bool, int, float, str, ResourceValueArray]
"""
A value that can be added to a `Resource`.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@

from pyo3_opentelemetry_lib._tracing_subscriber.layers import otel_otlp_file


__doc__ = otel_otlp_file.__doc__
__all__ = getattr(otel_otlp_file, "__all__", [])

Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ class Config:
and then `RUST_LOG` environment variable. If all of these values are empty, no spans will be exported.
"""
...

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@

from pyo3_opentelemetry_lib._tracing_subscriber import subscriber


__doc__ = subscriber.__doc__
__all__ = getattr(subscriber, "__all__", [])

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ class Config:
"""

def __new__(cls, *, layer: layers.Config) -> "Config": ...

Empty file.
Loading

0 comments on commit a74851d

Please sign in to comment.