diff --git a/changelog/unreleased/pr-24038.toml b/changelog/unreleased/pr-24038.toml new file mode 100644 index 0000000000..46fb683305 --- /dev/null +++ b/changelog/unreleased/pr-24038.toml @@ -0,0 +1,5 @@ +type = "a" +message = "Create metrics supplier for lookup tables." + +pulls = ["24038"] +issues = ["Graylog2/graylog-plugin-enterprise#12226"] diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java index c94b4475b4..44c824d342 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java @@ -136,4 +136,13 @@ public class DBLookupTableService { public void forEach(Consumer action) { collection.find().forEach(action); } + + /** + * Returns the total count of lookup tables. + * + * @return total count of lookup tables + */ + public long count() { + return collection.countDocuments(); + } } diff --git a/graylog2-server/src/main/java/org/graylog2/telemetry/TelemetryModule.java b/graylog2-server/src/main/java/org/graylog2/telemetry/TelemetryModule.java index 387a48c180..6123110711 100644 --- a/graylog2-server/src/main/java/org/graylog2/telemetry/TelemetryModule.java +++ b/graylog2-server/src/main/java/org/graylog2/telemetry/TelemetryModule.java @@ -22,6 +22,7 @@ import org.graylog2.telemetry.scheduler.TelemetrySubmissionPeriodical; import org.graylog2.telemetry.suppliers.InputsMetricsSupplier; import org.graylog2.telemetry.suppliers.OutputsMetricsSupplier; import org.graylog2.telemetry.suppliers.ShardsMetricsSupplier; +import org.graylog2.telemetry.suppliers.LookupTablesSupplier; public class TelemetryModule extends PluginModule { @Override @@ -35,5 +36,6 @@ public class TelemetryModule extends PluginModule { addTelemetryMetricProvider("Inputs Metrics", InputsMetricsSupplier.class); addTelemetryMetricProvider("Outputs Metrics", OutputsMetricsSupplier.class); addTelemetryMetricProvider("Shards Metrics", ShardsMetricsSupplier.class); + addTelemetryMetricProvider("Lookup Tables Metrics", LookupTablesSupplier.class); } } diff --git a/graylog2-server/src/main/java/org/graylog2/telemetry/suppliers/LookupTablesSupplier.java b/graylog2-server/src/main/java/org/graylog2/telemetry/suppliers/LookupTablesSupplier.java new file mode 100644 index 0000000000..0118f2f39a --- /dev/null +++ b/graylog2-server/src/main/java/org/graylog2/telemetry/suppliers/LookupTablesSupplier.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2020 Graylog, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * 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 + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * . + */ +package org.graylog2.telemetry.suppliers; + +import jakarta.inject.Inject; +import org.graylog2.lookup.db.DBLookupTableService; +import org.graylog2.telemetry.scheduler.TelemetryEvent; +import org.graylog2.telemetry.scheduler.TelemetryMetricSupplier; + +import java.util.Map; +import java.util.Optional; + +public class LookupTablesSupplier implements TelemetryMetricSupplier { + private final DBLookupTableService dbLookupTableService; + + @Inject + public LookupTablesSupplier(DBLookupTableService dbLookupTableService) { + this.dbLookupTableService = dbLookupTableService; + } + + @Override + public Optional get() { + Map metrics = Map.of( + "lookup_table_count", dbLookupTableService.count() + ); + + return Optional.of(TelemetryEvent.of(metrics)); + } +} diff --git a/graylog2-server/src/test/java/org/graylog2/telemetry/suppliers/LookupTablesSupplierTest.java b/graylog2-server/src/test/java/org/graylog2/telemetry/suppliers/LookupTablesSupplierTest.java new file mode 100644 index 0000000000..a14f622617 --- /dev/null +++ b/graylog2-server/src/test/java/org/graylog2/telemetry/suppliers/LookupTablesSupplierTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 Graylog, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * 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 + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * . + */ +package org.graylog2.telemetry.suppliers; + +import org.graylog2.lookup.db.DBLookupTableService; +import org.graylog2.telemetry.scheduler.TelemetryEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class LookupTablesSupplierTest { + @Mock + private DBLookupTableService dbLookupTableService; + + @InjectMocks + private LookupTablesSupplier lookupTablesSupplier; + + @Test + public void shouldReturnLookupTablesMetrics() { + long expectedCount = 5L; + when(dbLookupTableService.count()).thenReturn(expectedCount); + + Optional metrics = lookupTablesSupplier.get(); + + assertTrue(metrics.isPresent()); + assertEquals(expectedCount, metrics.get().metrics().get("lookup_table_count")); + } +}