Skip to content

Commit

Permalink
fixed based on feedback / add lifecycle methods to ServiceTelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
nls-jajuko committed Jan 23, 2024
1 parent 509d9a5 commit 797d0cc
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,73 @@
package fi.nls.hakunapi.core.telemetry;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;

import fi.nls.hakunapi.core.FeatureServiceConfig;
import fi.nls.hakunapi.core.config.HakunaConfigParser;
import fi.nls.hakunapi.core.request.GetFeatureRequest;

public interface ServiceTelemetry {
public interface ServiceTelemetry extends Closeable {

public default String getId() {
return "nop";
}
public String getId();

public void setName(String name);
public String getName();
public void setHeaders(Map<String, String> headersMap) ;
public void setCollections(Map<String, String> collectionsMap);

public default void setName(String name) {};
public default String getName() { return "telemetry"; };
public default void setHeaders(Map<String, String> headersMap) {};
public default void setCollections(Map<String, String> collectionsMap) {};
public default void parse(FeatureServiceConfig service, HakunaConfigParser parser) {}
public void parse(FeatureServiceConfig service, HakunaConfigParser parser);
public void start();

public RequestTelemetry forRequest(GetFeatureRequest r);


// No op implementation
static ServiceTelemetry NOP = new ServiceTelemetry() {

String name;
@Override
public RequestTelemetry forRequest(GetFeatureRequest r) {
return NOPFeatureTypeTelemetry.NOP;

}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

@Override
public void setHeaders(Map<String, String> headersMap) {
}

@Override
public String getId() {
return "nop";
}

@Override
public void setCollections(Map<String, String> collectionsMap) {
}

@Override
public void parse(FeatureServiceConfig service, HakunaConfigParser parser) {
}

@Override
public void start() {
}

@Override
public void close() throws IOException {
}

};

public static class NOPFeatureTypeTelemetry implements RequestTelemetry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class TelemetryConfigParser {
private static final Logger LOG = LoggerFactory.getLogger(TelemetryConfigParser.class);

protected static final String TELEMETRY_LOG_NOP = "NOP";
public static final String TELEMETRY_MODULE_PROP = "telemetry.module.%s";
protected static final String TELEMETRY_MODE = "telemetry.mode";
protected static final String TELEMETRY_LOGGER = "telemetry.logger";
protected static final String TELEMETRY_FIELDS = "telemetry.fields";
Expand Down Expand Up @@ -60,7 +61,7 @@ public static ServiceTelemetry parse(FeatureServiceConfig service, HakunaConfigP
if (collectionsWildcard == null) {
return ServiceTelemetry.NOP;

} else if (service != null && "*".equals(collectionsWildcard)) {
} else if (service != null && TELEMETRY_COLLECTIONS_WILDCARD.equals(collectionsWildcard)) {
Collection<FeatureType> collections = service.getCollections();

// defaults to logging ft.name as ft.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public interface TelemetrySpan extends AutoCloseable {

default void counts(int count) {};
default void counts(WriteReport report) {};
default void put(String key, String value) {};
void counts(int count);
void counts(WriteReport report);
void put(String key, String value);

public void close();

Expand All @@ -15,7 +15,18 @@ public interface TelemetrySpan extends AutoCloseable {

@Override
public void close() {
}

@Override
public void counts(int count) {
}

@Override
public void counts(WriteReport report) {
}

@Override
public void put(String key, String value) {
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public void contextInitialized(ServletContextEvent sce) {

ServiceTelemetry telemetry = TelemetryConfigParser.parse(service, parser);
service.setTelemetry(telemetry);
toClose.add(telemetry);

telemetry.start();


LOG.info("Starting OGC API Features service with collections: {}", collections);
sce.getServletContext().setAttribute("hakunaService", service);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package fi.nls.hakunapi.telemetry;

import static fi.nls.hakunapi.core.telemetry.TelemetryConfigParser.TELEMETRY_MODULE_PROP;

import java.io.IOException;
import java.time.Duration;
import java.util.Map;

import org.slf4j.Logger;
Expand All @@ -13,22 +17,35 @@
import fi.nls.hakunapi.core.telemetry.ServiceTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;

public class TracingServiceTelemetry implements ServiceTelemetry {

protected String name;
private static final long METRIC_EXPORT_INTERVAL_MS = 800L;

protected String name;
protected Logger log;
protected Map<String, String> headersMap;
protected Map<String, String> collectionsMap;

protected OpenTelemetry openTelemetry;
protected Tracer tracer;


protected long exportIntervalMs;

private PeriodicMetricReader periodicReader;
private SdkMeterProvider meterProvider;
private SdkTracerProvider tracerProvider;

public TracingServiceTelemetry() {
openTelemetry = TracingConfiguration.initOpenTelemetry();
}

@Override
public String getId() {
return "opentelemetry";
Expand All @@ -46,7 +63,6 @@ public void setName(String name) {
return;
}
log = LoggerFactory.getLogger(name);
tracer = openTelemetry.getTracer(name);
}

public void setHeaders(Map<String, String> headersMap) {
Expand All @@ -68,19 +84,55 @@ public RequestTelemetry forRequest(GetFeatureRequest request) {
if (request == null) {
return NOPFeatureTypeTelemetry.NOP;
}

FeatureType ft = request.getCollections().get(0).getFt();
if(!collectionsMap.containsKey(ft.getName())) {
if (!collectionsMap.containsKey(ft.getName())) {
return NOPFeatureTypeTelemetry.NOP;
}
return new TracingRequestTelemetry(request, log, headersMap, tracer, openTelemetry);
}

@Override
public void parse(FeatureServiceConfig service, HakunaConfigParser parser) {
// additional configuration

String intervalValue = parser.get(String.format(TELEMETRY_MODULE_PROP, "opentelemetry.export.interval"));

exportIntervalMs = intervalValue != null ? Long.valueOf(intervalValue) : METRIC_EXPORT_INTERVAL_MS;

}



@Override
public void close() throws IOException {
meterProvider.close();
tracerProvider.close();
}

@Override
public void start() {
initOpenTelemetry(exportIntervalMs);
tracer = openTelemetry.getTracer(name);
}

/**
* Initializes an OpenTelemetry SDK with a logging exporter and a
* SimpleSpanProcessor.
*
* @return A ready-to-use {@link OpenTelemetry} instance.
*/
public void initOpenTelemetry(long exportInterval) {
// Create an instance of PeriodicMetricReader and configure it
// to export via the logging exporter
periodicReader = PeriodicMetricReader.builder(LoggingMetricExporter.create())
.setInterval(Duration.ofMillis(exportInterval)).build();

// This will be used to create instruments
meterProvider = SdkMeterProvider.builder().registerMetricReader(periodicReader).build();

// Tracer provider configured to export spans with SimpleSpanProcessor using
// the logging exporter.
tracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create())).build();
openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(meterProvider).setTracerProvider(tracerProvider)
.buildAndRegisterGlobal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void testReadConfig() throws IOException {
HakunaConfigParser parser = new HakunaConfigParser(props);

ServiceTelemetry fst = TelemetryConfigParser.parse(null, parser);
fst.start();

assertTrue(fst instanceof TracingServiceTelemetry);

Expand All @@ -60,6 +61,8 @@ public void testReadConfig() throws IOException {
// TODO Auto-generated catch block
e.printStackTrace();
}

fst.close();
}

FeatureType ft = new FeatureType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fi.nls.hakunapi.telemetry;

import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
Expand Down Expand Up @@ -39,10 +40,6 @@ public String getName() {
@Override
public void setName(String name) {
this.name = name;
if (name == null) {
return;
}
log = LoggerFactory.getLogger(name);
}

public void setHeaders(Map<String, String> headersMap) {
Expand Down Expand Up @@ -74,9 +71,17 @@ public RequestTelemetry forRequest(GetFeatureRequest request) {

@Override
public void parse(FeatureServiceConfig service, HakunaConfigParser parser) {
// additional configuration
}



@Override
public void close() throws IOException {
}

@Override
public void start() {
if(name==null) {
return;
}
log = LoggerFactory.getLogger(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void testReadConfig() throws IOException {
HakunaConfigParser parser = new HakunaConfigParser(props);

ServiceTelemetry fst = TelemetryConfigParser.parse(null, parser);
fst.start();

assertTrue(fst instanceof LoggingServiceTelemetry);

Expand All @@ -59,6 +60,8 @@ public void testReadConfig() throws IOException {
// TODO Auto-generated catch block
e.printStackTrace();
}

fst.close();
}

FeatureType ft = new FeatureType() {
Expand Down

0 comments on commit 797d0cc

Please sign in to comment.