001/*
002 * (C) Copyright 2006-2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     dmetzler
018 */
019package org.nuxeo.datadog.reporter;
020
021import java.net.URI;
022import java.net.URISyntaxException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.List;
027
028import org.apache.commons.lang3.StringUtils;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XObject;
031import org.nuxeo.runtime.api.Framework;
032
033@XObject("configuration")
034public class DatadogReporterConfDescriptor {
035
036    @XNode("apiKey")
037    String apiKey;
038
039    @XNode("pollInterval")
040    int pollInterval;
041
042    @XNode("host")
043    String host;
044
045    @XNode("tags")
046    String tags;
047
048    @XNode("filter")
049    FilterDescriptor filter = new FilterDescriptor();
050
051    public long getPollInterval() {
052        return pollInterval;
053    }
054
055    public String getApiKey() {
056        return apiKey;
057    }
058
059    public String getHost() {
060        if (StringUtils.isNotBlank(host)) {
061            return host;
062        } else {
063            return computeHostFromNuxeoUrl();
064        }
065    }
066
067    private String computeHostFromNuxeoUrl() {
068        try {
069            String url = Framework.getProperty("nuxeo.url");
070            if (StringUtils.isBlank(url)) {
071                return "";
072            }
073
074            URI uri = new URI(url);
075
076            String domain = uri.getHost();
077            if (StringUtils.isBlank(domain)) {
078                return "";
079            }
080
081            return domain.startsWith("www.") ? domain.substring(4) : domain;
082
083        } catch (URISyntaxException e) {
084            return "";
085        }
086    }
087
088    public List<String> getTags() {
089        if (StringUtils.isBlank(tags)) {
090            return Collections.emptyList();
091        } else {
092            List<String> result = new ArrayList<>();
093
094            for (String tag : Arrays.asList(tags.split(","))) {
095                result.add(tag.trim());
096            }
097            return result;
098        }
099    }
100}