-
Notifications
You must be signed in to change notification settings - Fork 3
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
Telemetry with two implementations: basic usage logging and opentelemetry.io observability #92
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4c63bfe
telemetry basics and a simple logging telemetry implementation
nls-jajuko 8050ab2
telemetry factory with log-json module
nls-jajuko ffafbb6
use GetFeatureRequest instead of FeatureType
nls-jajuko 2a54835
hook for additional config options
nls-jajuko 3d7d0d9
add missing check for collection telemetry
nls-jajuko c4c9e05
alternate telemetry with opentelemetry.io
nls-jajuko 275f97f
span scope and context
nls-jajuko 09997d4
add opentelemtry module to telemetry enabled webapp
nls-jajuko 361a7ce
additional attributes for span
nls-jajuko 46cc0a9
Update LoggingRequestTelemetry.java
nls-jajuko 6589d71
Update LoggingRequestTelemetry.java
nls-jajuko 0f90c6b
Update LoggingRequestTelemetry.java
nls-jajuko fa49dea
Update LoggingRequestTelemetry.java
nls-jajuko e91533f
Update LoggingRequestTelemetry.java
nls-jajuko 7e72c48
Update TracingRequestTelemetry.java
nls-jajuko 509d9a5
Update TracingRequestTelemetry.java
nls-jajuko 797d0cc
fixed based on feedback / add lifecycle methods to ServiceTelemetry
nls-jajuko 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
7 changes: 7 additions & 0 deletions
7
src/hakunapi-core/src/main/java/fi/nls/hakunapi/core/telemetry/RequestTelemetry.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,7 @@ | ||
package fi.nls.hakunapi.core.telemetry; | ||
|
||
public interface RequestTelemetry { | ||
|
||
public TelemetrySpan span(); | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
src/hakunapi-core/src/main/java/fi/nls/hakunapi/core/telemetry/ServiceTelemetry.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,87 @@ | ||
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 extends Closeable { | ||
|
||
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 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 { | ||
|
||
|
||
public static final NOPFeatureTypeTelemetry NOP = new NOPFeatureTypeTelemetry(); | ||
|
||
@Override | ||
public TelemetrySpan span() { | ||
return TelemetrySpan.NOP; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
} |
91 changes: 91 additions & 0 deletions
91
src/hakunapi-core/src/main/java/fi/nls/hakunapi/core/telemetry/TelemetryConfigParser.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,91 @@ | ||
package fi.nls.hakunapi.core.telemetry; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import fi.nls.hakunapi.core.FeatureServiceConfig; | ||
import fi.nls.hakunapi.core.FeatureType; | ||
import fi.nls.hakunapi.core.config.HakunaConfigParser; | ||
|
||
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"; | ||
protected static final String TELEMETRY_FIELDS_HEADER = "telemetry.fields.%s.header"; | ||
protected static final String TELEMETRY_COLLECTIONS = "telemetry.collections"; | ||
protected static final String TELEMETRY_COLLECTIONS_NAME = "telemetry.collections.%s.name"; | ||
protected static final String TELEMETRY_COLLECTIONS_WILDCARD = "*"; | ||
|
||
|
||
public static ServiceTelemetry parse(FeatureServiceConfig service, HakunaConfigParser parser) { | ||
|
||
String telemetryId = parser.get(TELEMETRY_MODE, TELEMETRY_LOG_NOP); | ||
if (TELEMETRY_LOG_NOP.equals(telemetryId)) { | ||
LOG.info("Using no-op telemetry"); | ||
return ServiceTelemetry.NOP; | ||
} | ||
|
||
ServiceTelemetry telemetry = TelemetryProvider.getTelemetries().get(telemetryId); | ||
if(telemetry==null) { | ||
LOG.info("Telemetry not found "+telemetryId+". Using no-op telemetry"); | ||
return ServiceTelemetry.NOP; | ||
} | ||
|
||
LOG.info("Using telemetry "+telemetry.getClass().getName()); | ||
|
||
String usageLogName = parser.get(TELEMETRY_LOGGER); | ||
telemetry.setName(usageLogName); | ||
|
||
String[] headers = parser.getMultiple(TELEMETRY_FIELDS); | ||
|
||
Map<String, String> headersMap = new HashMap<>(); | ||
telemetry.setHeaders(headersMap); | ||
if (headers != null) { | ||
for (String header : headers) { | ||
String lookup = parser.get(String.format(TELEMETRY_FIELDS_HEADER, header), header); | ||
headersMap.put(header, lookup); | ||
} | ||
} | ||
|
||
String collectionsWildcard = parser.get(TELEMETRY_COLLECTIONS); | ||
if (collectionsWildcard == null) { | ||
return ServiceTelemetry.NOP; | ||
|
||
} else if (service != null && TELEMETRY_COLLECTIONS_WILDCARD.equals(collectionsWildcard)) { | ||
Collection<FeatureType> collections = service.getCollections(); | ||
|
||
// defaults to logging ft.name as ft.name | ||
Map<String, String> collectionsMap = collections.stream() | ||
.collect(Collectors.toMap(FeatureType::getName, FeatureType::getName)); | ||
telemetry.setCollections(collectionsMap); | ||
|
||
} else { | ||
String[] collections = parser.getMultiple(TELEMETRY_COLLECTIONS); | ||
Map<String, String> collectionsMap = new HashMap<>(); | ||
telemetry.setCollections(collectionsMap); | ||
|
||
// defaults to logging ft.name as ft.name with optional rename | ||
if (collections != null) { | ||
for (String collection : collections) { | ||
String lookup = parser.get(String.format(TELEMETRY_COLLECTIONS_NAME, collection), collection); | ||
collectionsMap.put(collection, lookup); | ||
} | ||
} | ||
} | ||
|
||
telemetry.parse(service,parser); | ||
|
||
return telemetry; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/hakunapi-core/src/main/java/fi/nls/hakunapi/core/telemetry/TelemetryFactory.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,6 @@ | ||
package fi.nls.hakunapi.core.telemetry; | ||
|
||
public interface TelemetryFactory { | ||
|
||
public ServiceTelemetry createServiceTelemetry(); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/hakunapi-core/src/main/java/fi/nls/hakunapi/core/telemetry/TelemetryProvider.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,24 @@ | ||
package fi.nls.hakunapi.core.telemetry; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
|
||
|
||
public class TelemetryProvider { | ||
|
||
private static final ServiceLoader<TelemetryFactory> LOADER = | ||
ServiceLoader.load(TelemetryFactory.class); | ||
|
||
public static Map<String, ServiceTelemetry> getTelemetries() { | ||
LOADER.reload(); | ||
|
||
final Map<String, ServiceTelemetry> telemetries = new HashMap<>(); | ||
|
||
for (TelemetryFactory factory : LOADER) { | ||
ServiceTelemetry telemetry = factory.createServiceTelemetry(); | ||
telemetries.put(telemetry.getId(), telemetry); | ||
} | ||
return telemetries; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/hakunapi-core/src/main/java/fi/nls/hakunapi/core/telemetry/TelemetrySpan.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,34 @@ | ||
package fi.nls.hakunapi.core.telemetry; | ||
|
||
import fi.nls.hakunapi.core.request.WriteReport; | ||
|
||
public interface TelemetrySpan extends AutoCloseable { | ||
|
||
void counts(int count); | ||
void counts(WriteReport report); | ||
void put(String key, String value); | ||
|
||
public void close(); | ||
|
||
// No op implementation | ||
TelemetrySpan NOP = new TelemetrySpan() { | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public void counts(int count) { | ||
} | ||
|
||
@Override | ||
public void counts(WriteReport report) { | ||
} | ||
|
||
@Override | ||
public void put(String key, String value) { | ||
} | ||
|
||
}; | ||
|
||
} |
Oops, something went wrong.
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.
Took a while to understand how this works, but it should.