use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.
the class Metrics method initializeMetrics.
private void initializeMetrics(Set<Class> metricsClassSet, Context context) {
for (Class clazz : metricsClassSet) {
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
Class[] parameters = constructor.getParameterTypes();
try {
Metric metric;
if (parameters.length == 0) {
metric = (Metric) constructor.newInstance();
classNameToMetric.put(metric.getClass().getName(), metric);
} else if (parameters.length == 1 && parameters[0].equals(Context.class)) {
metric = (Metric) constructor.newInstance(context);
classNameToMetric.put(metric.getClass().getName(), metric);
} else {
logger.warn("Can't initialize the Metric, please initialize it and add it to Metrics: {}", constructor);
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
logger.error("Fail to initialize the metric instance: {}", constructor);
}
}
}
}
use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.
the class TelemetrySystem method refreshBindingMap.
/**
* Refresh the bindingMap who notes user's authorization.
* If a new map is added during the game, the authorization functionality could also be used.
*/
private void refreshBindingMap() {
Map<String, Metric> classNameToMetricMap = metrics.getClassNameToMetric();
for (Metric metric : classNameToMetricMap.values()) {
TelemetryCategory telemetryCategory = metric.getClass().getAnnotation(TelemetryCategory.class);
if (!bindingMap.containsKey(telemetryCategory.id())) {
bindingMap.put(telemetryCategory.id(), config.getTelemetryConfig().isTelemetryEnabled());
}
List<String> fields = metric.createTelemetryFieldList();
for (String telemetryField : fields) {
if (!bindingMap.containsKey(telemetryField)) {
bindingMap.put(telemetryField, config.getTelemetryConfig().isTelemetryEnabled());
}
}
}
}
use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.
the class ModulesJsonProvider method writeTo.
@Override
public void writeTo(JsonGenerator generator, ILoggingEvent iLoggingEvent) throws IOException {
TelemetryLogstashAppender appender = TelemetryUtils.fetchTelemetryLogstashAppender();
Context context = appender.getGameContext();
if (context != null) {
Metrics metrics = context.get(Metrics.class);
Optional<Metric> optional = metrics.getMetric(ModulesMetric.class);
if (optional.isPresent()) {
Metric modulesMetric = optional.get();
Map<String, ?> map = modulesMetric.createTelemetryFieldToValue();
Map<String, String> stringMap = TelemetryUtils.toStringMap(map);
JsonWritingUtils.writeMapStringFields(generator, getFieldName(), stringMap);
}
}
}
use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.
the class TelemetryScreen method refreshContent.
private void refreshContent() {
ColumnLayout mainLayout = new ColumnLayout();
mainLayout.setHorizontalSpacing(8);
mainLayout.setVerticalSpacing(8);
fetchTelemetryCategoriesFromEngineOnlyEnvironment();
for (Map.Entry<TelemetryCategory, Class> telemetryCategory : telemetryCategories.entrySet()) {
Class metricClass = telemetryCategory.getValue();
Optional<Metric> optional = metrics.getMetric(metricClass);
if (optional.isPresent()) {
Metric metric = optional.get();
Map<String, ?> map = metric.createTelemetryFieldToValue();
if (map != null) {
addTelemetrySection(telemetryCategory.getKey(), mainLayout, map);
}
}
}
ScrollableArea area = find("area", ScrollableArea.class);
if (area != null) {
area.setContent(mainLayout);
}
}
use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.
the class TelemetryUtils method fetchMetricAndSend.
/**
* Fetch metric in {@link org.terasology.engine.context.Context} and send to the server.
* This method could be used in the modules.
* @param context The game context
* @param metricClass The class of the metric which we want to track
* @param nameSpace The name the class tracking this metric.
*/
public static void fetchMetricAndSend(Context context, Class metricClass, String nameSpace) {
Emitter emitter = context.get(Emitter.class);
Metrics metrics = context.get(Metrics.class);
TelemetryConfiguration telemetryConfiguration = context.get(TelemetryConfiguration.class);
if (emitter != null && metrics != null && telemetryConfiguration != null) {
Optional<Metric> metricOptional = metrics.getMetric(metricClass);
if (metricOptional.isPresent()) {
Metric metric = metricOptional.get();
Optional<Unstructured> unstructuredOptional = metric.getUnstructuredMetric();
if (unstructuredOptional.isPresent()) {
Unstructured unstructured = unstructuredOptional.get();
trackMetric(emitter, nameSpace, unstructured, metric, telemetryConfiguration);
}
}
} else {
logger.error("Emitter or metrics or telemetryConfiguration is not in context");
}
}
Aggregations