cloudposse / prometheus-to-cloudwatch

Utility for scraping Prometheus metrics from a Prometheus client endpoint and publishing them to CloudWatch
https://cloudposse.com/accelerate
Apache License 2.0
169 stars 37 forks source link

feat: add ability to include dimensions per-metric #29

Closed austince closed 5 years ago

austince commented 5 years ago

Been using the exclude feature that we put in yesterday (#28) and liking it, but found it would be useful to have a whitelist option as well. Tried to match inclusion/exclusion logic of INCLUDE_METRICS / EXCLUDE_METRICS.

austince commented 5 years ago

Thanks for the quick review @aknysh. Sounds good, I'll make sure of that. Has there been any discussion around adding a testing framework?

austince commented 5 years ago
type StringSet map[string]bool

func set(slice []string) StringSet {
    boolMap := make(StringSet, len(slice))

    for i := range slice {
        boolMap[slice[i]] = true
    }

    return boolMap
}

func shouldIncludeDimension(dimName string, includeSet, excludeSet StringSet) bool {
    // blacklist first
    if excludeSet != nil && excludeSet[dimName] {
        return false
    }
    // if no whitelist, keep it as it passed the blacklist
    if includeSet == nil {
        return true
    }
    // otherwise, check the whitelist
    return includeSet[dimName]
}

func main() {
    println(shouldIncludeDimension("dim1", nil, set([]string{"dim1"})))                   // false, in exclude set
    println(shouldIncludeDimension("dim2", set([]string{"dim1"}), nil))                   // false, not in include set
    println(shouldIncludeDimension("dim3", set([]string{"dim1"}), set([]string{"dim2"}))) // false, not in include set yet also not in exclude set
    println(shouldIncludeDimension("dim1", nil, nil))                                     // true, no include or exclude set specified
    println(shouldIncludeDimension("dim1", nil, set([]string{"dim2"})))                   // true, no include set specified and not in exclude set
    println(shouldIncludeDimension("dim1", set([]string{"dim1"}), set([]string{"dim2"}))) // true, in the include set, not in exclude set
    println(shouldIncludeDimension("dim1", set([]string{"dim1"}), set([]string{"dim1"}))) // false, in the include set, but also in exclude set
}

I think we should be good - here's a runnable example of the current logic.

austince commented 5 years ago

Thanks @aknysh - I'm not sure if I'll have time soon but might in a few weeks.