def generate_latest(registry=REGISTRY):
    """Returns the metrics from the registry in latest text format as a string."""

    def sample_line(line):
        if line.labels:
            labelstr = '{{{0}}}'.format(','.join(
                ['{0}="{1}"'.format(
                    k, v.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"'))
                    for k, v in sorted(line.labels.items())]))
        else:
            labelstr = ''
        timestamp = ''
        if line.timestamp is not None:
            # Convert to milliseconds.
            timestamp = ' {0:d}'.format(int(float(line.timestamp) * 1000))
        return '{0}{1} {2}{3}\n'.format(
            line.name, labelstr, floatToGoString(line.value), timestamp)

    output = []
    for metric in registry.collect():
        try:
            mname = metric.name
            mtype = metric.type
            # Munging from OpenMetrics into Prometheus format.
            if mtype == 'counter':
                mname = mname + '_total'
            elif mtype == 'info':
                mname = mname + '_info'
                mtype = 'gauge'
            elif mtype == 'stateset':
                mtype = 'gauge'
            elif mtype == 'gaugehistogram':
                # A gauge histogram is really a gauge,
                # but this captures the strucutre better.
                mtype = 'histogram'
            elif mtype == 'unknown':
                mtype = 'untyped'

            output.append('# HELP {0} {1}\n'.format(
                mname, metric.documentation.replace('\\', r'\\').replace('\n', r'\n')))
            output.append('# TYPE {0} {1}\n'.format(mname, mtype))

            om_samples = {}
            for s in metric.samples:
                for suffix in ['_created', '_gsum', '_gcount']:
                    if s.name == metric.name + suffix:
                        # OpenMetrics specific sample, put in a gauge at the end.
                        om_samples.setdefault(suffix, []).append(sample_line(s))
                        break
                else:
                    output.append(sample_line(s))
        except Exception as exception:
            exception.args = (exception.args or ('',)) + (metric,)
            raise

        for suffix, lines in sorted(om_samples.items()):
            output.append('# TYPE {0}{1} gauge\n'.format(metric.name, suffix))
            output.extend(lines)
    return ''.join(output).encode('utf-8')
def generate_latest ( registry = REGISTRY ) : def sample_line ( line ) : if line . labels : labelstr = '{{{0}}}' . format ( ',' . join ( [ '{0}="{1}"' . format ( k , v . replace ( '\\' , r'\\' ) . replace ( '\n' , r'\n' ) . replace ( '"' , r'\"' ) ) for k , v in sorted ( line . labels . items ( ) ) ] ) ) else : labelstr = '' timestamp = '' if line . timestamp is not None : timestamp = ' {0:d}' . format ( int ( float ( line . timestamp ) * 1000 ) ) return '{0}{1} {2}{3}\n' . format ( line . name , labelstr , floatToGoString ( line . value ) , timestamp ) output = [ ] for metric in registry . collect ( ) : try : mname = metric . name mtype = metric . type if mtype == 'counter' : mname = mname + '_total' elif mtype == 'info' : mname = mname + '_info' mtype = 'gauge' elif mtype == 'stateset' : mtype = 'gauge' elif mtype == 'gaugehistogram' : mtype = 'histogram' elif mtype == 'unknown' : mtype = 'untyped' output . append ( '# HELP {0} {1}\n' . format ( mname , metric . documentation . replace ( '\\' , r'\\' ) . replace ( '\n' , r'\n' ) ) ) output . append ( '# TYPE {0} {1}\n' . format ( mname , mtype ) ) om_samples = { } for s in metric . samples : for suffix in [ '_created' , '_gsum' , '_gcount' ] : if s . name == metric . name + suffix : om_samples . setdefault ( suffix , [ ] ) . append ( sample_line ( s ) ) break else : output . append ( sample_line ( s ) ) except Exception as exception : exception . args = ( exception . args or ( '' , ) ) + ( metric , ) raise for suffix , lines in sorted ( om_samples . items ( ) ) : output . append ( '# TYPE {0}{1} gauge\n' . format ( metric . name , suffix ) ) output . extend ( lines ) return '' . join ( output ) . encode ( 'utf-8' )
Returns the metrics from the registry in latest text format as a string.