mirror of
https://github.com/Graylog2/graylog2-server.git
synced 2026-03-13 09:32:21 +08:00
Create MongoDB metrics supplier (#24091)
* Implement mongodb metrics supplier * Add changelog --------- Co-authored-by: Florian Petersen <188503754+fpetersen-gl@users.noreply.github.com>
This commit is contained in:
5
changelog/unreleased/pr-24091.toml
Normal file
5
changelog/unreleased/pr-24091.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
type = "a"
|
||||
message = "Create metrics supplier for MongoDB."
|
||||
|
||||
pulls = ["24091"]
|
||||
issues = ["Graylog2/graylog-plugin-enterprise#12206"]
|
||||
@@ -21,6 +21,7 @@ import org.graylog2.plugin.PluginModule;
|
||||
import org.graylog2.telemetry.scheduler.TelemetrySubmissionPeriodical;
|
||||
import org.graylog2.telemetry.suppliers.InputsMetricsSupplier;
|
||||
import org.graylog2.telemetry.suppliers.OutputsMetricsSupplier;
|
||||
import org.graylog2.telemetry.suppliers.MongoDBMetricsSupplier;
|
||||
import org.graylog2.telemetry.suppliers.ShardsMetricsSupplier;
|
||||
import org.graylog2.telemetry.suppliers.LookupTablesSupplier;
|
||||
|
||||
@@ -35,6 +36,7 @@ public class TelemetryModule extends PluginModule {
|
||||
|
||||
addTelemetryMetricProvider("Inputs Metrics", InputsMetricsSupplier.class);
|
||||
addTelemetryMetricProvider("Outputs Metrics", OutputsMetricsSupplier.class);
|
||||
addTelemetryMetricProvider("MongoDB Metrics", MongoDBMetricsSupplier.class);
|
||||
addTelemetryMetricProvider("Shards Metrics", ShardsMetricsSupplier.class);
|
||||
addTelemetryMetricProvider("Lookup Tables Metrics", LookupTablesSupplier.class);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 com.mongodb.MongoClient;
|
||||
import jakarta.inject.Inject;
|
||||
import org.graylog2.database.MongoDBVersionCheck;
|
||||
import org.graylog2.telemetry.scheduler.TelemetryEvent;
|
||||
import org.graylog2.telemetry.scheduler.TelemetryMetricSupplier;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MongoDBMetricsSupplier implements TelemetryMetricSupplier {
|
||||
private final MongoClient mongoClient;
|
||||
|
||||
@Inject
|
||||
public MongoDBMetricsSupplier(MongoClient mongoClient) {
|
||||
this.mongoClient = mongoClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TelemetryEvent> get() {
|
||||
return Optional.ofNullable(MongoDBVersionCheck.getVersion(mongoClient))
|
||||
.map(version -> TelemetryEvent.of(Map.of("version", version.toString())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 com.github.zafarkhaja.semver.Version;
|
||||
import com.mongodb.MongoClient;
|
||||
import org.graylog2.database.MongoDBVersionCheck;
|
||||
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.MockedStatic;
|
||||
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.mockStatic;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class MongoDBMetricsSupplierTest {
|
||||
@Mock
|
||||
private MongoClient mongoClient;
|
||||
|
||||
@InjectMocks
|
||||
private MongoDBMetricsSupplier mongoDBMetricsSupplier;
|
||||
|
||||
@Test
|
||||
public void shouldReturnMongoDBMetrics() {
|
||||
Version version = Version.of(7, 0, 24);
|
||||
|
||||
try (MockedStatic<MongoDBVersionCheck> mongoDBVersionCheck = mockStatic(MongoDBVersionCheck.class)) {
|
||||
mongoDBVersionCheck.when(() -> MongoDBVersionCheck.getVersion(mongoClient)).thenReturn(version);
|
||||
|
||||
Optional<TelemetryEvent> event = mongoDBMetricsSupplier.get();
|
||||
|
||||
assertTrue(event.isPresent());
|
||||
assertEquals(version.toString(), event.get().metrics().get("version"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user