Skip to content

Commit

Permalink
Merge branch 'main' into codeboten/fstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
alrex authored Sep 28, 2021
2 parents f9e5da4 + 2710e25 commit 21f8aff
Show file tree
Hide file tree
Showing 29 changed files with 643 additions and 119 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ exclude =
exporter/opentelemetry-exporter-jaeger/build/*
docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/gen/
docs/examples/opentelemetry-example-app/build/*
opentelemetry-python-core/
opentelemetry-proto/build/*
opentelemetry-proto/src/opentelemetry/proto/
17 changes: 1 addition & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'release/*'
pull_request:
env:
CORE_REPO_SHA: c49ad57bfe35cfc69bfa863d74058ca9bec55fc3
CORE_REPO_SHA: 10208c1be1e720925a80a66f711b8afbe67537f4

jobs:
build:
Expand All @@ -28,12 +28,6 @@ jobs:
steps:
- name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v2
- name: Checkout Core Repo @ SHA - ${{ env.CORE_REPO_SHA }}
uses: actions/checkout@v2
with:
repository: open-telemetry/opentelemetry-python
ref: ${{ env.CORE_REPO_SHA }}
path: opentelemetry-python-core
- name: Set up Python ${{ env[matrix.python-version] }}
uses: actions/setup-python@v2
with:
Expand Down Expand Up @@ -84,12 +78,6 @@ jobs:
steps:
- name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v2
- name: Checkout Core Repo @ SHA ${{ env.CORE_REPO_SHA }}
uses: actions/checkout@v2
with:
repository: open-telemetry/opentelemetry-python
ref: ${{ env.CORE_REPO_SHA }}
path: opentelemetry-python-core
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
Expand All @@ -109,6 +97,3 @@ jobs:
key: v4-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }}
- name: run tox
run: tox -e ${{ matrix.tox-environment }}
- name: Ensure generated code is up to date
if: matrix.tox-environment == 'generate'
run: git diff --exit-code || (echo 'Generated code is out of date, please run "tox -e generate" and commit the changes in this PR.' && exit 1)
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ lib64
__pycache__
venv*/
.venv*/
opentelemetry-python-core*/
/opentelemetry-python-core

