Skip to content

Commit

Permalink
Add timer capability to the Prometheus metrics
Browse files Browse the repository at this point in the history
Co-authored-by: Ioannis Panagiotas <ioannis.panagiotas@neotechnology.com>
  • Loading branch information
vnickolov and IoannisPanagiotas committed Nov 15, 2023
1 parent d82176a commit 45ac77e
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,16 @@ public <A extends Algorithm<R>, R, C extends AlgoBaseConfig> AlgorithmComputatio
);

// run the algorithm
try {
algorithmMetricsService.started(algorithmFactory.taskName());
var algorithmMetric = algorithmMetricsService.create(algorithmFactory.taskName());
try(algorithmMetric) {
algorithmMetric.start();
var algorithmResult = algorithm.compute();

return AlgorithmComputationResult.of(algorithmResult, graph, graphStore, algorithm.getTerminationFlag());
} catch (Exception e) {
log.warn("Computation failed", e);
algorithm.getProgressTracker().endSubTaskWithFailure();
algorithmMetricsService.failed(algorithmFactory.taskName());
algorithmMetric.failed();
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.neo4j.gds.Algorithm;
import org.neo4j.gds.GraphAlgorithmFactory;
import org.neo4j.gds.algorithms.AlgorithmMemoryValidationService;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetric;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.api.Graph;
Expand All @@ -40,6 +41,7 @@

import static org.assertj.core.api.Assertions.assertThatException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -58,7 +60,9 @@ void shouldRegisterAlgorithmMetricCountForSuccess() {
when(graphStoreCatalogServiceMock.getGraphWithGraphStore(any(), any(), any(), any(), any()))
.thenReturn(Pair.of(graphMock, mock(GraphStore.class)));

var algorithmMetricMock = mock(AlgorithmMetric.class);
var algorithmMetricsServiceMock = mock(AlgorithmMetricsService.class);
when(algorithmMetricsServiceMock.create(anyString())).thenReturn(algorithmMetricMock);

var logMock = mock(Log.class);
when(logMock.getNeo4jLog()).thenReturn(Neo4jProxy.testLog());
Expand Down Expand Up @@ -87,9 +91,14 @@ void shouldRegisterAlgorithmMetricCountForSuccess() {
DatabaseId.EMPTY
);

verify(algorithmMetricsServiceMock, times(1)).started("TestingMetrics");
verify(algorithmMetricsServiceMock, times(0)).failed("TestingMetrics");
verifyNoMoreInteractions(algorithmMetricsServiceMock);
verify(algorithmMetricsServiceMock, times(1)).create("TestingMetrics");
verify(algorithmMetricMock, times(1)).start();
verify(algorithmMetricMock, times(1)).close();
verify(algorithmMetricMock, times(0)).failed();
verifyNoMoreInteractions(
algorithmMetricsServiceMock,
algorithmMetricMock
);
}


Expand All @@ -102,7 +111,9 @@ void shouldRegisterAlgorithmMetricCountForFailure() {
when(graphStoreCatalogServiceMock.getGraphWithGraphStore(any(), any(), any(), any(), any()))
.thenReturn(Pair.of(graphMock, mock(GraphStore.class)));

var algorithmMetricMock = mock(AlgorithmMetric.class);
var algorithmMetricsServiceMock = mock(AlgorithmMetricsService.class);
when(algorithmMetricsServiceMock.create(anyString())).thenReturn(algorithmMetricMock);

var logMock = mock(Log.class);
when(logMock.getNeo4jLog()).thenReturn(Neo4jProxy.testLog());
Expand Down Expand Up @@ -134,9 +145,14 @@ void shouldRegisterAlgorithmMetricCountForFailure() {
)
).withMessage("Ooops");

verify(algorithmMetricsServiceMock, times(1)).started("TestingMetrics");
verify(algorithmMetricsServiceMock, times(1)).failed("TestingMetrics");
verifyNoMoreInteractions(algorithmMetricsServiceMock);
verify(algorithmMetricsServiceMock, times(1)).create("TestingMetrics");
verify(algorithmMetricMock, times(1)).start();
verify(algorithmMetricMock, times(1)).close();
verify(algorithmMetricMock, times(1)).failed();
verifyNoMoreInteractions(
algorithmMetricsServiceMock,
algorithmMetricMock
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.algorithms.metrics;

public abstract class AlgorithmMetric implements AutoCloseable {

protected final String algorithm;

protected AlgorithmMetric(String algorithm) {
this.algorithm = algorithm;
}

public abstract void start();

public abstract void failed();

@Override
public abstract void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@

public interface AlgorithmMetricRegistrar {

void started(String algorithm);
void failed(String algorithm);
AlgorithmMetric create(String algorithm);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public AlgorithmMetricsService(AlgorithmMetricRegistrar metricRegistrar) {
this.metricRegistrar = metricRegistrar;
}

public void started(String algorithm) {
metricRegistrar.started(algorithm);
public AlgorithmMetric create(String algorithm) {
return metricRegistrar.create(algorithm);
}

public void failed(String algorithm) {
metricRegistrar.failed(algorithm);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.algorithms.metrics;

public final class PassthroughAlgorithmMetric extends AlgorithmMetric {

PassthroughAlgorithmMetric(String algorithm) {
super(algorithm);
}

@Override
public void start() {}

@Override
public void failed() {}

@Override
public void close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
public class PassthroughAlgorithmMetricRegistrar implements AlgorithmMetricRegistrar {

@Override
public void started(String algorithm) {

}

@Override
public void failed(String algorithm) {

public AlgorithmMetric create(String algorithm) {
return new PassthroughAlgorithmMetric(algorithm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,17 @@
class AlgorithmMetricsServiceTest {

@Test
void shouldRegisterStarted() {
void shouldCreateAlgorithmMetric() {
// given
var registrarMock = mock(AlgorithmMetricRegistrar.class);
var metricsService = new AlgorithmMetricsService(registrarMock);

// when
metricsService.started("foo");
metricsService.create("foo");

// then
verify(registrarMock, times(1)).started("foo");
verify(registrarMock, times(1)).create("foo");
verifyNoMoreInteractions(registrarMock);
}

@Test
void shouldRegisterFailed() {
// given
var registrarMock = mock(AlgorithmMetricRegistrar.class);
var metricsService = new AlgorithmMetricsService(registrarMock);

// when
metricsService.failed("foo");

// then
verify(registrarMock, times(1)).failed("foo");
verifyNoMoreInteractions(registrarMock);
}
}

0 comments on commit 45ac77e

Please sign in to comment.