zperfmetrics 1.0b5
Perfmetrics configuration for Zope/Plone.
License: Simplified BSD
Keywords: plone zope perfmetrics statsd graphite monitor performance

.. contents:: Table of Contents
   :depth: 2

Perfmetrics configuration for Zope and Plone
============================================

zperfmetrics works like and depends on `perfmetrics <https://pypi.python.org/pypi/perfmetrics>`_, additional it:

- offers a ZConfig configuration for the statsd,
- uses the current requests path in the statd dotted path prefix,
- also provides a patch to measure plone.app.theming for diazo compile and diazo transform if Plone is available,


Installation
============

First get your ``statsd`` configured properly.
This is out of scope of this module.

Depend on this module in your packages ``setup.py``.

Given you use ``zc.builodut`` to set up your Zope2 or Plone,
add to your ``[instance]`` section or the section with ``recipe = plone.recipe.zope2instance`` the lines::

    [instance01]
    recipe = plone.recipe.zope2instance
    ...
    zope-conf-imports =
        zperfmetrics
    zope-conf-additional =
        <perfmetrics>
            uri statsd://localhost:8125
            before MyFancyProject
            hostname on
            virtualhost on
            after ${:_buildout_section_name_}
        </perfmetrics>
    ...

Given this runs on a host called ``w-plone1``,
this will result in a prefix ``MyFancyProject.w-plone1.instance01``.

uri
    Full URI of statd.

before
    Prefix path before the hostname.

hostname
    Get hostname and insert into prefix.
    (Boolean: ``on`` or ``off``)

virtualhost
    Get virtualhost and use in ZPerfmetrics after the static prefix.
    (Boolean: ``on`` or ``off``)

after
    Prefix path after the hostname.


Usage
=====

You need to decorate your code or use the ``with`` statement to measure a code block.

Usage::

    from zperfmetrics import ZMetric
    from zperfmetrics import zmetric
    from zperfmetrics import zmetricmethod

    @zmetric
    def some_function():
        # whole functions timing and call counts will be measured
        pass

    @ZMetric(stat='a_custom_prefix')
    def some_other_function():
        # set a custom prefix instead of module path and fucntion name.
        pass

    class Foo(object):

        @zmetricmethod
        def some_method(self):
            # whole methods timing and call counts will be measured
            pass

        @ZMetric(method=True, timing=False):
        def some_counted_method(self):
            # whole methods number of calls will be measured, but no times
            pass

        @ZMetric(method=True, count=False):
        def some_timed_method(self):
            # whole methods timing be measured, but no counts
            pass

        def some_method_partly_measured(self):
            # do something here you dont want to measure
            with ZMetric(stat='part_of_other_method_time'):
                # do something to measure
                # call counts and timing will be measured
                pass
            # do something here you dont want to measure

        @ZMetric(method=True, rate=0.5):
        def some_random_recorded_method(self):
            # randomly record 50% of the calls.
            pass


Request Lifecycle Integration
=============================

All ZPerfmetrics with a request passed in are considered to be under the ``request_lifecycle`` section.

All metrics in here are build like: ``$PREFIX.request_lifecycle.[$VIRTUAL_HOST].$PATH.*``.

Zope
----

This package provides subscribers to measure the time a request takes,
including some points in time between.

These subscribers are loaded via zcml and are logging under ``publish.*``:

``publish.traversal``
    time needed from publication start until traversal is finished.

``publish.rendering``
    time needed from traversal end until before commit begin.

    This value is a bit fuzzy and should be taken with a grain of salt,
    because there can be other subscribers to this event which take their time.
    Since the order of execution of the subscribers is not defined,
    processing may happen after this measurement

    If plone tranformchain is active,
    the rendering time is before transforms are starting.

``publish.finalize``
    time needed from rendering end (or transform end if plone.transformchain is active) until database commit is done.

``publish_all``
    whole time needed from publication start until request is completly processed.

Plone
-----

Installing this package in Plone by depending on ``zperfmetrics[plone]`` forces usage of ``plone.transformchain`` version 1.2 or newer.

First, ``publish.rendering`` gets less fuzzy because the expensive transforms (also subscribers to publish.beforecommit) are all done.

Then it introduces new measurements related to ``plone.transformchain``:

``publish.transform_all``
    time needed for all transforms in the ``plone.transformchain``.
    This usually includes Diazo.

``publish_transform_single.${ORDER}-${TRANSFORMNAME}``
    time needed for a specific single transform.
    transforms are ordered and named, both are replaced.


This package patches:

