@Override protected String getDefaultUniqueTag() { if (publishing==null) return null; return "combiner:"+publishing.getName(); } @Override
public static AttributeSensor<String> mappedPortSensor(AttributeSensor source) { return Sensors.newStringSensor(MAPPED + "." + source.getName(), source.getDescription() + " (Docker mapping)"); } public static AttributeSensor<String> endpointSensor(AttributeSensor source) {
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()}); } } }
public static <T> AttributeSensor<T> newSensorRenamed(String newName, AttributeSensor<T> sensor) { return new BasicAttributeSensor<T>(sensor.getTypeToken(), newName, sensor.getDescription()); }
public <T> T updateWithoutPublishing(AttributeSensor<T> attribute, T newValue) { if (log.isTraceEnabled()) { Object oldValue = getValue(attribute); if (!Objects.equal(oldValue, newValue != null)) { log.trace("setting attribute {} to {} (was {}) on {}", new Object[] {attribute.getName(), newValue, oldValue, entity}); } else { log.trace("setting attribute {} to {} (unchanged) on {}", new Object[] {attribute.getName(), newValue, this}); } } T oldValue = update(attribute.getNameParts(), newValue); return (isNull(oldValue)) ? null : oldValue; }
String chefAttributeName = attribute.getKey(); AttributeSensor<?> sensor = attribute.getValue(); log.trace("Finding value for attribute sensor " + sensor.getName()); entity.getDisplayName(), Joiner.on('.').join(prefixedPath), sensor.getName(), e.getMessage()}); throw Throwables.propagate(e); entity.getDisplayName(), Joiner.on('.').join(prefixedPath), sensor.getName(), elementForSensor.getAsString()}); break; entity.sensors().set((AttributeSensor)sensor, TypeCoercions.coerce(elementForSensor.getAsString(), sensor.getTypeToken())); } else { log.debug("Entity {}: no Chef attribute matching {}; setting sensor {} to null", new Object[]{ entity.getDisplayName(), chefAttributeName, sensor.getName()}); entity.sensors().set(sensor, null);
@SuppressWarnings("unchecked") public <T> T getValue(AttributeSensor<T> sensor) { return (T) TypeCoercions.coerce(getValue(sensor.getNameParts()), sensor.getType()); }
public static AttributeSensor<String> endpointSensor(AttributeSensor source) { List<String> name = Lists.transform(source.getNameParts(), new Function<String, String>() { @Override public String apply(@Nullable String input) { String target = PORT; if (input.equals(target)) return ENDPOINT; if (input.endsWith(target)) { return input.replace(target, ENDPOINT); } target = Strings.toInitialCapOnly(PORT); if (input.endsWith(target)) { return input.replace(target, Strings.toInitialCapOnly(ENDPOINT)); } return input; } }); if (!name.contains(ENDPOINT)) name.add(ENDPOINT); return Sensors.newStringSensor(Joiner.on(".").join(name), source.getDescription() + " (Docker mapping)"); }
public AttributeWhenReadyTestWorker(TestApplication parent, AttributeSensor<String> sensor, BrooklynDslDeferredSupplier<?> dsl) { super(parent, dsl, sensor.getType()); this.sensor = sensor; }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Function<? super Collection<S>, ? extends T> get() { // relies on TypeCoercion of result from Number to T, and type erasure for us to get away with it! return new ComputingSum((Number)defaultValueForUnreportedSensors, (Number)valueToReportIfNoSensors, publishing.getTypeToken()); } };
protected AttributeSensor<?> getAttributeKey(String key) { AttributeSensor<?> result=null; if (attributeKeys!=null) { result = attributeKeys.get(key); if (result!=null && !LEGACY_KEY_DESCRIPTION.equals(result.getDescription())) return result; } AttributeSensor<?> resultStatic = (AttributeSensor<?>) getStaticSensorKeys().get(key); if (resultStatic!=null) return resultStatic; if (result!=null) return result; // see notes on legacy config key log.warn("Sensor "+key+": "+LEGACY_KEY_DESCRIPTION); return Sensors.newSensor(Object.class, key, LEGACY_KEY_DESCRIPTION); }
@Override @SuppressWarnings("unchecked") protected void addCustoms(RebindContext rebindContext, EntityMemento memento) { for (ConfigKey<?> key : memento.getDynamicConfigKeys()) { entity.getMutableEntityType().addConfigKey(key); } for (Effector<?> eff : memento.getEffectors()) { entity.getMutableEntityType().addEffector(eff); } for (Map.Entry<AttributeSensor<?>, Object> entry : memento.getAttributes().entrySet()) { try { AttributeSensor<?> key = entry.getKey(); Object value = entry.getValue(); @SuppressWarnings("unused") // just to ensure we can load the declared type? or maybe not needed Class<?> type = (key.getType() != null) ? key.getType() : rebindContext.loadClass(key.getTypeName()); entity.sensors().setWithoutPublishing((AttributeSensor<Object>)key, value); } catch (Exception e) { LOG.warn("Error adding custom sensor "+entry+" when rebinding "+entity+" (rethrowing): "+e); throw Exceptions.propagate(e); } } setParent(rebindContext, memento); addChildren(rebindContext, memento); addMembers(rebindContext, memento); addLocations(rebindContext, memento); }
@Test public void canGetAttributeByNameParts() { // Initially null assertNull(entityImpl.getAttributeByNameParts(COLOR.getNameParts())); // Once set, returns val entity.sensors().set(COLOR, "red"); assertEquals(entityImpl.getAttributeByNameParts(COLOR.getNameParts()), "red"); } }
public void remove(AttributeSensor<?> attribute) { BrooklynLogging.log(log, BrooklynLogging.levelDebugOrTraceIfReadOnly(entity), "removing attribute {} on {}", attribute.getName(), entity); remove(attribute.getNameParts()); }
String chefAttributeName = attribute.getKey(); AttributeSensor<?> sensor = attribute.getValue(); log.trace("Finding value for attribute sensor " + sensor.getName()); entity.getDisplayName(), Joiner.on('.').join(prefixedPath), sensor.getName(), e.getMessage()}); throw Throwables.propagate(e); entity.getDisplayName(), Joiner.on('.').join(prefixedPath), sensor.getName(), elementForSensor.getAsString()}); break; entity.sensors().set((AttributeSensor)sensor, TypeCoercions.coerce(elementForSensor.getAsString(), sensor.getTypeToken())); } else { log.debug("Entity {}: no Chef attribute matching {}; setting sensor {} to null", new Object[]{ entity.getDisplayName(), chefAttributeName, sensor.getName()}); entity.sensors().set(sensor, null);
@Override public T apply(String input) { return TypeCoercions.coerce(Strings.trimEnd(input), (Class<T>) sensor.getType()); }}, SshValueFunctions.stdout()));
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Function<? super Collection<S>, ? extends T> get() { // relies on TypeCoercion of result from Number to T, and type erasure for us to get away with it! return new ComputingAverage((Number)defaultValueForUnreportedSensors, (Number)valueToReportIfNoSensors, publishing.getTypeToken()); } };
@SuppressWarnings({ "unchecked", "rawtypes" }) public WindowsPerformanceCounterPollConfig(AttributeSensor<T> sensor) { super(sensor); description(sensor.getDescription()); onSuccess((Function)Functions.identity()); }
@Override public void onSuccess(WinRmToolResponse val) { for (String pollResponse : val.getStdOut().split("\r\n")) { if (Strings.isNullOrEmpty(pollResponse)) { continue; } String path = pollResponse.substring(0, OUTPUT_COLUMN_WIDTH - 1); // The performance counter output prepends the sensor name with "\\<machinename>" so we need to remove it Matcher machineNameLookbackMatcher = MACHINE_NAME_LOOKBACK_PATTERN.matcher(path); if (!machineNameLookbackMatcher.find()) { continue; } String name = machineNameLookbackMatcher.group(0).trim(); String rawValue = pollResponse.substring(OUTPUT_COLUMN_WIDTH).replaceAll("^\\s+", ""); WindowsPerformanceCounterPollConfig<?> config = getPollConfig(name); Class<?> clazz = config.getSensor().getType(); AttributeSensor<Object> attribute = (AttributeSensor<Object>) Sensors.newSensor(clazz, config.getSensor().getName(), config.getDescription()); try { Object value = TypeCoercions.coerce(rawValue, TypeToken.of(clazz)); entity.sensors().set(attribute, value); } catch (Exception e) { Exceptions.propagateIfFatal(e); if (failedAttributes.add(attribute)) { log.warn("Failed to coerce value '{}' to {} for {} -> {}", new Object[] {rawValue, clazz, entity, attribute}); } else { if (log.isTraceEnabled()) log.trace("Failed (repeatedly) to coerce value '{}' to {} for {} -> {}", new Object[] {rawValue, clazz, entity, attribute}); } } } }