import re


def _get_custom_metric_name(name):
    """Make a metric suitable for sending to Cloud Monitoring's API.

    Metric names must not exceed 100 characters.

    For now we limit to alphanumeric plus dot and underscore. Invalid
    characters are converted to underscores.
    """
    # Don't guess at automatic truncation. Let the caller decide.
    prefix = 'custom.googleapis.com/'
    maxlen = 100 - len(prefix)
    if len(name) > maxlen:
        raise ValueError('Metric name too long: %d (limit %d): %s'
                         % (len(name), maxlen, name))
    return ('%s%s' % (prefix, re.sub(r'[^\w_.]', '_', name)))