public void testMonospaceTimeWeightedDeltaEnricher() { @SuppressWarnings("unchecked") YamlTimeWeightedDeltaEnricher<Integer> delta = producer.enrichers().add(EnricherSpec.create(YamlTimeWeightedDeltaEnricher.class) .configure(YamlTimeWeightedDeltaEnricher.PRODUCER, producer) .configure(YamlTimeWeightedDeltaEnricher.SOURCE_SENSOR, intSensor)
if (!e.enrichers().isEmpty()) { out.append(currentIndentation+tab+tab+"Enrichers:\n"); for (Enricher enricher : e.enrichers()) {
sensors().set(DOCKER_CONTAINER_FABRIC, fabric); hosts.enrichers().add(Enrichers.builder() .aggregating(MachineAttributes.CPU_USAGE) .computingAverage() .valueToReportIfNoSensors(0d) .build()); hosts.enrichers().add(Enrichers.builder() .aggregating(DOCKER_CONTAINER_COUNT) .computingSum() .configure(ContainerHeadroomEnricher.CONTAINER_HEADROOM, headroom) .configure(ContainerHeadroomEnricher.CONTAINER_HEADROOM_PERCENTAGE, headroomPercent)); hosts.enrichers().add(Enrichers.builder() .propagating( ContainerHeadroomEnricher.DOCKER_CONTAINER_CLUSTER_COLD,
public void testVariableTimeWeightedDeltaEnricher() { @SuppressWarnings("unchecked") YamlTimeWeightedDeltaEnricher<Integer> delta = producer.enrichers().add(EnricherSpec.create(YamlTimeWeightedDeltaEnricher.class) .configure(YamlTimeWeightedDeltaEnricher.PRODUCER, producer) .configure(YamlTimeWeightedDeltaEnricher.SOURCE_SENSOR, intSensor)
public static void connectWebAppServerPolicies(Entity entity, Duration windowPeriod) { entity.enrichers().add(EnricherSpec.create(TimeWeightedDeltaEnricher.class) .configure("producer", entity) .configure("source", REQUEST_COUNT) .configure("target", REQUESTS_PER_SECOND_LAST) .configure("unitMillis", 1000)); if (windowPeriod!=null) { entity.enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", entity) .configure("source", REQUESTS_PER_SECOND_LAST) .configure("target", REQUESTS_PER_SECOND_IN_WINDOW) .configure("timePeriod", windowPeriod)); } entity.enrichers().add(EnricherSpec.create(TimeFractionDeltaEnricher.class) .configure("producer", entity) .configure("source", TOTAL_PROCESSING_TIME) .configure("target", PROCESSING_TIME_FRACTION_LAST) .configure("durationPerOrigUnit", Duration.millis(1))); if (windowPeriod!=null) { entity.enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", entity) .configure("source", PROCESSING_TIME_FRACTION_LAST) .configure("target", PROCESSING_TIME_FRACTION_IN_WINDOW) .configure("timePeriod", windowPeriod)); } }
@Test public void testAggregatingExcludingBlankString() { group.addMember(entity); group.addMember(entity2); group.enrichers().add(Enrichers.builder() .aggregating(STR1) .publishing(SET1) .fromMembers() .excludingBlank() .computing(new Function<Collection<?>, Set<Object>>() { @Override public Set<Object> apply(Collection<?> input) { // accept null values, so don't use ImmutableSet return (input == null) ? ImmutableSet.<Object>of() : MutableSet.<Object>copyOf(input); }}) .build()); entity.sensors().set(STR1, "1"); entity2.sensors().set(STR1, "2"); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("1", "2")); entity.sensors().set(STR1, "3"); entity2.sensors().set(STR1, null); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("3")); entity.sensors().set(STR1, ""); entity2.sensors().set(STR1, "4"); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("4")); }
public static void configureEnrichers(SubnetTier subnetTier, Entity entity) { for (AttributeSensor sensor : Iterables.filter(entity.getEntityType().getSensors(), AttributeSensor.class)) { if ((DockerUtils.URL_SENSOR_NAMES.contains(sensor.getName()) || sensor.getName().endsWith(".url") || URI.class.isAssignableFrom(sensor.getType())) && !DockerUtils.BLACKLIST_URL_SENSOR_NAMES.contains(sensor.getName())) { AttributeSensor<String> target = DockerUtils.<String>mappedSensor(sensor); entity.enrichers().add(subnetTier.uriTransformingEnricher( EntityAndAttribute.create(entity, sensor), target)); Set<Hint<?>> hints = RendererHints.getHintsFor(sensor); for (Hint<?> hint : hints) { RendererHints.register(target, (NamedActionWithUrl) hint); } LOG.debug("Mapped URL sensor: entity={}, origin={}, mapped={}", new Object[] {entity, sensor.getName(), target.getName()}); } else if (sensor.getName().matches("docker\\.port\\.[0-9]+") || PortAttributeSensorAndConfigKey.class.isAssignableFrom(sensor.getClass())) { AttributeSensor<String> target = DockerUtils.mappedPortSensor(sensor); entity.enrichers().add(subnetTier.hostAndPortTransformingEnricher( EntityAndAttribute.create(entity, sensor), target)); LOG.debug("Mapped port sensor: entity={}, origin={}, mapped={}", new Object[] {entity, sensor.getName(), target.getName()}); } } }
@Override protected Entity createNode(Location loc, Map<?, ?> flags) { MySqlNode node = (MySqlNode) super.createNode(loc, flags); if (!MySqlClusterUtils.IS_MASTER.apply(node)) { ServiceNotUpLogic.updateNotUpIndicator(node, MySqlSlave.SLAVE_HEALTHY, "Replication not started"); addFeed(FunctionFeed.builder() .entity(node) .period(Duration.FIVE_SECONDS) .poll(FunctionPollConfig.forSensor(MySqlSlave.SLAVE_HEALTHY) .callable(new SlaveStateCallable(node)) .checkSuccess(StringPredicates.isNonBlank()) .onSuccess(new SlaveStateParser(node)) .setOnFailure(false) .description("Polls SHOW SLAVE STATUS")) .build()); node.enrichers().add(Enrichers.builder().updatingMap(Attributes.SERVICE_NOT_UP_INDICATORS) .from(MySqlSlave.SLAVE_HEALTHY) .computing(Functionals.ifNotEquals(true).value("Slave replication status is not healthy") ) .build()); } return node; }
@Test public void testAggregatesNewMembersOfGroup() { BasicGroup group = app.createAndManageChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); log.debug("created {} and the entities it will contain {} {}", new Object[] {group, p1, p2}); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .defaultValueForUnreportedSensors(0) .valueToReportIfNoSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 0); group.addMember(p1); p1.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); group.addMember(p2); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(group, target, 3); group.removeMember(p2); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); }
@Test public void testAppliesFilterWhenAggregatingMembersOfGroup() { BasicGroup group = app.createAndManageChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); group.addMember(p1); group.addMember(p2); p1.sensors().set(intSensor, 1); p2.sensors().set(intSensor, 2); p3.sensors().set(intSensor, 4); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .entityFilter(Predicates.equalTo((Entity)p1)) .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); group.addMember(p3); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", SHORT_WAIT_MS), group, target, 1); }
@Test public void testVariableTimeWeightedDeltaEnricher() { AttributeSensor<Double> deltaSensor = new BasicAttributeSensor<Double>(Double.class, "per second delta delta sensor"); @SuppressWarnings("unchecked") TimeWeightedDeltaEnricher<Integer> delta = producer.enrichers().add(EnricherSpec.create(TimeWeightedDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor) .configure("unitMillis", 1000)); delta.onEvent(intSensor.newEvent(producer, 0), 1000); delta.onEvent(intSensor.newEvent(producer, 0), 3000); assertEquals(producer.getAttribute(deltaSensor), 0d); delta.onEvent(intSensor.newEvent(producer, 3), 6000); assertEquals(producer.getAttribute(deltaSensor), 1d); delta.onEvent(intSensor.newEvent(producer, 7), 8000); assertEquals(producer.getAttribute(deltaSensor), 2d); delta.onEvent(intSensor.newEvent(producer, 12), 8500); assertEquals(producer.getAttribute(deltaSensor), 10d); delta.onEvent(intSensor.newEvent(producer, 15), 10500); assertEquals(producer.getAttribute(deltaSensor), 1.5d); }
@Test public void testAggregatingRespectsUnchanged() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .<Object>publishing(LONG1) .fromMembers() .computing(new Function<Iterable<Integer>, Object>() { @Override public Object apply(Iterable<Integer> input) { if (input != null && Iterables.contains(input, 123)) { return Enrichers.sum(input, 0, 0, new TypeToken<Integer>(){}); } else { return Entities.UNCHANGED; } }}) .build()); entity.sensors().set(NUM1, 123); EntityAsserts.assertAttributeEqualsEventually(group, LONG1, Long.valueOf(123)); entity.sensors().set(NUM1, 987654); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"), group, LONG1, Long.valueOf(123)); }
protected void registerPortMappings(KubernetesSshMachineLocation machine, Entity entity, Service service) { PortForwardManager portForwardManager = (PortForwardManager) getManagementContext().getLocationRegistry() .getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC); List<ServicePort> ports = service.getSpec().getPorts(); String publicHostText = ((SshMachineLocation) machine).getSshHostAndPort().getHostText(); LOG.debug("Recording port-mappings for container {} of {}: {}", new Object[]{machine, this, ports}); for (ServicePort port : ports) { String protocol = port.getProtocol(); Integer targetPort = port.getTargetPort().getIntVal(); if (!"TCP".equalsIgnoreCase(protocol)) { LOG.debug("Ignoring port mapping {} for {} because only TCP is currently supported", port, machine); } else if (targetPort == null) { LOG.debug("Ignoring port mapping {} for {} because targetPort.intValue is null", port, machine); } else if (port.getNodePort() == null) { LOG.debug("Ignoring port mapping {} to {} because port.getNodePort() is null", targetPort, machine); } else { portForwardManager.associate(publicHostText, HostAndPort.fromParts(publicHostText, port.getNodePort()), machine, targetPort); AttributeSensor<Integer> sensor = Sensors.newIntegerSensor("kubernetes." + Strings.maybeNonBlank(port.getName()).or(targetPort.toString()) + ".port"); entity.sensors().set(sensor, targetPort); } } entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class).configure(OnPublicNetworkEnricher.MAP_MATCHING, "kubernetes.[a-zA-Z0-9][a-zA-Z0-9-_]*.port")); }
@Test public void testAggregatingExcludingNull() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .publishing(SET1) .fromMembers() .excludingBlank() .computing(new Function<Collection<?>, Set<Object>>() { @Override public Set<Object> apply(Collection<?> input) { // accept null values, so don't use ImmutableSet return (input == null) ? ImmutableSet.<Object>of() : MutableSet.<Object>copyOf(input); }}) .build()); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of()); entity.sensors().set(NUM1, 1); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of(1)); entity.sensors().set(NUM1, null); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of()); entity.sensors().set(NUM1, 2); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of(2)); }
@BeforeMethod(alwaysRun=true) @SuppressWarnings("unchecked") @Override public void setUp() throws Exception { super.setUp(); producer = app.addChild(EntitySpec.create(TestEntity.class)); intSensor = new BasicAttributeSensor<Integer>(Integer.class, "int sensor"); deltaSensor = new BasicAttributeSensor<Integer>(Integer.class, "delta sensor"); avgSensor = new BasicAttributeSensor<Double>(Double.class, "avg sensor"); producer.enrichers().add(EnricherSpec.create(DeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor)); averager = producer.enrichers().add(EnricherSpec.create(RollingMeanEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor) .configure("windowSize", 4)); }
@Test public void testCalculatesFractions() { @SuppressWarnings("unchecked") TimeFractionDeltaEnricher<Integer> enricher = producer.enrichers().add(EnricherSpec.create(TimeFractionDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", fractionSensor) .configure("durationPerOrigUnit", Duration.millis(1))); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 0, 1000000L)); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 0, 1001000L)); assertEquals(producer.getAttribute(fractionSensor), 0d); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 100, 1002000L)); assertEquals(producer.getAttribute(fractionSensor), 0.1d, PRECISION); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 300, 1003000L)); assertEquals(producer.getAttribute(fractionSensor), 0.2d, PRECISION); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 2300, 1004000L)); assertEquals(producer.getAttribute(fractionSensor), 2d, PRECISION); }
@Test public void testAggregatesExistingMembersOfGroup() { BasicGroup group = app.addChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = group.addChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = group.addChild(EntitySpec.create(TestEntity.class)); group.addMember(p1); group.addMember(p2); p1.sensors().set(intSensor, 1); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(group, target, 3); group.removeMember(p2); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); }
@BeforeMethod(alwaysRun=true) @SuppressWarnings("unchecked") @Override public void setUp() throws Exception { super.setUp(); producer = app.addChild(EntitySpec.create(TestEntity.class)); intSensor = new BasicAttributeSensor<Integer>(Integer.class, "int sensor"); deltaSensor = new BasicAttributeSensor<Integer>(Integer.class, "delta sensor"); avgSensor = new BasicAttributeSensor<Double>(Double.class, "avg sensor"); producer.enrichers().add(EnricherSpec.create(DeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor)); averager = producer.enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", producer) .configure("source", deltaSensor) .configure("target", avgSensor) .configure("timePeriod", timePeriod)); }
@SuppressWarnings("unchecked") @BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); producer = app.addChild(EntitySpec.create(BasicEntity.class)); intSensor = new BasicAttributeSensor<Integer>(Integer.class, "int sensor"); deltaSensor = new BasicAttributeSensor<Double>(Double.class, "delta sensor"); avgSensor = new BasicAttributeSensor<Double>(Double.class, "avg sensor"); delta = producer.enrichers().add(EnricherSpec.create(YamlTimeWeightedDeltaEnricher.class) .configure(YamlTimeWeightedDeltaEnricher.PRODUCER, producer) .configure(YamlTimeWeightedDeltaEnricher.SOURCE_SENSOR, intSensor) .configure(YamlTimeWeightedDeltaEnricher.TARGET_SENSOR, deltaSensor)); averager = producer.enrichers().add(EnricherSpec.create(YamlRollingTimeWindowMeanEnricher.class) .configure(YamlRollingTimeWindowMeanEnricher.PRODUCER, producer) .configure(YamlRollingTimeWindowMeanEnricher.SOURCE_SENSOR, deltaSensor) .configure(YamlRollingTimeWindowMeanEnricher.TARGET_SENSOR, avgSensor) .configure(YamlRollingTimeWindowMeanEnricher.WINDOW_DURATION, timePeriod)); }