# Installer logs
pip-log.txt
Expand Down
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ profile=black
; docs: https://github.com/timothycrosley/isort#multi-line-output-modes
multi_line_output=3
skip=target
skip_glob=**/gen/*,.venv*/*,venv*/*,opentelemetry-python-core/*,.tox/*
skip_glob=**/gen/*,.venv*/*,venv*/*,.tox/*
known_first_party=opentelemetry
known_third_party=psutil,pytest,redis,redis_opentracing
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-urllib3` Updated `_RequestHookT` with two additional fields - the request body and the request headers
([#660](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/660))

### Changed
- Tests for Falcon 3 support
([#644](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/644))

### Added

- `opentelemetry-instrumentation-urllib3`, `opentelemetry-instrumentation-requests`
The `net.peer.ip` attribute is set to the IP of the connected HTTP server or proxy
using a new instrumentor in `opententelemetry-util-http`
([#661](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/661))

## [1.5.0-0.24b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.5.0-0.24b0) - 2021-08-26

### Added
Expand Down
14 changes: 7 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ For a deeper discussion, see: https://github.com/open-telemetry/opentelemetry-sp
## Running Tests Locally

1. Go to your Contrib repo directory. `git clone git@github.com:open-telemetry/opentelemetry-python-contrib.git && cd opentelemetry-python-contrib`.
2. Clone the [OpenTelemetry Python](https://github.com/open-telemetry/opentelemetry-python) Python Core repo to a folder named `opentelemetry-python-core`. `git clone https://github.com/open-telemetry/opentelemetry-python.git opentelemetry-python-core`.
3. Make sure you have `tox` installed. `pip install tox`.
4. Run `tox` without any arguments to run tests for all the packages. Read more about [tox](https://tox.readthedocs.io/en/latest/).
2. Make sure you have `tox` installed. `pip install tox`.
3. Run `tox` without any arguments to run tests for all the packages. Read more about [tox](https://tox.readthedocs.io/en/latest/).

### Testing against a different Core repo branch/commit

1. Change directory to the repo that was cloned above. `cd opentelemetry-python-core`.
2. Move the head of this repo to the SHA hash you want your tests to use. The current SHA hash can be found in `.github/workflows/test.yml` file under the `opentelemetry-python-contrib` directory. For example, currently it is `1a12fa0d681e37c1fda9cb8d46212ff3bbf6b76a`. So use `git fetch && git checkout <current SHA hash>`.
3. Go back to the root directory. `cd ../`.
4. With `tox` installed, run tests for a package. (e.g. `tox -e test-instrumentation-flask`.)
Some of the tox targets install packages from the [OpenTelemetry Python Core Repository](https://github.com/open-telemetry/opentelemetry-python) via pip. The version of the packages installed defaults to the main branch in that repository when tox is run locally. It is possible to install packages tagged with a specific git commit hash by setting an environment variable before running tox as per the following example:

CORE_REPO_SHA=c49ad57bfe35cfc69bfa863d74058ca9bec55fc3 tox

The continuation integration overrides that environment variable with as per the configuration [here](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/.github/workflows/test.yml#L9).


## Style Guide
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,8 @@ Maintainers ([@open-telemetry/python-maintainers](https://github.com/orgs/open-t
1. Go to your Contrib repo directory. `cd ~/git/opentelemetry-python-contrib`.
2. Create a virtual env in your Contrib repo directory. `python3 -m venv my_test_venv`.
3. Activate your virtual env. `source my_test_venv/bin/activate`.
4. Clone the [OpenTelemetry Python](https://github.com/open-telemetry/opentelemetry-python) Python Core repo to a folder named `opentelemetry-python-core`. `git clone git@github.com:open-telemetry/opentelemetry-python.git opentelemetry-python-core`.
5. Change directory to the repo that was just cloned. `cd opentelemetry-python-core`.
6. Move the head of this repo to the hash you want your tests to use. This is currently the SHA `47483865854c7adae7455f8441dab7f814f4ce2a` as seen in `.github/workflows/test.yml`. Use `git fetch && git checkout 47483865854c7adae7455f8441dab7f814f4ce2a`.
7. Go back to the root directory. `cd ../`.
8. Make sure you have `tox` installed. `pip install tox`.
9. Run tests for a package. (e.g. `tox -e test-instrumentation-flask`.)
4. Make sure you have `tox` installed. `pip install tox`.
5. Run tests for a package. (e.g. `tox -e test-instrumentation-flask`.)

### Thanks to all the people who already contributed!

Expand Down
1 change: 1 addition & 0 deletions _template/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ classifiers =
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9

[options]
python_requires = >=3.6
Expand Down
8 changes: 2 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@

settings.configure()

source_dirs = [
os.path.abspath(
"../opentelemetry-python-core/opentelemetry-instrumentation/src/"
),
]
source_dirs = []

exp = "../exporter"
exp_dirs = [
Expand All @@ -51,7 +47,7 @@
if isdir(join(sdk_ext, f))
]

sys.path[:0] = source_dirs + exp_dirs + instr_dirs + sdk_ext_dirs
sys.path[:0] = exp_dirs + instr_dirs + sdk_ext_dirs

# -- Project information -----------------------------------------------------

Expand Down
2 changes: 0 additions & 2 deletions eachdist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
[DEFAULT]
ignore=
_template
reference
opentelemetry-python-core

sortfirst=
util/opentelemetry-util-http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,18 +550,28 @@ def test_span_processor_accepts_parent_context(self):
)

def test_origin(self):
context = trace_api.SpanContext(
trace_id=0x000000000000000000000000DEADBEEF,
span_id=trace_api.INVALID_SPAN,
is_remote=True,
trace_state=trace_api.TraceState(
[(datadog.constants.DD_ORIGIN, "origin-service")]
),
trace_id = 0x000000000000000000000000DEADBEEF
trace_state = trace_api.TraceState(
[(datadog.constants.DD_ORIGIN, "origin-service")]
)
parent_span_ctx = trace_api.SpanContext(
trace_id=trace_id,
span_id=0x1,
is_remote=False,
trace_state=trace_state,
)
child_span_ctx = trace_api.SpanContext(
trace_id=trace_id,
span_id=0x2,
is_remote=False,
trace_state=trace_state,
)

root_span = trace._Span(name="root", context=context, parent=None)
root_span = trace._Span(
name="root", context=parent_span_ctx, parent=None
)
child_span = trace._Span(
name="child", context=context, parent=root_span
name="child", context=child_span_ctx, parent=parent_span_ctx
)
root_span.start()
child_span.start()
Expand Down
2 changes: 1 addition & 1 deletion gen-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ astor==0.8.1
jinja2~=2.7
isort
black

requests
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| [opentelemetry-instrumentation-dbapi](./opentelemetry-instrumentation-dbapi) | dbapi |
| [opentelemetry-instrumentation-django](./opentelemetry-instrumentation-django) | django >= 1.10 |
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 2.0 |
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon ~= 2.0 |
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 2.0.0, < 4.0.0 |
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 |
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0, < 3.0 |
| [opentelemetry-instrumentation-grpc](./opentelemetry-instrumentation-grpc) | grpcio ~= 1.27 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ OpenTelemetry Falcon Tracing
This library builds on the OpenTelemetry WSGI middleware to track web requests
in Falcon applications.

Currently, only Falcon v2 is supported.

Installation
------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ def response_hook(span, req, resp):

_response_propagation_setter = FuncSetter(falcon.Response.append_header)

if hasattr(falcon, "App"):
# Falcon 3
_instrument_app = "App"
else:
# Falcon 2
_instrument_app = "API"


class FalconInstrumentor(BaseInstrumentor):
# pylint: disable=protected-access,attribute-defined-outside-init
Expand All @@ -138,14 +145,16 @@ def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
self._original_falcon_api = falcon.API
falcon.API = partial(_InstrumentedFalconAPI, **kwargs)
self._original_falcon_api = getattr(falcon, _instrument_app)
setattr(
falcon, _instrument_app, partial(_InstrumentedFalconAPI, **kwargs)
)

def _uninstrument(self, **kwargs):
falcon.API = self._original_falcon_api
setattr(falcon, _instrument_app, self._original_falcon_api)


class _InstrumentedFalconAPI(falcon.API):
class _InstrumentedFalconAPI(getattr(falcon, _instrument_app)):
def __init__(self, *args, **kwargs):
# inject trace middleware
middlewares = kwargs.pop("middleware", [])
Expand All @@ -169,6 +178,15 @@ def __init__(self, *args, **kwargs):
self._excluded_urls = get_excluded_urls("FALCON")
super().__init__(*args, **kwargs)

def _handle_exception(
self, req, resp, ex, params
): # pylint: disable=C0103
# Falcon 3 does not execute middleware within the context of the exception
# so we capture the exception here and save it into the env dict
_, exc, _ = exc_info()
req.env[_ENVIRON_EXC] = exc
return super()._handle_exception(req, resp, ex, params)

def __call__(self, env, start_response):
# pylint: disable=E1101
if self._excluded_urls.url_disabled(env.get("PATH_INFO", "/")):
Expand All @@ -193,7 +211,6 @@ def __call__(self, env, start_response):
env[_ENVIRON_ACTIVATION_KEY] = activation

def _start_response(status, response_headers, *args, **kwargs):
otel_wsgi.add_response_attributes(span, status, response_headers)
response = start_response(
status, response_headers, *args, **kwargs
)
Expand Down Expand Up @@ -262,29 +279,32 @@ def process_response(
if resource is None:
status = "404"
reason = "NotFound"

exc_type, exc, _ = exc_info()
if exc_type and not req_succeeded:
if "HTTPNotFound" in exc_type.__name__:
status = "404"
reason = "NotFound"
else:
if _ENVIRON_EXC in req.env:
exc = req.env[_ENVIRON_EXC]
exc_type = type(exc)
else:
status = "500"
reason = f"{exc_type.__name__}: {exc}"
exc_type, exc = None, None
if exc_type and not req_succeeded:
if "HTTPNotFound" in exc_type.__name__:
status = "404"
reason = "NotFound"
else:
status = "500"
reason = f"{exc_type.__name__}: {exc}"

status = status.split(" ")[0]
try:
status_code = int(status)
except ValueError:
pass
finally:
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
span.set_status(
Status(
status_code=http_status_to_status_code(status_code),
description=reason,
)
)
except ValueError:
pass

propagator = get_global_response_propagator()
if propagator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.


_instruments = ("falcon ~= 2.0",)
_instruments = ("falcon >= 2.0.0, < 4.0.0",)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def on_get(self, req, resp):


def make_app():
app = falcon.API()
if hasattr(falcon, "App"):
# Falcon 3
app = falcon.App()
else:
# Falcon 2
app = falcon.API()
app.add_route("/hello", HelloWorldResource())
app.add_route("/ping", HelloWorldResource())
app.add_route("/error", ErrorResource())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ def test_head(self):
self._test_method("HEAD")

def _test_method(self, method):
self.client().simulate_request(method=method, path="/hello")
self.client().simulate_request(
method=method, path="/hello", remote_addr="127.0.0.1"
)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, f"HelloWorldResource.on_{method.lower()}")
self.assertEqual(span.status.status_code, StatusCode.UNSET)
self.assertEqual(
span.status.description, None,
)
self.assertSpanHasAttributes(
span,
{
Expand All @@ -103,12 +108,15 @@ def _test_method(self, method):
self.memory_exporter.clear()

def test_404(self):
self.client().simulate_get("/does-not-exist")
self.client().simulate_get("/does-not-exist", remote_addr="127.0.0.1")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, "HTTP GET")
self.assertEqual(span.status.status_code, StatusCode.ERROR)
self.assertEqual(
span.status.description, "NotFound",
)
self.assertSpanHasAttributes(
span,
{
Expand All @@ -127,7 +135,7 @@ def test_404(self):

def test_500(self):
try:
self.client().simulate_get("/error")
self.client().simulate_get("/error", remote_addr="127.0.0.1")
except NameError:
pass
spans = self.memory_exporter.get_finished_spans()
Expand Down
Loading

0 comments on commit 21f8aff

Please sign in to comment.