Create lookup tables metrics supplier (#24038)

* Implement lookup tables metrics supplier

* Add changelog
This commit is contained in:
Ismail Belkacim
2025-11-10 11:14:57 +01:00
committed by GitHub
parent dc47d11b5a
commit 000b5e33b0
5 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
type = "a"
message = "Create metrics supplier for lookup tables."
pulls = ["24038"]
issues = ["Graylog2/graylog-plugin-enterprise#12226"]

View File

@@ -136,4 +136,13 @@ public class DBLookupTableService {
public void forEach(Consumer<? super LookupTableDto> action) {
collection.find().forEach(action);
}
/**
* Returns the total count of lookup tables.
*
* @return total count of lookup tables
*/
public long count() {
return collection.countDocuments();
}
}

View File

@@ -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);
}
}

View File

@@ -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
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
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<TelemetryEvent> get() {
Map<String, Object> metrics = Map.of(
"lookup_table_count", dbLookupTableService.count()
);
return Optional.of(TelemetryEvent.of(metrics));
}
}

View File

@@ -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
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
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<TelemetryEvent> metrics = lookupTablesSupplier.get();
assertTrue(metrics.isPresent());
assertEquals(expectedCount, metrics.get().metrics().get("lookup_table_count"));
}
}