Skip to content

Commit 6ac3f87

Browse files
committed
[#3520] specific metrics for unknown messages
Signed-off-by: Bob Claerhout <claerhout.bob@gmail.com>
1 parent 4a8911a commit 6ac3f87

27 files changed

Lines changed: 524 additions & 13 deletions

adapters/http-base/src/main/java/org/eclipse/hono/adapter/http/AbstractVertxBasedHttpProtocolAdapter.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public abstract class AbstractVertxBasedHttpProtocolAdapter<T extends HttpProtoc
8585

8686
private static final String KEY_MATCH_ALL_ROUTE_APPLIED = "matchAllRouteApplied";
8787

88-
private HttpAdapterMetrics metrics = HttpAdapterMetrics.NOOP;
88+
protected HttpAdapterMetrics metrics = HttpAdapterMetrics.NOOP;
8989
private HttpServer server;
9090
private HttpServer insecureServer;
9191

@@ -203,7 +203,15 @@ public final void doStart(final Promise<Void> startPromise) {
203203
.onComplete(startPromise);
204204
}
205205

206-
private Sample getMicrometerSample(final RoutingContext ctx) {
206+
/**
207+
* Gets the timer used to track the processing of a telemetry message.
208+
*
209+
* @param ctx The routing context to extract the sample from.
210+
* @return The sample or {@code null} if the context does not
211+
* contain a sample.
212+
* @throws NullPointerException if ctx is {@code null}.
213+
*/
214+
protected Sample getMicrometerSample(final RoutingContext ctx) {
207215
return ctx.get(KEY_MICROMETER_SAMPLE);
208216
}
209217

@@ -1295,9 +1303,16 @@ public final void uploadCommandResponseMessage(
12951303
});
12961304
}
12971305