``diazo.setup`` metric
    ``plone.app.theming.transform.ThemeTransform.setupTransform`` is patched as a basic (path-less) perfmetrics ``Metric``.
    The setup of the transform happens once on startup and is the time needed to create the Diazo xslt from its rules.xml, index.html and related files.

Statd, Graphite & Grafana in Docker
===================================

Setting up Statsd, Graphite and Grafana can be complex.
For local testing - but also for production environments - firing up some docker containers comes in handy.

A very minimal version of such a `Statd, Graphite & Grafana in Docker setup <https://github.com/collective/zperfmetrics/tree/master/docker>`_ (`original <https://github.com/Ennexa/docker-graphite>`_) helps getting things initially up and running.
`Install Docker <https://docs.docker.com/engine/installation/>`_ and `install docker-compose <https://docs.docker.com/compose/install/>`_ (just ``pip install docker-compose``),
then just clone the repository and in its ``docker`` directory run ``docker-compose up -d``.

Let Zperfmetrics point to ``uri statsd://localhost:8125`` and collect some data.
Open Grafana in your browser at ``http://localhost:3000``.

Go first to `Grafana Getting Started <http://docs.grafana.org/guides/gettingstarted/>`_,
the 10 minute video *really helps* to find the hidden part of its UI.

Source Code
===========

The sources are in a GIT DVCS with its main branches at `github <https://github.com/collective/zperfmetrics>`_.

We'd be happy to see many branches, forks and pull-requests to make zperfmetrics even better.

Contributors
============

- Jens W. Klein <jens@bluedynamics.com>

- Zalán Somogyváry

History
=======

1.0b5 (2016-08-05)
------------------

- Event better naming in order to be able to group and stack:
  Renamed ``publish.sum`` to ``publish_all`` and ``publish.transform_single`` to ``publish_transform_single``.
  [jensens]

1.0b4 (2016-08-05)
------------------

- Cleanup names, better grouping in order to make graphing easier.
  [jensens]

1.0b3 (2016-08-05)
------------------

- Enhancement: Introduce after-prefix a top level ``request_lifecycle`` to be consistent with levels.
  [jensens]


1.0b2 (2016-08-04)
------------------

- Fix: Name of a single transform may contain dots.
  They are now replaced by ``_``.
  [jensens]

- Fix: The root element of a path was not shown on the same level as all other paths.
  Now it is shown as ``__root__``
  [jensens]

- Feature: It was not possible to filter by virtual host in environment with more than one site per Zope.
  A new configuration option ``virtualhost`` was introduced.
  If set to on, the virtualhost name will be inserted before ``PATH``.
  [jensens]

- Support: In order to make a test installation easier,
  an example docker-compose setup was added to the Github repository together with some documentation in the README.rst.
  [jensens]

1.0b1 (2016-08-04)
------------------

- Added subscribers to ``plone.transformchain`` events.
  New setup extra ``[plone]``.
  Removed one patch for diazo transform.
  Made ``publish.beforecommit`` less fuzzy in Plone.
  [jensens]

1.0a7 (2016-08-03)
------------------

- Fix: virtual path config in ZMetric class.
  [syzn]


1.0a6 (2016-07-28)
------------------

- Feature: New value publish.commit.
  Delta time used from publish.beforecommit to request end.
  [jensens]

- Fix: publish.sum shows now overall time of request processing.
  [jensens]

- Fix: Update README to reflect last changes.
  [jensens]

- Use more beautiful paths if VirtualHostMonster is used.
  [syzn]


1.0a5 (2016-06-28)
------------------

- Fix: Before/after hooks were not assigned correctly.
  [jensens]


1.0a4 (2016-06-10)
------------------

- Fixes measurements of zope request, also add more detailed metrics.
  [jensens]


1.0a3 (2016-06-09)
------------------

- Measure time a zope request needs.
  [jensens]


1.0a2 (2016-03-22)
------------------

- Refactored: ZConfig best practice
  [jensens]

- Fix: Strip trailing dot from prefix
  [jensens]


1.0a1 (2016-03-22)
------------------

- Fix: README was wrong.
  [jensens]


1.0a0 (2016-03-22)
------------------

- made it work [jensens, 2016-03-17]


License
=======

Copyright (c) 2016, BlueDynamics Alliance, Austria, Germany, Switzerland
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this 
  list of conditions and the following disclaimer in the documentation and/or 
  other materials provided with the distribution.
* Neither the name of the BlueDynamics Alliance nor the names of its 
  contributors may be used to endorse or promote products derived from this 
  software without specific prior written permission.
      
THIS SOFTWARE IS PROVIDED BY BlueDynamics Alliance ``AS IS`` AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BlueDynamics Alliance BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.