All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scouter.server.ScouterTgMtConfig Maven / Gradle / Ivy

There is a newer version: 2.20.0
Show newest version
/*
 *  Copyright 2015 the original author or authors.
 *  @https://github.com/scouter-project/scouter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package scouter.server;

import scouter.server.http.model.CounterProtocol;
import scouter.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gun Lee ([email protected]) on 2018. 7. 22.
 */
public class ScouterTgMtConfig {
    public static final String X = "X$";

    private String measurement;

    private boolean enabled = true;
    private boolean debugEnabled = false;

    private Map counterMapping = new HashMap();

    private String objFamilyBase = X;
    private List objFamilyAppendTags = new ArrayList();

    private String objTypeBase = X;
    private List objTypePrependTags = new ArrayList();
    private List objTypeAppendTags = new ArrayList();
    private String objTypeIcon = "";

    private String objNameBase = X;
    private List objNameAppendTags = new ArrayList();

    private String hostTag = "host";
    private Map hostMapping = new HashMap();
    private Map> tagFilter = new HashMap>();

    public ScouterTgMtConfig(String measurement) {
        this.measurement = measurement;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isDebugEnabled() {
        return debugEnabled;
    }

    public void setDebugEnabled(boolean debugEnabled) {
        this.debugEnabled = debugEnabled;
    }

    public Map getCounterMapping() {
        return counterMapping;
    }

    public void setCounterMapping(Map counterMapping) {
        this.counterMapping = counterMapping;
    }

    public String getObjFamilyBase() {
        return objFamilyBase;
    }

    public void setObjFamilyBase(String objFamilyBase) {
        this.objFamilyBase = X + objFamilyBase;
    }

    public List getObjFamilyAppendTags() {
        return objFamilyAppendTags;
    }

    public void setObjFamilyAppendTags(List objFamilyAppendTags) {
        this.objFamilyAppendTags = objFamilyAppendTags;
    }

    public String getObjTypeBase() {
        return objTypeBase;
    }

    public void setObjTypeBase(String objTypeBase) {
        this.objTypeBase = objTypeBase;
    }

    public List getObjTypePrependTags() {
        return objTypePrependTags;
    }

    public void setObjTypePrependTags(List objTypePrependTags) {
        this.objTypePrependTags = objTypePrependTags;
    }

    public List getObjTypeAppendTags() {
        return objTypeAppendTags;
    }

    public void setObjTypeAppendTags(List objTypeAppendTags) {
        this.objTypeAppendTags = objTypeAppendTags;
    }

    public String getObjTypeIcon() {
        return objTypeIcon;
    }

    public void setObjTypeIcon(String objTypeIcon) {
        this.objTypeIcon = objTypeIcon;
    }

    public String getObjNameBase() {
        return objNameBase;
    }

    public void setObjNameBase(String objNameBase) {
        this.objNameBase = X + objNameBase;
    }

    public List getObjNameAppendTags() {
        return objNameAppendTags;
    }

    public void setObjNameAppendTags(List objNameAppendTags) {
        this.objNameAppendTags = objNameAppendTags;
    }

    public String getHostTag() {
        return hostTag;
    }

    public void setHostTag(String hostTag) {
        this.hostTag = hostTag;
    }

    public Map getHostMapping() {
        return hostMapping;
    }

    public void setHostMapping(Map hostMapping) {
        this.hostMapping = hostMapping;
    }

    public static String getPrefix() {
        return X;
    }

    public Map> getTagFilter() {
        return tagFilter;
    }

    public void setTagFilter(Map> tagFilter) {
        this.tagFilter = tagFilter;
    }

    public String toFamily(Map tags) {
        StringBuilder objFamilySb = new StringBuilder(objFamilyBase);
        for (String tagKey : objFamilyAppendTags) {
            objFamilySb.append('_').append(tags.get(tagKey));
        }
        return objFamilySb.toString();
    }

    public String toObjType(Map tags) {
        StringBuilder objTypeSb = new StringBuilder();

        for (String tagKey : objTypePrependTags) {
            String prependValue = tags.get(tagKey);
            if (StringUtil.isNotEmpty(prependValue)) {
                objTypeSb.append(prependValue).append('_');
            }
        }

        objTypeSb.append(objTypeBase);

        for (String tagKey : objTypeAppendTags) {
            objTypeSb.append('_').append(tags.get(tagKey));
        }
        return objTypeSb.toString();
    }

    public String toHost(Map tags) {
        String host = tags.get(hostTag);
        if (host == null) {
            host = "unknown";
        } else {
            String mappedHost = hostMapping.get(host);
            if (mappedHost != null) {
                host = mappedHost;
            }
        }
        return host;
    }

    public String toObjName(String host, Map tags) {
        StringBuilder objNameSb = new StringBuilder(40).append('/').append(host).append('/').append(objNameBase);
        for (String tagKey : objNameAppendTags) {
            objNameSb.append('_').append(tags.get(tagKey));
        }
        return objNameSb.toString();
    }

    public CounterProtocol getCounterProtocol(String counterName) {
        return counterMapping.get(counterName);
    }
    public boolean isTagFilterMatching(Map tags) {
        if (tagFilter == null || tagFilter.size() == 0) {
            return true;
        }

        boolean matching = false;
        for (Map.Entry> e : tagFilter.entrySet()) {
            List matchStrList = e.getValue();
            for (String matchStr : matchStrList) {
                boolean not = false;
                if (matchStr.charAt(0) == '!') {
                    not = true;
                    matchStr = matchStr.substring(1);
                }
                String value = tags.get(e.getKey());
                if (not) {
                    if (!matchStr.equals(value)) {
                        matching = true;
                        break;
                    }
                } else {
                    if (matchStr.equals(value)) {
                        matching = true;
                        break;
                    }
                }
            }
            if (matching) {
                break;
            }
        }
        return matching;
    }

    public boolean isValidConfig() {
        if (StringUtil.isEmpty(objFamilyBase) || objFamilyBase.equals(X)) {
            return false;
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy