Skip to content

Commit

Permalink
#23 Basic metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Nov 14, 2023
1 parent 4c0b325 commit ba14bc0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Pipe observations from Building Automation Systems to your favorite Cloud Provid
ingestion.interval.minutes
sensormappings.simulator.enabled=true
```

## Statistics and Metrics

[Metrics](http://localhost:8083/admin/metrics/app/*)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.cantara.realestate.cloudconnector;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import no.cantara.config.ApplicationProperties;
import no.cantara.realestate.cloudconnector.ingestion.SimulatorPresentValueIngestionService;
import no.cantara.realestate.cloudconnector.ingestion.SimulatorTrendsIngestionService;
Expand Down Expand Up @@ -50,6 +52,7 @@ public class RealestateCloudconnectorApplication extends AbstractStingrayApplica
private ObservationDistributor observationDistributor;
private MappedIdRepository mappedIdRepository;
private Thread observationDistributorThread;
private MetricRegistry metricRegistry;

public RealestateCloudconnectorApplication(ApplicationProperties config) {
super("RealestateCloudconnector",
Expand Down Expand Up @@ -89,6 +92,10 @@ protected void doInit() {
boolean useSimulatedSensors = config.asBoolean("sensormappings.simulator.enabled");
initBuiltinDefaults();
StingraySecurity.initSecurity(this);
metricRegistry = get(MetricRegistry.class);
if (metricRegistry == null) {
throw new RealestateCloudconnectorException("Missing Metric Registry");
}
mappedIdRepository = createMappedIdRepository(useSimulatedSensors);
initNotificationServices();
initObservationReceiver();
Expand All @@ -99,6 +106,9 @@ protected void doInit() {
initRouter();
initObservationDistributor();

//Setup Metrics observation
initMetrics();

//StatusGui
init(Random.class, this::createRandom);
SystemStatusResource systemStatusResource = initAndRegisterJaxRsWsComponent(SystemStatusResource.class, this::createSystemStatusResource);
Expand Down Expand Up @@ -152,6 +162,16 @@ protected void doInit() {

}

private void initMetrics() {
metricRegistry.register(MetricRegistry.name(ObservationsRepository.class, "ObservationsQueue", "size"),
new Gauge<Long>() {
@Override
public Long getValue() {
return observationsRepository.getObservedValuesQueueSize();
}
});
}

private RepositoryResource creatRepositoryStatusResource() {
TemplateEngine templateEngine = new TemplateEngine();
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
Expand Down Expand Up @@ -308,7 +328,7 @@ private void initObservationDistributor() {
log.warn("ObservationsRepository is null. Cannot start ObservationDistributor");
throw new RealestateCloudconnectorException("ObservationsRepository is null. Cannot start ObservationDistributor");
}
observationDistributor = new ObservationDistributor(observationsRepository, new ArrayList<>(distributionServices.values()), mappedIdRepository);
observationDistributor = new ObservationDistributor(observationsRepository, new ArrayList<>(distributionServices.values()), mappedIdRepository,metricRegistry);
get(StingrayHealthService.class).registerHealthProbe("ObservationDistributor-isHealthy: ", observationDistributor::isHealthy);
get(StingrayHealthService.class).registerHealthProbe("ObservationsRepository-ObservedValues-distributed: ", observationDistributor::getObservedValueDistributedCount);
observationDistributorThread = new Thread(observationDistributor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package no.cantara.realestate.cloudconnector.routing;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import no.cantara.realestate.cloudconnector.mappedid.MappedIdRepository;
import no.cantara.realestate.cloudconnector.semantics.ObservationMapper;
import no.cantara.realestate.observations.ObservationMessage;
Expand All @@ -24,12 +27,23 @@ public class ObservationDistributor implements Runnable {
private static final long DEFAULT_SLEEP_PERIOD_MS = 100;
private long sleepPeriod;
private long observedValueDistributedCount;
MetricRegistry metricRegistry;
Meter distributedMeter;

public ObservationDistributor(ObservationsRepository observationsRepository, List<DistributionService> distributionServices, MappedIdRepository mappedIdRepository) {
public ObservationDistributor(ObservationsRepository observationsRepository, List<DistributionService> distributionServices, MappedIdRepository mappedIdRepository, MetricRegistry metricRegistry) {
this.observationsRepository = observationsRepository;
this.distributionServices = distributionServices;
this.mappedIdRepository = mappedIdRepository;
sleepPeriod = DEFAULT_SLEEP_PERIOD_MS;
this.metricRegistry = metricRegistry;
distributedMeter = metricRegistry.meter("ObservationsDistributed");
metricRegistry.register(MetricRegistry.name(ObservationDistributor.class, "ObservationsDistributed", "total"),
new Gauge<Long>() {
@Override
public Long getValue() {
return observedValueDistributedCount;
}
});
}

@Override
Expand Down Expand Up @@ -69,6 +83,7 @@ protected void addSemanticsAndDistribute(ObservedValue observedValue) {
ObservationMessage observationMessage = ObservationMapper.buildRecObservation(mappedSensorId, observedValue);
for (DistributionService distributionService : distributionServices) {
auditLog.trace("Distribute__Publish__{}__{}__{}__{}__{}", distributionService.getName(), observationMessage.getClass(), sensorId.getId(), observationMessage.getValue(), observationMessage.getObservationTime());
distributedMeter.mark();
distributionService.publish(observationMessage);
}
addObservedValueDistributedCount();
Expand Down

0 comments on commit ba14bc0

Please sign in to comment.