use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class BinFunctionsTest method run.
public static Object run(String rule, Map<String, Object> variables) {
Context context = Context.EMPTY_CONTEXT();
StellarProcessor processor = new StellarProcessor();
Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
return processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class MedianAbsoluteDeviationTest method run.
public static Object run(String rule, Map<String, Object> variables) {
Context context = Context.EMPTY_CONTEXT();
StellarProcessor processor = new StellarProcessor();
Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
return processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class ConfigurationsUtils method setupStellarStatically.
public static void setupStellarStatically(CuratorFramework client, Optional<String> globalConfig) {
/*
In order to validate stellar functions, the function resolver must be initialized. Otherwise,
those utilities that require validation cannot validate the stellar expressions necessarily.
*/
Context.Builder builder = new Context.Builder().with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client);
if (globalConfig.isPresent()) {
builder = builder.with(Context.Capabilities.GLOBAL_CONFIG, () -> GLOBAL.deserialize(globalConfig.get())).with(Context.Capabilities.STELLAR_CONFIG, () -> GLOBAL.deserialize(globalConfig.get()));
} else {
builder = builder.with(Context.Capabilities.STELLAR_CONFIG, () -> new HashMap<>());
}
Context stellarContext = builder.build();
StellarFunctions.FUNCTION_RESOLVER().initialize(stellarContext);
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class GetProfileTest method testWithConfigAndOneGroup.
/**
* Values should be retrievable that have been stored within a 'group', with
* configuration different than current global config.
* This time put the config_override case before the non-override case.
*/
@Test
public void testWithConfigAndOneGroup() {
final int periodsPerHour = 4;
final int expectedValue = 2302;
final int hours = 2;
final long startTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(hours);
final List<Object> group = Arrays.asList("weekends");
// setup - write some measurements to be read later
final int count = hours * periodsPerHour;
ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile1").withEntity("entity1").withPeriod(startTime, periodDuration, periodUnits);
profileWriter.write(m, count, group, val -> expectedValue);
// create a variable that contains the groups to use
state.put("groups", group);
// now change the executor configuration
Context context2 = setup2();
// validate it is changed in significant way
@SuppressWarnings("unchecked") Map<String, Object> global = (Map<String, Object>) context2.getCapability(Context.Capabilities.GLOBAL_CONFIG).get();
Assert.assertEquals(global.get(PROFILER_PERIOD.getKey()), Long.toString(periodDuration2));
Assert.assertNotEquals(periodDuration, periodDuration2);
// execute - read the profile values - with config_override.
// first two override values are strings, third is deliberately a number.
String overrides = "{'profiler.client.period.duration' : '" + periodDuration + "', " + "'profiler.client.period.duration.units' : '" + periodUnits.toString() + "', " + "'profiler.client.salt.divisor' : " + saltDivisor + " }";
String expr = "PROFILE_GET('profile1', 'entity1'" + ", PROFILE_FIXED(4, 'HOURS', " + overrides + "), ['weekends'], " + overrides + ")";
@SuppressWarnings("unchecked") List<Integer> result = run(expr, List.class);
// validate - expect to read all values from the past 4 hours
Assert.assertEquals(count, result.size());
// execute - read the profile values - with (wrong) default global config values.
// No error message at this time, but returns empty results list, because
// row keys are not correctly calculated.
expr = "PROFILE_GET('profile1', 'entity1', PROFILE_FIXED(4, 'HOURS'), ['weekends'])";
result = run(expr, List.class);
// validate - expect to fail to read any values
Assert.assertEquals(0, result.size());
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class StellarAdapter method enrich.
@Override
public JSONObject enrich(CacheKey value) {
Context stellarContext = (Context) value.getConfig().getConfiguration().get(STELLAR_CONTEXT_CONF);
ConfigHandler handler = getHandler.apply(value.getConfig());
Map<String, Object> globalConfig = value.getConfig().getConfiguration();
Map<String, Object> sensorConfig = value.getConfig().getEnrichment().getConfig();
if (handler == null) {
_LOG.trace("Stellar ConfigHandler is null.");
return new JSONObject();
}
Long slowLogThreshold = null;
if (_PERF_LOG.isDebugEnabled()) {
slowLogThreshold = ConversionUtils.convert(globalConfig.getOrDefault(STELLAR_SLOW_LOG, STELLAR_SLOW_LOG_DEFAULT), Long.class);
}
// Ensure that you clone the message, because process will modify the message. If the message object is modified
// then cache misses will happen because the cache will be modified.
Map<String, Object> message = new HashMap<>(value.getValue(Map.class));
VariableResolver resolver = new MapVariableResolver(message, sensorConfig, globalConfig);
StellarProcessor processor = new StellarProcessor();
JSONObject enriched = process(message, handler, value.getField(), slowLogThreshold, processor, resolver, stellarContext);
_LOG.trace("Stellar Enrichment Success: {}", enriched);
return enriched;
}
Aggregations