Skip to content

Commit

Permalink
Minor Releases (#776)
Browse files Browse the repository at this point in the history
* Bump versions

* Apply Requests library spec fidelity

* Fixes value for `http.route` in Flask middleware

* Standard Metrics - Incoming Requests Per Second  (#758)

* Incoming requests for Python3

* python2 support

* change namespace

* add tests

* More tests

* Fix lint

* Fix lint

* Add test

* Add CHANGELOG

* Hotfix/django flask pyramid status code

* Fixed requests contrib to raise original exceptions
  • Loading branch information
lzchen authored Aug 26, 2019
1 parent bcda851 commit f109f0a
Show file tree
Hide file tree
Showing 42 changed files with 1,069 additions and 112 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

## Unreleased

## 0.7.3
Released 2019-08-26

- Added `http code` to `grpc code` status code mapping on `utils`
([#746](https://github.com/census-instrumentation/opencensus-python/pull/746))
- Updated `django`, `flask`, `httplib`, `requests` and `pyramid` modules
([#755](https://github.com/census-instrumentation/opencensus-python/pull/755))
- Updated `requests` module
([#771](https://github.com/census-instrumentation/opencensus-python/pull/771))

## 0.7.2
Released 2019-08-16

- Fix GCP resource loading for certain environments
([#761](https://github.com/census-instrumentation/opencensus-python/pull/761))

## 0.7.1
Released 2019-08-05

- Added `set_status` to `span`
([#738](https://github.com/census-instrumentation/opencensus-python/pull/738))
- Update released stackdriver exporter version

## 0.7.0
Released 2019-07-31

## 0.7.2
Released 2019-08-16

Expand Down
6 changes: 6 additions & 0 deletions contrib/opencensus-ext-azure/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

## 0.7.1
Released 2019-08-26

- Standard metrics incoming requests per second
([#758](https://github.com/census-instrumentation/opencensus-python/pull/758))

## 0.7.0
Released 2019-07-31

Expand Down
1 change: 1 addition & 0 deletions contrib/opencensus-ext-azure/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Below is a list of standard metrics that are currently available:

- Available Memory (bytes)
- CPU Processor Time (percentage)
- Incoming Request Rate (per second)
- Outgoing Request Rate (per second)
- Process CPU Usage (percentage)
- Process Private Bytes (bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = '0.7.0'
__version__ = '0.7.1'
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@
from opencensus.metrics.export.metric_producer import MetricProducer
from opencensus.ext.azure.metrics_exporter.standard_metrics.cpu \
import ProcessorTimeMetric
from opencensus.ext.azure.metrics_exporter.standard_metrics.dependency \
from opencensus.ext.azure.metrics_exporter.standard_metrics.http_dependency \
import DependencyRateMetric
from opencensus.ext.azure.metrics_exporter.standard_metrics.memory \
import AvailableMemoryMetric
from opencensus.ext.azure.metrics_exporter.standard_metrics.process \
import ProcessCPUMetric
from opencensus.ext.azure.metrics_exporter.standard_metrics.process \
import ProcessMemoryMetric
from opencensus.ext.azure.metrics_exporter.standard_metrics.http_requests \
import RequestsRateMetric

# List of standard metrics to track
STANDARD_METRICS = [AvailableMemoryMetric,
DependencyRateMetric,
ProcessCPUMetric,
ProcessMemoryMetric,
ProcessorTimeMetric]
ProcessorTimeMetric,
RequestsRateMetric]


def register_metrics():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __call__(self):
value over the elapsed time.
:rtype: :class:`opencensus.metrics.export.gauge.DerivedLongGauge`
:return: The gauge representing the available memory metric
:return: The gauge representing the outgoing requests metric
"""
gauge = DerivedDoubleGauge(
DependencyRateMetric.NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2019, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import time

from opencensus.metrics.export.gauge import DerivedDoubleGauge
if sys.version_info < (3,):
from BaseHTTPServer import HTTPServer
else:
from http.server import HTTPServer

requests_map = dict()
ORIGINAL_CONSTRUCTOR = HTTPServer.__init__


def request_patch(func):
def wrapper(self=None):
func(self)
count = requests_map.get('count', 0)
requests_map['count'] = count + 1
return wrapper


def server_patch(*args, **kwargs):
if len(args) >= 3:
handler = args[2]
if handler:
# Patch the handler methods if they exist
if "do_DELETE" in dir(handler):
handler.do_DELETE = request_patch(handler.do_DELETE)
if "do_GET" in dir(handler):
handler.do_GET = request_patch(handler.do_GET)
if "do_HEAD" in dir(handler):
handler.do_HEAD = request_patch(handler.do_HEAD)
if "do_OPTIONS" in dir(handler):
handler.do_OPTIONS = request_patch(handler.do_OPTIONS)
if "do_POST" in dir(handler):
handler.do_POST = request_patch(handler.do_POST)
if "do_PUT" in dir(handler):
handler.do_PUT = request_patch(handler.do_PUT)
result = ORIGINAL_CONSTRUCTOR(*args, **kwargs)
return result


def setup():
# Patch the HTTPServer handler to track request information
HTTPServer.__init__ = server_patch


class RequestsRateMetric(object):
NAME = "\\ASP.NET Applications(??APP_W3SVC_PROC??)\\Requests/Sec"

def __init__(self):
setup()

@staticmethod
def get_value():
current_count = requests_map.get('count', 0)
current_time = time.time()
last_count = requests_map.get('last_count', 0)
last_time = requests_map.get('last_time')
last_result = requests_map.get('last_result', 0)

try:
# last_time is None the very first time this function is called
if last_time is not None:
elapsed_seconds = current_time - last_time
interval_count = current_count - last_count
result = interval_count / elapsed_seconds
else:
result = 0
requests_map['last_time'] = current_time
requests_map['last_count'] = current_count
requests_map['last_result'] = result
return result
except ZeroDivisionError:
# If elapsed_seconds is 0, exporter call made too close to previous
# Return the previous result if this is the case
return last_result

def __call__(self):
""" Returns a derived gauge for incoming requests per second
Calculated by obtaining by getting the number of incoming requests
made to an HTTPServer within an elapsed time and dividing that value
over the elapsed time.
:rtype: :class:`opencensus.metrics.export.gauge.DerivedLongGauge`
:return: The gauge representing the incoming requests metric
"""
gauge = DerivedDoubleGauge(
RequestsRateMetric.NAME,
'Incoming Requests per second',
'rps',
[])
gauge.create_default_time_series(RequestsRateMetric.get_value)
return gauge
Loading

0 comments on commit f109f0a

Please sign in to comment.