
org.glowroot.agent.server.ServerCollectorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glowroot-agent-it-harness Show documentation
Show all versions of glowroot-agent-it-harness Show documentation
Glowroot Agent Integration Test Harness
/*
* Copyright 2015-2016 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.server;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import javax.annotation.Nullable;
import org.glowroot.agent.shaded.google.common.base.Strings;
import org.glowroot.agent.shaded.grpc.stub.StreamObserver;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;
import org.glowroot.agent.server.ServerConnection.GrpcCall;
import org.glowroot.agent.shaded.glowroot.common.live.LiveJvmService;
import org.glowroot.agent.shaded.glowroot.common.live.LiveTraceRepository;
import org.glowroot.agent.shaded.glowroot.common.live.LiveWeavingService;
import org.glowroot.agent.shaded.glowroot.common.util.OnlyUsedByTests;
import org.glowroot.agent.shaded.glowroot.wire.api.Collector;
import org.glowroot.agent.shaded.glowroot.wire.api.model.AgentConfigOuterClass.AgentConfig;
import org.glowroot.agent.shaded.glowroot.wire.api.model.AggregateOuterClass.AggregatesByType;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceGrpc;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceGrpc.CollectorServiceStub;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.AggregateMessage;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.EmptyMessage;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.GaugeValue;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.GaugeValueMessage;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.InitMessage;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.InitResponse;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.LogEvent;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.LogMessage;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.SystemInfo;
import org.glowroot.agent.shaded.glowroot.wire.api.model.CollectorServiceOuterClass.TraceMessage;
import org.glowroot.agent.shaded.glowroot.wire.api.model.TraceOuterClass.Trace;
import static org.glowroot.agent.shaded.google.common.base.Preconditions.checkNotNull;
public class ServerCollectorImpl implements Collector {
static final Logger logger = LoggerFactory.getLogger(ServerCollectorImpl.class);
private final String agentId;
private final ServerConnection serverConnection;
private final CollectorServiceStub collectorServiceStub;
private final DownstreamServiceObserver downstreamServiceObserver;
public ServerCollectorImpl(Map properties, @Nullable String collectorHost,
LiveJvmService liveJvmService, LiveWeavingService liveWeavingService,
LiveTraceRepository liveTraceRepository, ScheduledExecutorService scheduledExecutor,
AgentConfigUpdater agentConfigUpdater) throws Exception {
String agentId = properties.get("glowroot.agent.id");
if (Strings.isNullOrEmpty(agentId)) {
agentId = InetAddress.getLocalHost().getHostName();
}
String collectorPortStr = properties.get("glowroot.collector.port");
if (Strings.isNullOrEmpty(collectorPortStr)) {
collectorPortStr = System.getProperty("glowroot.collector.port");
}
int collectorPort;
if (Strings.isNullOrEmpty(collectorPortStr)) {
collectorPort = 80;
} else {
collectorPort = Integer.parseInt(collectorPortStr);
}
checkNotNull(collectorHost);
this.agentId = agentId;
serverConnection = new ServerConnection(collectorHost, collectorPort, scheduledExecutor);
collectorServiceStub = CollectorServiceGrpc.newStub(serverConnection.getChannel());
downstreamServiceObserver = new DownstreamServiceObserver(serverConnection,
agentConfigUpdater, liveJvmService, liveWeavingService, liveTraceRepository,
agentId);
downstreamServiceObserver.connectAsync();
}
@Override
public void init(File glowrootBaseDir, SystemInfo systemInfo, AgentConfig agentConfig,
final AgentConfigUpdater agentConfigUpdater) {
final InitMessage initMessage = InitMessage.newBuilder()
.setAgentId(agentId)
.setSystemInfo(systemInfo)
.setAgentConfig(agentConfig)
.build();
serverConnection.callUntilSuccessful(new GrpcCall() {
@Override
public void call(StreamObserver responseObserver) {
collectorServiceStub.collectInit(initMessage, responseObserver);
}
@Override
void doWithResponse(InitResponse response) {
if (response.hasAgentConfig()) {
try {
agentConfigUpdater.update(response.getAgentConfig());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
});
}
@Override
public void collectAggregates(long captureTime, List aggregatesByType) {
final AggregateMessage aggregateMessage = AggregateMessage.newBuilder()
.setAgentId(agentId)
.setCaptureTime(captureTime)
.addAllAggregatesByType(aggregatesByType)
.build();
serverConnection.callWithAFewRetries(new GrpcCall() {
@Override
public void call(StreamObserver responseObserver) {
collectorServiceStub.collectAggregates(aggregateMessage, responseObserver);
}
});
}
@Override
public void collectGaugeValues(List gaugeValues) {
final GaugeValueMessage gaugeValueMessage = GaugeValueMessage.newBuilder()
.setAgentId(agentId)
.addAllGaugeValues(gaugeValues)
.build();
serverConnection.callWithAFewRetries(new GrpcCall() {
@Override
public void call(StreamObserver responseObserver) {
collectorServiceStub.collectGaugeValues(gaugeValueMessage, responseObserver);
}
});
}
@Override
public void collectTrace(Trace trace) {
final TraceMessage traceMessage = TraceMessage.newBuilder()
.setAgentId(agentId)
.setTrace(trace)
.build();
serverConnection.callWithAFewRetries(new GrpcCall() {
@Override
public void call(StreamObserver responseObserver) {
collectorServiceStub.collectTrace(traceMessage, responseObserver);
}
});
}
@Override
public void log(LogEvent logEvent) {
if (serverConnection.suppressLogCollector()) {
return;
}
final LogMessage logMessage = LogMessage.newBuilder()
.setAgentId(agentId)
.setLogEvent(logEvent)
.build();
serverConnection.callWithAFewRetries(new GrpcCall() {
@Override
public void call(StreamObserver responseObserver) {
collectorServiceStub.log(logMessage, responseObserver);
}
});
}
@OnlyUsedByTests
public void close() throws InterruptedException {
downstreamServiceObserver.close();
serverConnection.close();
}
@OnlyUsedByTests
public void awaitClose() throws InterruptedException {
serverConnection.awaitClose();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy