'Contextmanager which acts as a timer for the metric ``metric_name``,
but which also yields a :class:`Tagger` object that allows the timed block
of code to add tags and quantity measurements. Tags are added verbatim to the
timer output. Measurements are recorded as histogram measurements in their own,
and also as bucketed tags on the timer measurement.
Arguments:
metric_name: The name used to aggregate all of these metrics.
course_context: The course which the query is being made for.'

@contextmanager
def timer(self, metric_name, course_context):

    tagger = Tagger(self._sample_rate)
    metric_name = '{}.{}'.format(self._metric_base, metric_name)
    start = time()
    try:
        (yield tagger)
    finally:
        end = time()
        tags = tagger.tags
        tags.append('course:{}'.format(course_context))
        for (name, size) in tagger.measures:
            dog_stats_api.histogram('{}.{}'.format(metric_name, name), size, timestamp=end, tags=[tag for tag in tags if (not tag.startswith('{}:'.format(metric_name)))], sample_rate=tagger.sample_rate)
        dog_stats_api.histogram('{}.duration'.format(metric_name), (end - start), timestamp=end, tags=tags, sample_rate=tagger.sample_rate)
        dog_stats_api.increment(metric_name, timestamp=end, tags=tags, sample_rate=tagger.sample_rate)
