Skip to content

Commit

Permalink
Merge branch 'main' into fix-asyncio-unittest-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored May 30, 2024
2 parents e86f7d9 + 7bddbb5 commit 1d390d7
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 95 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2397](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2397)))
- `opentelemetry-processor-baggage` Initial release
([#2436](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2436))
- `opentelemetry-processor-baggage` Add baggage key predicate
([#2535](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2535))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,39 @@
annotated-types==0.6.0
asgiref==3.7.2
attrs==23.2.0
aws-sam-translator==1.85.0
aws-xray-sdk==2.12.1
boto==2.49.0
boto3==1.34.44
botocore==1.34.44
certifi==2024.2.2
cffi==1.16.0
cfn-lint==0.85.2
charset-normalizer==3.3.2
cryptography==42.0.3
Deprecated==1.2.14
docker==7.0.0
ecdsa==0.18.0
graphql-core==3.2.3
idna==3.6
importlib-metadata==6.11.0
importlib-resources==6.1.1
iniconfig==2.0.0
Jinja2==3.1.3
Jinja2==3.1.4
jmespath==1.0.1
jschema-to-python==1.2.3
jsondiff==2.0.0
jsonpatch==1.33
jsonpickle==3.0.2
jsonpointer==2.4
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
junit-xml==1.9
MarkupSafe==2.1.5
moto==2.3.2
mpmath==1.3.0
networkx==3.1
packaging==23.2
pbr==6.0.0
pkgutil_resolve_name==1.3.10
pluggy==1.4.0
py==1.11.0
py-cpuinfo==9.0.0
pyasn1==0.5.1
pycparser==2.21
pydantic==2.6.1
pydantic_core==2.16.2
pytest==7.1.3
pytest-benchmark==4.0.0
python-dateutil==2.8.2
python-jose==3.3.0
pytz==2024.1
PyYAML==6.0.1
referencing==0.33.0
regex==2023.12.25
requests==2.31.0
responses==0.25.0
rpds-py==0.18.0
rsa==4.9
s3transfer==0.10.0
sarif-om==1.0.4
six==1.16.0
sshpubkeys==3.3.1
sympy==1.12
tomli==2.0.1
typing_extensions==4.9.0
urllib3==1.26.18
Werkzeug==3.0.1
Werkzeug==2.1.2
wrapt==1.16.0
xmltodict==0.13.0
zipp==3.17.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,35 @@
annotated-types==0.6.0
asgiref==3.7.2
attrs==23.2.0
aws-sam-translator==1.85.0
aws-xray-sdk==2.12.1
boto3==1.28.80
botocore==1.31.80
certifi==2024.2.2
cffi==1.16.0
cfn-lint==0.85.2
charset-normalizer==3.3.2
cryptography==42.0.5
Deprecated==1.2.14
docker==7.0.0
ecdsa==0.18.0
idna==3.6
importlib-metadata==6.11.0
importlib-resources==6.1.1
iniconfig==2.0.0
Jinja2==3.1.3
Jinja2==3.1.4
jmespath==1.0.1
jschema-to-python==1.2.3
jsondiff==2.0.0
jsonpatch==1.33
jsonpickle==3.0.3
jsonpointer==2.4
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
junit-xml==1.9
MarkupSafe==2.0.1
moto==3.1.19
mpmath==1.3.0
networkx==3.1
packaging==23.2
pbr==6.0.0
pkgutil_resolve_name==1.3.10
pluggy==1.4.0
py==1.11.0
py-cpuinfo==9.0.0
pyasn1==0.5.1
pycparser==2.21
pydantic==2.6.2
pydantic_core==2.16.3
pytest==7.1.3
pytest-benchmark==4.0.0
python-dateutil==2.8.2
python-jose==3.3.0
pytz==2024.1
PyYAML==6.0.1
referencing==0.33.0
regex==2023.12.25
requests==2.31.0
responses==0.25.0
rpds-py==0.18.0
rsa==4.9
s3transfer==0.7.0
sarif-om==1.0.4
six==1.16.0
sshpubkeys==3.3.1
sympy==1.12
tomli==2.0.1
typing_extensions==4.9.0
urllib3==1.26.18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def test_basic_post_request_metric_success(self):
if isinstance(point, NumberDataPoint):
self.assertEqual(point.value, 0)

def test_metric_uninstruemnt_app(self):
def test_metric_uninstrument_app(self):
self._client.get("/foobar")
self._instrumentor.uninstrument_app(self._app)
self._client.get("/foobar")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_basic_metric_success(self):
)
self.assertEqual(point.value, 0)

def test_metric_uninstruemnt(self):
def test_metric_uninstrument(self):
self.client.get("/hello/756")
PyramidInstrumentor().uninstrument()
self.config = Configurator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ classifiers = [
dependencies = [
"opentelemetry-instrumentation == 0.46b0.dev",
"opentelemetry-api ~= 1.11",
"opentelemetry-sdk ~= 1.11",
"psutil ~= 5.9",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
from opentelemetry.instrumentation.system_metrics.package import _instruments
from opentelemetry.instrumentation.system_metrics.version import __version__
from opentelemetry.metrics import CallbackOptions, Observation, get_meter
from opentelemetry.sdk.util import get_dict_as_key

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -638,8 +637,8 @@ def _get_system_network_connections(
net_connection, metric
)

connection_counters_key = get_dict_as_key(
self._system_network_connections_labels
connection_counters_key = tuple(
sorted(self._system_network_connections_labels.items())
)

if connection_counters_key in connection_counters:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
asgiref==3.7.2
attrs==23.2.0
confluent-kafka==2.3.0
Deprecated==1.2.14
importlib-metadata==6.11.0
iniconfig==2.0.0
Expand Down
17 changes: 5 additions & 12 deletions opentelemetry-distro/tests/test_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# type: ignore

import os
from unittest import TestCase
from unittest import TestCase, mock

from pkg_resources import DistributionNotFound, require

Expand All @@ -33,17 +33,10 @@ def test_package_available(self):
except DistributionNotFound:
self.fail("opentelemetry-distro not installed")

@mock.patch.dict("os.environ", {}, clear=True)
def test_default_configuration(self):
distro = OpenTelemetryDistro()
self.assertIsNone(os.environ.get(OTEL_TRACES_EXPORTER))
self.assertIsNone(os.environ.get(OTEL_METRICS_EXPORTER))
distro.configure()
self.assertEqual(
"otlp", os.environ.get(OTEL_TRACES_EXPORTER)
)
self.assertEqual(
"otlp", os.environ.get(OTEL_METRICS_EXPORTER)
)
self.assertEqual(
"grpc", os.environ.get(OTEL_EXPORTER_OTLP_PROTOCOL)
)
self.assertEqual("otlp", os.environ.get(OTEL_TRACES_EXPORTER))
self.assertEqual("otlp", os.environ.get(OTEL_METRICS_EXPORTER))
self.assertEqual("grpc", os.environ.get(OTEL_EXPORTER_OTLP_PROTOCOL))
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ def configure(self, **kwargs):
def load_instrumentor( # pylint: disable=no-self-use
self, entry_point: EntryPoint, **kwargs
):
"""Takes a collection of instrumentation entry points
and activates them by instantiating and calling instrument()
on each one.
"""Takes an instrumentation entry point and activates it by instantiating
and calling instrument() on it.
This is called for each opentelemetry_instrumentor entry point by auto
instrumentation.
Distros can override this method to customize the behavior by
inspecting each entry point and configuring them in special ways,
Expand Down
29 changes: 29 additions & 0 deletions processor/opentelemetry-processor-baggage/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,32 @@ Do not put sensitive information in Baggage.

To repeat: a consequence of adding data to Baggage is that the keys and
values will appear in all outgoing HTTP headers from the application.

## Usage

Add the span processor when configuring the tracer provider.

To configure the span processor to copy all baggage entries during configuration:

```python
from opentelemetry.processor.baggage import BaggageSpanProcessor, ALLOW_ALL_BAGGAGE_KEYS
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BaggageSpanProcessor(ALLOW_ALL_BAGGAGE_KEYS))
```

Alternatively, you can provide a custom baggage key predicate to select which baggage keys you want to copy.

For example, to only copy baggage entries that start with `my-key`:

```python
starts_with_predicate = lambda baggage_key: baggage_key.startswith("my-key")
tracer_provider.add_span_processor(BaggageSpanProcessor(starts_with_predicate))
```

For example, to only copy baggage entries that match the regex `^key.+`:

```python
regex_predicate = lambda baggage_key: baggage_key.startswith("^key.+")
tracer_provider.add_span_processor(BaggageSpanProcessor(regex_predicate))
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# pylint: disable=import-error

from .processor import BaggageSpanProcessor
from .processor import ALLOW_ALL_BAGGAGE_KEYS, BaggageSpanProcessor
from .version import __version__

__all__ = ["BaggageSpanProcessor", "__version__"]
__all__ = ["ALLOW_ALL_BAGGAGE_KEYS", "BaggageSpanProcessor", "__version__"]
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional
from typing import Callable, Optional

from opentelemetry.baggage import get_all as get_all_baggage
from opentelemetry.context import Context
from opentelemetry.sdk.trace.export import SpanProcessor
from opentelemetry.trace import Span

# A BaggageKeyPredicate is a function that takes a baggage key and returns a boolean
BaggageKeyPredicateT = Callable[[str], bool]

# A BaggageKeyPredicate that always returns True, allowing all baggage keys to be added to spans
ALLOW_ALL_BAGGAGE_KEYS: BaggageKeyPredicateT = lambda _: True


class BaggageSpanProcessor(SpanProcessor):
"""
Expand All @@ -44,12 +50,13 @@ class BaggageSpanProcessor(SpanProcessor):
"""

def __init__(self) -> None:
pass
def __init__(self, baggage_key_predicate: BaggageKeyPredicateT) -> None:
self._baggage_key_predicate = baggage_key_predicate

def on_start(
self, span: "Span", parent_context: Optional[Context] = None
) -> None:
baggage = get_all_baggage(parent_context)
for key, value in baggage.items():
span.set_attribute(key, value)
if self._baggage_key_predicate(key):
span.set_attribute(key, value)
Loading

0 comments on commit 1d390d7

Please sign in to comment.