Skip to content

Commit

Permalink
Merge pull request #314 from opendatacube/cleanup-opencensus
Browse files Browse the repository at this point in the history
Deepclean opencensus
  • Loading branch information
pindge authored Jul 1, 2020
2 parents c4f44c2 + eaefda3 commit 2f631a3
Show file tree
Hide file tree
Showing 11 changed files with 4 additions and 146 deletions.
16 changes: 0 additions & 16 deletions datacube_ows/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@

import logging

from datacube_ows.utils import get_opencensus_tracer, opencensus_trace_call

_LOG = logging.getLogger(__name__)

tracer = get_opencensus_tracer()

class DataStacker(object):
@log_call
Expand Down Expand Up @@ -65,7 +62,6 @@ def needed_bands(self):
return self._needed_bands

@log_call
@opencensus_trace_call(tracer=tracer)
def datasets(self, index, mask=False, all_time=False, point=None):
# Return datasets as a time-grouped xarray DataArray. (or None if no datasets)
# No PQ product, so no PQ datasets.
Expand Down Expand Up @@ -113,7 +109,6 @@ def _dataset_query(self, index, prod_name, query_args):
return datasets

@log_call
@opencensus_trace_call(tracer=tracer)
def data(self, datasets, mask=False, manual_merge=False, skip_corrections=False, **kwargs):
# pylint: disable=too-many-locals, consider-using-enumerate
# datasets is an XArray DataArray of datasets grouped by time.
Expand Down Expand Up @@ -147,7 +142,6 @@ def data(self, datasets, mask=False, manual_merge=False, skip_corrections=False,
return data

@log_call
@opencensus_trace_call(tracer=tracer)
def manual_data_stack(self, datasets, measurements, mask, skip_corrections, **kwargs):
# pylint: disable=too-many-locals, too-many-branches
# REFACTOR: TODO
Expand Down Expand Up @@ -191,7 +185,6 @@ def manual_data_stack(self, datasets, measurements, mask, skip_corrections, **kw

# Read data for given datasets and measurements per the output_geobox
@log_call
@opencensus_trace_call(tracer=tracer)
def read_data(self, datasets, measurements, geobox, resampling=Resampling.nearest, **kwargs):
return datacube.Datacube.load_data(
datasets,
Expand All @@ -201,7 +194,6 @@ def read_data(self, datasets, measurements, geobox, resampling=Resampling.neares

# Read data for single datasets and measurements per the output_geobox
@log_call
@opencensus_trace_call(tracer=tracer)
def read_data_for_single_dataset(self, dataset, measurements, geobox, resampling=Resampling.nearest, **kwargs):
datasets = [dataset]
if self._product.is_raw_time_res:
Expand All @@ -226,7 +218,6 @@ def bbox_to_geom(bbox, crs):


@log_call
@opencensus_trace_call(tracer=tracer)
def get_map(args):
# pylint: disable=too-many-nested-blocks, too-many-branches, too-many-statements, too-many-locals
# Parse GET parameters
Expand Down Expand Up @@ -329,7 +320,6 @@ def get_map(args):


@log_call
@opencensus_trace_call(tracer=tracer)
def _write_png(data, pq_data, style, extent_mask, geobox):
img_data = style.transform_data(data, pq_data, extent_mask)
width = geobox.width
Expand All @@ -351,7 +341,6 @@ def _write_png(data, pq_data, style, extent_mask, geobox):


@log_call
@opencensus_trace_call(tracer=tracer)
def _write_empty(geobox):
with MemoryFile() as memfile:
with memfile.open(driver='PNG',
Expand All @@ -366,7 +355,6 @@ def _write_empty(geobox):


@log_call
@opencensus_trace_call(tracer=tracer)
def _write_polygon(geobox, polygon, zoom_fill):
geobox_ext = geobox.extent
if geobox_ext.within(polygon):
Expand Down Expand Up @@ -399,7 +387,6 @@ def _write_polygon(geobox, polygon, zoom_fill):


@log_call
@opencensus_trace_call(tracer=tracer)
def get_s3_browser_uris(datasets, pt=None, s3url="", s3bucket=""):
uris = []
for tds in datasets:
Expand Down Expand Up @@ -436,7 +423,6 @@ def convert(uri):


@log_call
@opencensus_trace_call(tracer=tracer)
def _make_band_dict(prod_cfg, pixel_dataset, band_list):
band_dict = {}
for band in band_list:
Expand Down Expand Up @@ -466,7 +452,6 @@ def _make_band_dict(prod_cfg, pixel_dataset, band_list):


@log_call
@opencensus_trace_call(tracer=tracer)
def _make_derived_band_dict(pixel_dataset, style_index):
"""Creates a dict of values for bands derived by styles.
This only works for styles with an `index_function` defined.
Expand All @@ -493,7 +478,6 @@ def geobox_is_point(geobox):
return geobox.height == 1 and geobox.width == 1

@log_call
@opencensus_trace_call(tracer=tracer)
def feature_info(args):
# pylint: disable=too-many-nested-blocks, too-many-branches, too-many-statements, too-many-locals
# Parse GET parameters
Expand Down
12 changes: 0 additions & 12 deletions datacube_ows/ogc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from datacube_ows.wcs2 import handle_wcs2
from datacube_ows.wmts import handle_wmts
from datacube_ows.ogc_exceptions import OGCException, WCS1Exception, WCS2Exception, WMSException, WMTSException
from datacube_ows.utils import opencensus_trace_call, get_jaeger_exporter, get_opencensus_tracer, opencensus_tracing_enabled
from datacube_ows.cube_pool import cube
from datacube.utils.rio import set_default_rio_config
from datacube_ows.ows_configuration import get_config
Expand Down Expand Up @@ -50,16 +49,6 @@
app = Flask(__name__.split('.')[0])
RequestID(app)

tracer = None
if opencensus_tracing_enabled():
from opencensus.trace import config_integration
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
tracer = get_opencensus_tracer()
integration = ['sqlalchemy']
config_integration.trace_integrations(integration, tracer=tracer)
jaegerExporter = get_jaeger_exporter()
middleware = FlaskMiddleware(app, exporter=jaegerExporter)
_LOG.info("Opencensus tracing enabled")

# If invoked using Gunicorn, link our root logger to the gunicorn logger
# this will mean the root logs will be captured and managed by the gunicorn logger
Expand Down Expand Up @@ -194,7 +183,6 @@ def ogc_impl():
return ogc_e.exception_response(traceback=traceback.extract_tb(tb))


@opencensus_trace_call(tracer=tracer)
def ogc_svc_impl(svc):
svc_support = OWS_SUPPORTED[svc]
nocase_args = lower_get_args()
Expand Down
74 changes: 0 additions & 74 deletions datacube_ows/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,6 @@ def timing_wrapper(*args, **kwargs):
return result
return timing_wrapper

def opencensus_tracing_enabled():
return os.getenv("OPENCENSUS_TRACING_ENABLED", "false").lower() == "true"


def opencensus_trace_call(tracer=None, name=""):
def decorator(func):
@wraps(func)
def opencensus_wrapper(*args, **kwargs):
span_name = name if name else func.__name__
with tracer.span(name=span_name):
tracer.add_attribute_to_current_span(
"{}.args".format(span_name),
str(args))
tracer.add_attribute_to_current_span(
"{}.kwargs".format(span_name),
str(kwargs))
return func(*args, **kwargs)
if tracer is None:
return func
else:
return opencensus_wrapper

return decorator

def get_jaeger_exporter():
from opencensus.ext.jaeger import trace_exporter

if not opencensus_tracing_enabled():
return None

opts = {
"service_name": os.getenv("JAEGER_SERVICE_NAME", "Open Web Services")
}

hostname = os.getenv("JAEGER_HOSTNAME")
if hostname is not None:
# opts["host_name"] = hostname
opts["agent_host_name"] = hostname

port = os.getenv("JAEGER_PORT")
if port is not None:
port = int(port)
# opts["port"] = port
opts["agent_port"] = port

endpoint = os.getenv("JAEGER_ENDPOINT")
if endpoint is not None:
# opts["endpoint"] = endpoint
opts["agent_endpoint"] = endpoint

return trace_exporter(**opts)

def get_opencensus_sampler():
from opencensus.trace.samplers import probability, always_on

sample_rate = int(os.getenv("OPENCENSUS_SAMPLE_RATE", "100"))

if not sample_rate < 100 and not sample_rate > 0:
return always_on.AlwaysOnSampler
else:
return probability.ProbabilitySampler(rate=(sample_rate / 100.0))


def get_opencensus_tracer():
if not opencensus_tracing_enabled():
return None

from opencensus.trace import tracer as tracer_module
jaegerExporter = get_jaeger_exporter()
sampler = get_opencensus_sampler()
tracer = tracer_module.Tracer(exporter=jaegerExporter, sampler=sampler)

return tracer


def group_by_statistical():
from datacube.api.query import GroupBy
Expand Down
8 changes: 1 addition & 7 deletions datacube_ows/wcs1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

from datacube_ows.ows_configuration import get_config

from datacube_ows.utils import log_call, opencensus_trace_call, get_opencensus_tracer
from datacube_ows.utils import log_call


WCS_REQUESTS = ("DESCRIBECOVERAGE", "GETCOVERAGE")

tracer = get_opencensus_tracer()

@log_call
@opencensus_trace_call(tracer=tracer)
def handle_wcs1(nocase_args):
operation = nocase_args.get("request", "").upper()
if not operation:
Expand All @@ -33,7 +30,6 @@ def handle_wcs1(nocase_args):


@log_call
@opencensus_trace_call(tracer=tracer)
def get_capabilities(args):
# TODO: Handle updatesequence request parameter for cache consistency.
section = args.get("section")
Expand Down Expand Up @@ -76,7 +72,6 @@ def get_capabilities(args):


@log_call
@opencensus_trace_call(tracer=tracer)
def desc_coverages(args):
# Extract layer metadata from Datacube.
cfg = get_config()
Expand Down Expand Up @@ -111,7 +106,6 @@ def desc_coverages(args):


@log_call
@opencensus_trace_call(tracer=tracer)
def get_coverage(args):
cfg = get_config()
req = WCS1GetCoverageRequest(args)
Expand Down
7 changes: 0 additions & 7 deletions datacube_ows/wcs1_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
from datacube_ows.ogc_exceptions import WCS1Exception
from datacube_ows.ogc_utils import ProductLayerException
from datacube_ows.ows_configuration import get_config
from datacube_ows.utils import opencensus_trace_call, get_opencensus_tracer

tracer = get_opencensus_tracer()

class WCS1GetCoverageRequest():
version = Version(1,0,0)
#pylint: disable=too-many-instance-attributes, too-many-branches, too-many-statements, too-many-locals
@opencensus_trace_call(tracer=tracer)
def __init__(self, args):
self.args = args
cfg = get_config()
Expand Down Expand Up @@ -274,7 +270,6 @@ def __init__(self, args):
self.affine = trans_aff * scale_aff
self.geobox = geometry.GeoBox(self.width, self.height, self.affine, self.request_crs)

@opencensus_trace_call(tracer=tracer)
def get_coverage_data(req):
#pylint: disable=too-many-locals, protected-access
with cube() as dc:
Expand Down Expand Up @@ -343,7 +338,6 @@ def get_coverage_data(req):
return output


@opencensus_trace_call(tracer=tracer)
def get_tiff(req, data):
"""Uses rasterio MemoryFiles in order to return a streamable GeoTiff response"""
# Copied from CEOS. Does not seem to support multi-time dimension data - is this even possible in GeoTiff?
Expand Down Expand Up @@ -395,7 +389,6 @@ def get_tiff(req, data):
return memfile.read()


@opencensus_trace_call(tracer=tracer)
def get_netcdf(req, data):
# Cleanup dataset attributes for NetCDF export
data.attrs["crs"] = req.response_crsid # geometry.CRS(response_crs)
Expand Down
7 changes: 1 addition & 6 deletions datacube_ows/wcs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@

from datacube_ows.ows_configuration import get_config

from datacube_ows.utils import log_call, opencensus_trace_call, get_opencensus_tracer
from datacube_ows.utils import log_call


WCS_REQUESTS = ("DESCRIBECOVERAGE", "GETCOVERAGE")

tracer = get_opencensus_tracer()

@log_call
@opencensus_trace_call(tracer=tracer)
def handle_wcs2(nocase_args):
operation = request.args.get("request", "").upper()
if not operation:
Expand All @@ -48,7 +46,6 @@ def handle_wcs2(nocase_args):


@log_call
@opencensus_trace_call(tracer=tracer)
def get_capabilities(args):
# Extract layer metadata from Datacube.
cfg = get_config()
Expand Down Expand Up @@ -230,7 +227,6 @@ def create_coverage_description(cfg, product):
)

@log_call
@opencensus_trace_call(tracer=tracer)
def desc_coverages(args):
cfg = get_config()

Expand Down Expand Up @@ -277,7 +273,6 @@ def desc_coverages(args):
_LOG = logging.getLogger(__name__)

@log_call
@opencensus_trace_call(tracer=tracer)
def get_coverage(args):
request_obj = kvp_decode_get_coverage(args)

Expand Down
7 changes: 0 additions & 7 deletions datacube_ows/wcs2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
from datacube_ows.ogc_exceptions import WCS2Exception
from datacube_ows.ogc_utils import ProductLayerException
from datacube_ows.ows_configuration import get_config
from datacube_ows.utils import opencensus_trace_call, get_opencensus_tracer
from datacube_ows.wcs_scaler import WCSScaler, WCSScalerUnknownDimension

_LOG = logging.getLogger(__name__)

tracer = get_opencensus_tracer()


def uniform_crs(crs):
" Helper function to transform a URL style EPSG definition to an 'EPSG:nnn' one "
Expand All @@ -47,7 +44,6 @@ def uniform_crs(crs):
return crs


@opencensus_trace_call(tracer=tracer)
def get_coverage_data(request):
#pylint: disable=too-many-locals, protected-access

Expand Down Expand Up @@ -273,7 +269,6 @@ def get_coverage_data(request):
return output, fmt.mime, filename


@opencensus_trace_call(tracer=tracer)
def get_tiff(request, data, crs, product, width, height, affine):
"""Uses rasterio MemoryFiles in order to return a streamable GeoTiff response"""
# Copied from CEOS. Does not seem to support multi-time dimension data - is this even possible in GeoTiff?
Expand Down Expand Up @@ -339,8 +334,6 @@ def get_tiff(request, data, crs, product, width, height, affine):
dst.update_tags(idx, STATISTICS_STDDEV=data[band].values.std())
return memfile.read()


@opencensus_trace_call(tracer=tracer)
def get_netcdf(request, data, crs):
# Cleanup dataset attributes for NetCDF export
data.attrs["crs"] = crs # geometry.CRS(response_crs)
Expand Down
Loading

0 comments on commit 2f631a3

Please sign in to comment.