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

org.graylog.plugins.usagestatistics.UsageStatsNodePeriodical Maven / Gradle / Ivy

There is a newer version: 2.2.0-alpha.1
Show newest version
/**
 * Copyright (C) 2015 Graylog, Inc. ([email protected])
 *
 * 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 org.graylog.plugins.usagestatistics;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.EvictingQueue;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import org.graylog.plugins.usagestatistics.providers.CompressingHttpClient;
import org.graylog.plugins.usagestatistics.providers.SmileObjectMapper;
import org.graylog2.plugin.ServerStatus;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.cluster.ClusterId;
import org.graylog2.plugin.system.NodeId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.net.URL;

@Singleton
public class UsageStatsNodePeriodical extends UsageStatsPeriodical {
    private static final Logger LOG = LoggerFactory.getLogger(UsageStatsNodePeriodical.class);

    private final ServerStatus serverStatus;
    private final NodeId nodeId;
    private final UsageStatsNodeService usageStatsNodeService;

    @Inject
    public UsageStatsNodePeriodical(UsageStatsNodeService usageStatsNodeService,
                                    NodeId nodeId,
                                    ServerStatus serverStatus,
                                    UsageStatsConfiguration config,
                                    ClusterConfigService clusterConfigService,
                                    @CompressingHttpClient OkHttpClient httpClient,
                                    @SmileObjectMapper ObjectMapper objectMapper) {
        this(
                usageStatsNodeService,
                nodeId,
                serverStatus,
                config,
                clusterConfigService,
                EvictingQueue.create(config.getMaxQueueSize()),
                httpClient,
                objectMapper);
    }

    private UsageStatsNodePeriodical(UsageStatsNodeService usageStatsNodeService,
                                     NodeId nodeId,
                                     ServerStatus serverStatus,
                                     UsageStatsConfiguration config,
                                     ClusterConfigService clusterConfigService,
                                     EvictingQueue evictingQueue,
                                     OkHttpClient httpClient,
                                     ObjectMapper objectMapper) {
        super(config, clusterConfigService, evictingQueue, httpClient, objectMapper,
                "node-" + nodeId.anonymize() + "-%s.smile");
        this.serverStatus = serverStatus;
        this.nodeId = nodeId;
        this.usageStatsNodeService = usageStatsNodeService;
    }

    protected URL getUrl() {
        final ClusterId clusterId = clusterConfigService.get(ClusterId.class);
        if(clusterId != null) {
            return HttpUrl.get(config.getUrl()).newBuilder()
                    .addPathSegment("cluster")
                    .addPathSegment(clusterId.clusterId())
                    .addPathSegment("node")
                    .addPathSegment(nodeId.anonymize())
                    .build()
                    .url();
        } else {
            return null;
        }
    }

    @Override
    protected byte[] buildPayload() {
        try {
            return objectMapper.writeValueAsBytes(usageStatsNodeService.buildNodeDataSet());
        } catch (JsonProcessingException e) {
            LOG.error("Error while serializing usage statistics data", e);
            return null;
        }
    }

    @Override
    public boolean startOnThisNode() {
        return config.isEnabled() && !serverStatus.hasCapability(ServerStatus.Capability.LOCALMODE);
    }

    @Override
    protected Logger getLogger() {
        return LOG;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy