-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: emit mqtt metrics #106
Open
vaibhavmurkute
wants to merge
2
commits into
main
Choose a base branch
from
broker-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
100 changes: 100 additions & 0 deletions
100
integration/src/main/java/com/aws/greengrass/mqtt/moquette/metrics/MetricsStore.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,100 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.mqtt.moquette.metrics; | ||
|
||
|
||
import com.aws.greengrass.telemetry.impl.Metric; | ||
import com.aws.greengrass.telemetry.models.TelemetryAggregation; | ||
import com.aws.greengrass.telemetry.models.TelemetryUnit; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
|
||
import java.time.Instant; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
||
public class MetricsStore { | ||
public static final String LOCAL_MQTT_NAMESPACE = "LocalMQTT"; | ||
@Getter(AccessLevel.PACKAGE) | ||
private final Map<MqttMetric, AtomicInteger> metrics = new HashMap<>(); | ||
|
||
private MetricsStore() { | ||
init(); | ||
} | ||
|
||
private static class MetricsStoreHelper { | ||
@SuppressWarnings("PMD.AccessorClassGeneration") | ||
private static final MetricsStore INSTANCE = new MetricsStore(); | ||
} | ||
|
||
/** | ||
* Gets the singleton instance of MetricsStore with lazy initialization. | ||
* | ||
* @return {@link MetricsStore} | ||
*/ | ||
public static MetricsStore getInstance() { | ||
return MetricsStoreHelper.INSTANCE; | ||
} | ||
|
||
public void incrementMetricValue(MqttMetric metric) { | ||
Optional.ofNullable(metrics.get(metric)).map(AtomicInteger::incrementAndGet); | ||
} | ||
|
||
public List<Metric> getAndResetTelemetryMetrics() { | ||
return metrics.keySet().stream().map(this::getTelemetryMetricAndReset) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Metric getTelemetryMetricAndReset(MqttMetric metric) { | ||
int metricValue = metrics.get(metric).getAndSet(0); | ||
return Metric.builder() | ||
.namespace(LOCAL_MQTT_NAMESPACE) | ||
.name(metric.toString()) | ||
.unit(TelemetryUnit.Count) | ||
.aggregation(TelemetryAggregation.Sum) | ||
.value(metricValue) | ||
.timestamp(Instant.now().toEpochMilli()) | ||
.build(); | ||
} | ||
|
||
private void init() { | ||
metrics.put(MqttMetric.CONNECT_SUCCESS, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.CONNECT_AUTH_ERROR, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.SUBSCRIBE_SUCCESS, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.SUBSCRIBE_AUTH_ERROR, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.PUBLISH_OUT_SUCCESS, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.PUBLISH_IN_AUTH_ERROR, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.UNSUBSCRIBE, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.DISCONNECT, new AtomicInteger(0)); | ||
metrics.put(MqttMetric.UNKNOWN_AUTH_ERROR, new AtomicInteger(0)); | ||
} | ||
|
||
public enum MqttMetric { | ||
CONNECT_SUCCESS("Connect.Success"), | ||
CONNECT_AUTH_ERROR("Connect.AuthError"), | ||
SUBSCRIBE_SUCCESS("Subscribe.Success"), | ||
SUBSCRIBE_AUTH_ERROR("Subscribe.AuthError"), | ||
PUBLISH_OUT_SUCCESS("PublishOut.Success"), | ||
PUBLISH_IN_AUTH_ERROR("PublishIn.AuthError"), | ||
DISCONNECT("Disconnect"), | ||
UNSUBSCRIBE("Unsubscribe"), | ||
UNKNOWN_AUTH_ERROR("UnknownAuthError"); | ||
|
||
private final String name; | ||
MqttMetric(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
integration/src/main/java/com/aws/greengrass/mqtt/moquette/metrics/MqttMetricsCaptor.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,49 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.mqtt.moquette.metrics; | ||
|
||
import com.aws.greengrass.mqtt.moquette.metrics.MetricsStore.MqttMetric; | ||
import io.moquette.interception.AbstractInterceptHandler; | ||
import io.moquette.interception.InterceptHandler; | ||
import io.moquette.interception.messages.InterceptConnectMessage; | ||
import io.moquette.interception.messages.InterceptDisconnectMessage; | ||
import io.moquette.interception.messages.InterceptPublishMessage; | ||
import io.moquette.interception.messages.InterceptSubscribeMessage; | ||
import io.moquette.interception.messages.InterceptUnsubscribeMessage; | ||
|
||
|
||
public class MqttMetricsCaptor extends AbstractInterceptHandler implements InterceptHandler { | ||
|
||
@Override | ||
public String getID() { | ||
return "MoquetteMqttMetricsCaptor"; | ||
} | ||
|
||
@Override | ||
public void onConnect(InterceptConnectMessage msg) { | ||
MetricsStore.getInstance().incrementMetricValue(MqttMetric.CONNECT_SUCCESS); | ||
} | ||
|
||
@Override | ||
public void onDisconnect(InterceptDisconnectMessage msg) { | ||
MetricsStore.getInstance().incrementMetricValue(MqttMetric.DISCONNECT); | ||
} | ||
|
||
@Override | ||
public void onPublish(InterceptPublishMessage msg) { | ||
MetricsStore.getInstance().incrementMetricValue(MqttMetric.PUBLISH_OUT_SUCCESS); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(InterceptSubscribeMessage msg) { | ||
MetricsStore.getInstance().incrementMetricValue(MqttMetric.SUBSCRIBE_SUCCESS); | ||
} | ||
|
||
@Override | ||
public void onUnsubscribe(InterceptUnsubscribeMessage msg) { | ||
MetricsStore.getInstance().incrementMetricValue(MqttMetric.UNSUBSCRIBE); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
integration/src/main/java/com/aws/greengrass/mqtt/moquette/metrics/MqttMetricsEmitter.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,52 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.mqtt.moquette.metrics; | ||
|
||
import com.aws.greengrass.telemetry.impl.Metric; | ||
import com.aws.greengrass.telemetry.impl.MetricFactory; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.inject.Inject; | ||
|
||
public class MqttMetricsEmitter { | ||
private static final long DEFAULT_METRIC_EMIT_FREQUENCY_SECONDS = 30; | ||
|
||
private final ScheduledExecutorService ses; | ||
private final MetricFactory metricFactory = new MetricFactory(MetricsStore.LOCAL_MQTT_NAMESPACE); | ||
|
||
private ScheduledFuture<?> emitFuture; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ses ScheduledExecutorService for periodic metric emission | ||
*/ | ||
@Inject | ||
public MqttMetricsEmitter(ScheduledExecutorService ses) { | ||
this.ses = ses; | ||
} | ||
|
||
/** | ||
* Schedules periodic MQTT metric emission. | ||
*/ | ||
public void schedulePeriodicMetricEmit() { | ||
if (emitFuture != null) { | ||
emitFuture.cancel(true); | ||
} | ||
|
||
emitFuture = ses.scheduleAtFixedRate(this::emitMetrics, DEFAULT_METRIC_EMIT_FREQUENCY_SECONDS, | ||
DEFAULT_METRIC_EMIT_FREQUENCY_SECONDS, TimeUnit.SECONDS); | ||
} | ||
|
||
private void emitMetrics() { | ||
List<Metric> metrics = MetricsStore.getInstance().getAndResetTelemetryMetrics(); | ||
metrics.forEach(metricFactory::putMetricData); | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make this configurable?