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

org.glowroot.agent.it.harness.impl.ConfigServiceImpl Maven / Gradle / Ivy

There is a newer version: 0.14.0-beta.3
Show newest version
/*
 * Copyright 2015-2019 the original author or authors.
 *
 * 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.glowroot.agent.it.harness.impl;

import java.util.List;
import java.util.ListIterator;

import org.glowroot.agent.it.harness.shaded.com.google.common.collect.ImmutableList;
import org.glowroot.agent.it.harness.shaded.com.google.common.collect.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;

import org.glowroot.agent.it.harness.ConfigService;
import org.glowroot.agent.util.JavaVersion;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.AdvancedConfig;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.GaugeConfig;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.InstrumentationConfig;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.MBeanAttribute;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.PluginConfig;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.PluginProperty;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.PluginProperty.StringList;
import org.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig.TransactionConfig;
import org.glowroot.wire.api.model.Proto.OptionalInt32;

class ConfigServiceImpl implements ConfigService {

    private final GrpcServerWrapper server;
    private final boolean reweavable;

    ConfigServiceImpl(GrpcServerWrapper server, boolean reweavable) {
        this.server = server;
        this.reweavable = reweavable;
    }

    @Override
    public void updateTransactionConfig(TransactionConfig config) throws Exception {
        AgentConfig agentConfig = server.getAgentConfig();
        server.updateAgentConfig(agentConfig.toBuilder()
                .setTransactionConfig(config)
                .build());
    }

    @Override
    public void setPluginProperty(String pluginId, String propertyName, boolean propertyValue)
            throws Exception {
        updatePluginConfig(pluginId, propertyName,
                PluginProperty.Value.newBuilder().setBval(propertyValue).build());
    }

    @Override
    public void setPluginProperty(String pluginId, String propertyName,
            @Nullable Double propertyValue) throws Exception {
        if (propertyValue == null) {
            updatePluginConfig(pluginId, propertyName,
                    PluginProperty.Value.newBuilder().setDvalNull(true).build());
        } else {
            updatePluginConfig(pluginId, propertyName,
                    PluginProperty.Value.newBuilder().setDval(propertyValue).build());
        }
    }

    @Override
    public void setPluginProperty(String pluginId, String propertyName, String propertyValue)
            throws Exception {
        updatePluginConfig(pluginId, propertyName,
                PluginProperty.Value.newBuilder().setSval(propertyValue).build());
    }

    @Override
    public void setPluginProperty(String pluginId, String propertyName, List propertyValue)
            throws Exception {
        updatePluginConfig(pluginId, propertyName, PluginProperty.Value.newBuilder()
                .setLval(StringList.newBuilder().addAllVal(propertyValue)).build());
    }

    @Override
    public int updateInstrumentationConfigs(List configs) throws Exception {
        AgentConfig agentConfig = server.getAgentConfig();
        server.updateAgentConfig(agentConfig.toBuilder()
                .clearInstrumentationConfig()
                .addAllInstrumentationConfig(configs)
                .build());
        if (reweavable) {
            return server.reweave();
        } else {
            return 0;
        }
    }

    @Override
    public void updateAdvancedConfig(AdvancedConfig config) throws Exception {
        AgentConfig agentConfig = server.getAgentConfig();
        server.updateAgentConfig(agentConfig.toBuilder()
                .setAdvancedConfig(config)
                .build());
    }

    void initConfigForTests() throws Exception {
        AgentConfig agentConfig = server.getAgentConfig();
        server.updateAgentConfig(agentConfig.toBuilder()
                .setTransactionConfig(agentConfig.getTransactionConfig().toBuilder()
                        .setSlowThresholdMillis(of(0)))
                .build());
    }

    void resetConfigForTests() throws Exception {
        AgentConfig.Builder builder = AgentConfig.newBuilder()
                .setTransactionConfig(getDefaultTransactionConfigForTests())
                .setAdvancedConfig(getDefaultAdvancedConfigForTests())
                .addAllGaugeConfig(getDefaultGaugeConfigsForTests());
        for (PluginConfig pluginConfig : server.getAgentConfig().getPluginConfigList()) {
            PluginConfig.Builder pluginConfigBuilder = PluginConfig.newBuilder()
                    .setId(pluginConfig.getId());
            for (PluginProperty pluginProperty : pluginConfig.getPropertyList()) {
                pluginConfigBuilder.addProperty(pluginProperty.toBuilder()
                        .setValue(pluginProperty.getDefault()));
            }
            builder.addPluginConfig(pluginConfigBuilder.build());
        }
        server.updateAgentConfig(builder.build());
    }

    private void updatePluginConfig(String pluginId, String name, PluginProperty.Value value)
            throws Exception {
        PluginConfig pluginConfig = getPluginConfig(pluginId);
        List properties = Lists.newArrayList(pluginConfig.getPropertyList());
        ListIterator i = properties.listIterator();
        boolean found = false;
        while (i.hasNext()) {
            PluginProperty existingPluginProperty = i.next();
            if (existingPluginProperty.getName().equals(name)) {
                i.set(existingPluginProperty.toBuilder()
                        .setValue(value)
                        .build());
                found = true;
                break;
            }
        }
        if (!found) {
            throw new IllegalStateException("Could not find plugin property with name: " + name);
        }
        updatePluginConfig(pluginConfig.toBuilder()
                .clearProperty()
                .addAllProperty(properties)
                .build());
    }

    private PluginConfig getPluginConfig(String pluginId) throws InterruptedException {
        AgentConfig agentConfig = server.getAgentConfig();
        for (PluginConfig pluginConfig : agentConfig.getPluginConfigList()) {
            if (pluginConfig.getId().equals(pluginId)) {
                return pluginConfig;
            }
        }
        throw new IllegalStateException("Could not find plugin with id: " + pluginId);
    }

    private void updatePluginConfig(PluginConfig config) throws Exception {
        AgentConfig agentConfig = server.getAgentConfig();
        List pluginConfigs = Lists.newArrayList(agentConfig.getPluginConfigList());
        ListIterator i = pluginConfigs.listIterator();
        boolean found = false;
        while (i.hasNext()) {
            if (i.next().getId().equals(config.getId())) {
                i.set(config);
                found = true;
                break;
            }
        }
        if (!found) {
            throw new IllegalStateException("Could not find plugin with id: " + config.getId());
        }
        server.updateAgentConfig(agentConfig.toBuilder()
                .clearPluginConfig()
                .addAllPluginConfig(pluginConfigs)
                .build());
    }

    private static TransactionConfig getDefaultTransactionConfigForTests() {
        // TODO this needs to be kept in sync with default values
        return TransactionConfig.newBuilder()
                .setProfilingIntervalMillis(of(1000))
                .setSlowThresholdMillis(of(0)) // default for tests
                .setCaptureThreadStats(true)
                .build();
    }

    private static AdvancedConfig getDefaultAdvancedConfigForTests() {
        // TODO this needs to be kept in sync with default values
        return AdvancedConfig.newBuilder()
                .setWeavingTimer(false)
                .setImmediatePartialStoreThresholdSeconds(of(60))
                .setMaxTransactionAggregates(of(500))
                .setMaxQueryAggregates(of(500))
                .setMaxServiceCallAggregates(of(500))
                .setMaxTraceEntriesPerTransaction(of(2000))
                .setMaxProfileSamplesPerTransaction(of(50000))
                .setMbeanGaugeNotFoundDelaySeconds(of(60))
                .build();
    }

    private static List getDefaultGaugeConfigsForTests() {
        // TODO this needs to be kept in sync with default values
        List defaultGaugeConfigs = Lists.newArrayList();
        defaultGaugeConfigs.add(GaugeConfig.newBuilder()
                .setMbeanObjectName("java.lang:type=Memory")
                .addMbeanAttribute(MBeanAttribute.newBuilder()
                        .setName("HeapMemoryUsage.used"))
                .build());
        defaultGaugeConfigs.add(GaugeConfig.newBuilder()
                .setMbeanObjectName("java.lang:type=GarbageCollector,name=*")
                .addMbeanAttribute(MBeanAttribute.newBuilder()
                        .setName("CollectionCount")
                        .setCounter(true))
                .addMbeanAttribute(MBeanAttribute.newBuilder()
                        .setName("CollectionTime")
                        .setCounter(true))
                .build());
        defaultGaugeConfigs.add(GaugeConfig.newBuilder()
                .setMbeanObjectName("java.lang:type=MemoryPool,name=*")
                .addMbeanAttribute(MBeanAttribute.newBuilder()
                        .setName("Usage.used"))
                .build());
        GaugeConfig.Builder operatingSystemMBean = GaugeConfig.newBuilder()
                .setMbeanObjectName("java.lang:type=OperatingSystem")
                .addMbeanAttribute(MBeanAttribute.newBuilder()
                        .setName("FreePhysicalMemorySize"));
        if (!JavaVersion.isJava6()) {
            // these are only available since 1.7
            operatingSystemMBean.addMbeanAttribute(MBeanAttribute.newBuilder()
                    .setName("ProcessCpuLoad"));
            operatingSystemMBean.addMbeanAttribute(MBeanAttribute.newBuilder()
                    .setName("SystemCpuLoad"));
        }
        defaultGaugeConfigs.add(operatingSystemMBean.build());
        return ImmutableList.copyOf(defaultGaugeConfigs);
    }

    private static OptionalInt32 of(int value) {
        return OptionalInt32.newBuilder().setValue(value).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy