-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding simple support for backend telemetry. (#19257)
* Adding posthog java library, setting up client. * Adding capture method, adding cluster id. * Capture only if telemetry is enabled. * Establishing periodical and bindings for metrics suppliers. * Make generated `TelemetryEvent` optional. * Adding convenience methods. * Adding license headers. * Construct Posthog client for each submission, shutdown after. * Replacing posthog client with simple homebrewn client. * Adding path to Retrofit interface method. * Handle errors while submitting events. * Do not run periodial if telemetry is disabled. * Do not run `capture` if metrics are empty. * Including cluster id in groups and separate field.
- Loading branch information
1 parent
70b318c
commit b569b1a
Showing
8 changed files
with
327 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
graylog2-server/src/main/java/org/graylog2/telemetry/TelemetryModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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; | ||
|
||
import org.graylog2.plugin.PluginModule; | ||
import org.graylog2.telemetry.scheduler.TelemetrySubmissionPeriodical; | ||
|
||
public class TelemetryModule extends PluginModule { | ||
@Override | ||
protected void configure() { | ||
// Initializing binder so it can be injected with no actual bindings | ||
telemetryMetricSupplierBinder(); | ||
|
||
addPeriodical(TelemetrySubmissionPeriodical.class); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
graylog2-server/src/main/java/org/graylog2/telemetry/client/PosthogAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.client; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.collect.ImmutableMap; | ||
import retrofit2.Call; | ||
import retrofit2.http.Body; | ||
import retrofit2.http.POST; | ||
|
||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public interface PosthogAPI { | ||
record BatchRequest(@JsonProperty("api_key") String apiKey, @JsonProperty("batch") Collection<Event> batch) {} | ||
|
||
record Event(@JsonProperty("uuid") String uuid, | ||
@JsonProperty("timestamp") String timestamp, | ||
@JsonProperty("distinct_id") String distinctId, | ||
@JsonProperty("event") String event, | ||
@JsonProperty("properties") Map<String, Object> properties) { | ||
public static Event create(String clusterId, String event, Map<String, Object> properties) { | ||
final var groups = Map.of("cluster", clusterId); | ||
final var propertiesWithGroups = ImmutableMap.<String, Object>builder() | ||
.putAll(properties) | ||
.put("$groups", groups) | ||
.put("cluster_id", clusterId) | ||
.build(); | ||
return new Event(UUID.randomUUID().toString(), Instant.now().toString(), clusterId, event, propertiesWithGroups); | ||
} | ||
} | ||
|
||
@POST("/batch") | ||
Call<Void> batchSend(@Body BatchRequest batchRequest); | ||
} |
73 changes: 73 additions & 0 deletions
73
graylog2-server/src/main/java/org/graylog2/telemetry/client/TelemetryClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.client; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
import okhttp3.OkHttpClient; | ||
import org.graylog2.configuration.TelemetryConfiguration; | ||
import org.graylog2.telemetry.cluster.TelemetryClusterService; | ||
import org.graylog2.telemetry.scheduler.TelemetryEvent; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
@Singleton | ||
public class TelemetryClient { | ||
private static final Logger LOG = LoggerFactory.getLogger(TelemetryClient.class); | ||
private final PosthogAPI posthog; | ||
private final String clusterId; | ||
private final boolean isEnabled; | ||
private final String apiKey; | ||
|
||
@Inject | ||
public TelemetryClient(TelemetryConfiguration telemetryConfiguration, TelemetryClusterService telemetryClusterService, | ||
OkHttpClient okHttpClient, ObjectMapper objectMapper) { | ||
this.isEnabled = telemetryConfiguration.isTelemetryEnabled(); | ||
this.apiKey = telemetryConfiguration.getTelemetryApiKey(); | ||
this.posthog = new Retrofit.Builder() | ||
.baseUrl(telemetryConfiguration.getTelemetryApiHost()) | ||
.addConverterFactory(JacksonConverterFactory.create(objectMapper)) | ||
.client(okHttpClient) | ||
.build() | ||
.create(PosthogAPI.class); | ||
this.clusterId = telemetryClusterService.getClusterId(); | ||
} | ||
|
||
public void capture(Map<String, TelemetryEvent> events) throws IOException { | ||
if (isEnabled) { | ||
final var batch = events.entrySet() | ||
.stream() | ||
.map(entry -> PosthogAPI.Event.create(clusterId, entry.getKey(), entry.getValue().metrics())) | ||
.toList(); | ||
final var request = new PosthogAPI.BatchRequest(apiKey, batch); | ||
final var response = posthog.batchSend(request).execute(); | ||
if (!response.isSuccessful()) { | ||
throw new RuntimeException("Submitting telemetry failed with status " + response.code() + " - message: " + response.message()); | ||
} | ||
} | ||
} | ||
|
||
public boolean isEnabled() { | ||
return isEnabled; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
graylog2-server/src/main/java/org/graylog2/telemetry/scheduler/TelemetryEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.scheduler; | ||
|
||
import java.util.Map; | ||
|
||
public record TelemetryEvent(Map<String, Object> metrics) { | ||
public static TelemetryEvent of(Map<String, Object> metrics) { | ||
return new TelemetryEvent(metrics); | ||
} | ||
|
||
public static TelemetryEvent of(String key, Object value) { | ||
return TelemetryEvent.of(Map.of(key, value)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
graylog2-server/src/main/java/org/graylog2/telemetry/scheduler/TelemetryMetricSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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.scheduler; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public interface TelemetryMetricSupplier extends Supplier<Optional<TelemetryEvent>> { | ||
} |
104 changes: 104 additions & 0 deletions
104
...-server/src/main/java/org/graylog2/telemetry/scheduler/TelemetrySubmissionPeriodical.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.scheduler; | ||
|
||
import com.github.joschi.jadconfig.util.Duration; | ||
import jakarta.inject.Inject; | ||
import org.graylog2.configuration.TelemetryConfiguration; | ||
import org.graylog2.plugin.periodical.Periodical; | ||
import org.graylog2.telemetry.client.TelemetryClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.AbstractMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class TelemetrySubmissionPeriodical extends Periodical { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(TelemetrySubmissionPeriodical.class); | ||
private final TelemetryClient telemetryClient; | ||
private final Map<String, TelemetryMetricSupplier> metricsProviders; | ||
private static final Duration runPeriod = Duration.days(1); | ||
private final boolean isEnabled; | ||
|
||
@Inject | ||
public TelemetrySubmissionPeriodical(TelemetryClient telemetryClient, | ||
TelemetryConfiguration telemetryConfiguration, | ||
Map<String, TelemetryMetricSupplier> metricsProviders) { | ||
this.telemetryClient = telemetryClient; | ||
this.metricsProviders = metricsProviders; | ||
this.isEnabled = telemetryConfiguration.isTelemetryEnabled(); | ||
} | ||
|
||
@Override | ||
public void doRun() { | ||
final var telemetryMetrics = metricsProviders.entrySet() | ||
.stream() | ||
.map(entry -> entry(entry.getKey(), entry.getValue().get())) | ||
.flatMap(entry -> entry.getValue().map(metrics -> entry(entry.getKey(), metrics)).stream()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
try { | ||
if (!telemetryMetrics.isEmpty()) { | ||
telemetryClient.capture(telemetryMetrics); | ||
} | ||
} catch (Exception e) { | ||
LOG.warn("Error while submitting telemetry: ", e); | ||
} | ||
} | ||
|
||
private <K, V> Map.Entry<K, V> entry(K key, V value) { | ||
return new AbstractMap.SimpleEntry<>(key, value); | ||
} | ||
|
||
@Override | ||
public boolean runsForever() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean stopOnGracefulShutdown() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean startOnThisNode() { | ||
return isEnabled; | ||
} | ||
|
||
@Override | ||
public boolean isDaemon() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getInitialDelaySeconds() { | ||
return Math.toIntExact(runPeriod.toSeconds()); | ||
} | ||
|
||
@Override | ||
public int getPeriodSeconds() { | ||
return Math.toIntExact(runPeriod.toSeconds()); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
protected Logger getLogger() { | ||
return LOG; | ||
} | ||
} |