1298-
private static MetricsTags.QoS getQoSLevel(
1299-
final EndpointType endpoint,
1300-
final org.eclipse.hono.util.QoS requestedQos) {
1306+
/**
1307+
* Get the QoS based on the endpoint and the requested QoS.
1308+
*
1309+
* @param endpoint The endpoint the message was sent to.
1310+
* @param requestedQos The QoS requested by the sender.
1311+
* @return The resulting QoS.
1312+
*/
1313+
protected static MetricsTags.QoS getQoSLevel(
1314+
final EndpointType endpoint,
1315+
final org.eclipse.hono.util.QoS requestedQos) {
13011316

13021317
if (endpoint == EndpointType.EVENT) {
13031318
return MetricsTags.QoS.AT_LEAST_ONCE;

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/LoraProtocolAdapter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ void handleProviderRoute(final HttpContext ctx, final LoraProvider provider) {
269269
currentSpan.finish();
270270
// discard the message but return 202 to not cause errors on the LoRa provider side
271271
handle202(ctx.getRoutingContext());
272+
273+
final Future<TenantObject> tenantTracker = getTenantConfiguration(gatewayDevice.getTenantId(), currentSpan.context());
274+
final MetricsTags.EndpointType endpoint = MetricsTags.EndpointType.fromString(ctx.getRequestedResource().getEndpoint());
275+
final MetricsTags.QoS qos = getQoSLevel(endpoint, ctx.getRequestedQos());
276+
metrics.reportTelemetry(
277+
endpoint,
278+
gatewayDevice.getTenantId(),
279+
tenantTracker.result(),
280+
MetricsTags.ProcessingOutcome.UNKNOWN,
281+
qos,
282+
ctx.getRoutingContext().body().buffer().length(),
283+
ctx.getTtdStatus(),
284+
getMicrometerSample(ctx.getRoutingContext()));
272285
}
273286
} catch (final LoraProviderMalformedPayloadException e) {
274287
LOG.debug("error processing request from provider [name: {}]", provider.getProviderName(), e);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) 2023 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
14+
15+
package org.eclipse.hono.adapter.lora;
16+
17+
import io.vertx.core.buffer.Buffer;
18+
19+
20+
/**
21+
* A Lora message that contains unknown data sent from an end-device to a Network Server.
22+
*
23+
*/
24+
public class UnknownLoraMessage implements LoraMessage {
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
@Override
30+
public final byte[] getDevEUI() {
31+
return new byte[0];
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
@Override
38+
public final String getDevEUIAsString() {
39+
return "";
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
@Override
46+
public final LoraMessageType getType() {
47+
return LoraMessageType.UNKNOWN;
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
@Override
54+
public final Buffer getPayload() {
55+
return Buffer.buffer();
56+
}
57+
}

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/providers/JsonBasedLoraProvider.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.eclipse.hono.adapter.lora.LoraMessage;
2121
import org.eclipse.hono.adapter.lora.LoraMessageType;
2222
import org.eclipse.hono.adapter.lora.LoraMetaData;
23+
import org.eclipse.hono.adapter.lora.UnknownLoraMessage;
2324
import org.eclipse.hono.adapter.lora.UplinkLoraMessage;
2425
import org.eclipse.hono.util.CommandEndpoint;
2526
import org.eclipse.hono.util.Strings;
@@ -51,7 +52,7 @@ public LoraMessage getMessage(final RoutingContext ctx) {
5152
case UPLINK:
5253
return createUplinkMessage(ctx.request(), message);
5354
default:
54-
throw new LoraProviderMalformedPayloadException(String.format("unsupported message type [%s]", type));
55+
return createUnknownMessage(ctx.request(), message);
5556
}
5657
} catch (final RuntimeException e) {
5758
// catch generic exception in order to also cover any (runtime) exceptions
@@ -184,4 +185,26 @@ protected UplinkLoraMessage createUplinkMessage(final HttpServerRequest request,
184185
message.setAdditionalData(getAdditionalData(requestBody));
185186
return message;
186187
}
188+
189+
/**
190+
* Creates an object representation of a Lora unknown message.
191+
* <p>
192+
* This method uses the {@link #getDevEui(JsonObject)}
193+
* method to extract relevant information from the request body to add
194+
* to the returned message.
195+
*
196+
* @param request The request sent by the provider's Network Server.
197+
* @param requestBody The JSON object contained in the request's body.
198+
* @return The message.
199+
* @throws RuntimeException if the message cannot be parsed.
200+
*/
201+
protected UnknownLoraMessage createUnknownMessage(final HttpServerRequest request, final JsonObject requestBody) {
202+
203+
Objects.requireNonNull(requestBody);
204+
205+
final UnknownLoraMessage message = new UnknownLoraMessage();
206+
return message;
207+
}
208+
209+
187210
}

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/providers/KerlinkProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ public Set<String> pathPrefixes() {
7979
*/
8080
@Override
8181
public LoraMessageType getMessageType(final JsonObject loraMessage) {
82-
return LoraMessageType.UPLINK;
82+
if (loraMessage.containsKey(FIELD_KERLINK_PAYLOAD)) {
83+
return LoraMessageType.UPLINK;
84+
}
85+
return LoraMessageType.UNKNOWN;
8386
}
8487

8588
@Override

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/providers/KerlinkProviderCustomContentType.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public Set<String> pathPrefixes() {
5757
*/
5858
@Override
5959
public LoraMessageType getMessageType(final JsonObject loraMessage) {
60-
return LoraMessageType.UPLINK;
60+
if (loraMessage.containsKey(FIELD_UPLINK_PAYLOAD)) {
61+
return LoraMessageType.UPLINK;
62+
}
63+
return LoraMessageType.UNKNOWN;
6164
}
6265

6366
/**

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/providers/OrbiwiseProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ protected Buffer getPayload(final JsonObject loraMessage) {
8282
protected LoraMessageType getMessageType(final JsonObject loraMessage) {
8383

8484
Objects.requireNonNull(loraMessage);
85-
return LoraMessageType.UPLINK;
85+
if (loraMessage.containsKey(FIELD_ORBIWISE_PAYLOAD)) {
86+
return LoraMessageType.UPLINK;
87+
}
88+
return LoraMessageType.UNKNOWN;
8689
}
8790

8891
@Override

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/providers/ProximusProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public Set<String> pathPrefixes() {
6666
*/
6767
@Override
6868
protected LoraMessageType getMessageType(final JsonObject loraMessage) {
69-
return LoraMessageType.UPLINK;
69+
70+
if (loraMessage.containsKey(FIELD_PROXIMUS_PAYLOAD)) {
71+
return LoraMessageType.UPLINK;
72+
}
73+
return LoraMessageType.UNKNOWN;
7074
}
7175

7276
@Override

adapters/lora/src/main/java/org/eclipse/hono/adapter/lora/providers/ThingsNetworkProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ public Set<String> pathPrefixes() {
7676
*/
7777
@Override
7878
protected LoraMessageType getMessageType(final JsonObject loraMessage) {
79-
return LoraMessageType.UPLINK;
79+
if (loraMessage.containsKey(FIELD_TTN_PAYLOAD_RAW)) {
80+
return LoraMessageType.UPLINK;
81+
}
82+
return LoraMessageType.UNKNOWN;
8083
}
8184

8285
@Override

adapters/lora/src/test/java/org/eclipse/hono/adapter/lora/providers/LoraProviderTestBase.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.eclipse.hono.adapter.lora.LoraCommand;
2626
import org.eclipse.hono.adapter.lora.LoraMessageType;
27+
import org.eclipse.hono.adapter.lora.UnknownLoraMessage;
2728
import org.eclipse.hono.adapter.lora.UplinkLoraMessage;
2829
import org.eclipse.hono.util.CommandEndpoint;
2930
import org.junit.jupiter.api.BeforeEach;
@@ -65,7 +66,7 @@ public abstract class LoraProviderTestBase<T extends LoraProvider> {
6566
*/
6667
protected final RoutingContext getRequestContext(final LoraMessageType type, final String... classifiers) throws IOException {
6768

68-
final Buffer message = LoraTestUtil.loadTestFile(provider.getProviderName(), LoraMessageType.UPLINK, classifiers);
69+
final Buffer message = LoraTestUtil.loadTestFile(provider.getProviderName(), type, classifiers);
6970
final HttpServerRequest request = mock(HttpServerRequest.class);
7071
final RoutingContext routingContext = mock(RoutingContext.class);
7172
when(routingContext.request()).thenReturn(request);
@@ -99,6 +100,19 @@ public void testGetMessageSucceedsForUplinkMessage() throws IOException {
99100
assertMetaDataForUplinkMessage(loraMessage);
100101
}
101102

103+
/**
104+
* Verifies that uplink messages are parsed correctly.
105+
*
106+
* @throws IOException If the file containing the example message could not be loaded.
107+
*/
108+
@Test
109+
public void testGetMessageSucceedsForUnknownMessage() throws IOException {
110+
111+
final RoutingContext request = getRequestContext(LoraMessageType.UNKNOWN);
112+
final UnknownLoraMessage loraMessage = (UnknownLoraMessage) provider.getMessage(request);
113+
assertThat(loraMessage.getType()).isEqualTo(LoraMessageType.UNKNOWN);
114+
}
115+
102116
/**
103117
* Asserts presence of common properties in an uplink message.
104118
*

0 commit comments

Comments
 (0)