public TypeHandler<?> getTypeHandler() { return metricInfo.getTypeHandler(); }
private int getDescriptorOffset() { return metricInfo.getOffset(); }
private synchronized void addMetricInfo(MetricName name, Semantics semantics, Unit<?> unit, Object initialValue, TypeHandler<?> pcpType) { if (metricData.containsKey(name)) { throw new IllegalArgumentException("Metric " + name + " has already been added to writer"); } metricNameValidator.validateNameConstraints(name); PcpMetricInfo metricInfo = getMetricInfo(name.getMetric()); InstanceDomain domain = null; Instance instance = null; if (name.hasInstance()) { domain = getInstanceDomain(name.getInstanceDomainTag()); instance = domain.getInstance(name.getInstance()); metricInfo.setInstanceDomain(domain); } metricInfo.setTypeHandler(pcpType); metricInfo.setUnit(unit); metricInfo.setSemantics(semantics); PcpValueInfo info = new PcpValueInfo(name, metricInfo, instance, initialValue, stringStore); metricData.put(name, info); }
@Test @SuppressWarnings("unchecked") public void shouldBuildAByteBufferOfTheCorrectLength() throws IOException { InstanceDomain instanceDomain = mock(InstanceDomain.class); Instance instance = mock(Instance.class); PcpMetricInfo pcpMetricInfo = mock(PcpMetricInfo.class); when(pcpMetricInfo.getTypeHandler()).thenReturn(mock(TypeHandler.class)); when(pcpMetricInfo.byteSize()).thenReturn(MOCK_PCP_METRIC_INFO_BYTE_SIZE); when(instanceDomain.getInstances()).thenReturn(singletonList(instance)); when(instanceDomain.byteSize()).thenReturn(MOCK_INSTANCE_DOMAIN_BYTE_SIZE); when(instance.byteSize()).thenReturn(MOCK_INSTANCE_BYTE_SIZE); when(metricInfoStore.byName(SOME_METRIC_NAME)).thenReturn(pcpMetricInfo); when(metricInfoStore.all()).thenReturn(singletonList(pcpMetricInfo)); when(instanceDomainStore.all()).thenReturn(singletonList(instanceDomain)); when(byteBuffer.slice()).thenReturn(mock(ByteBuffer.class)); pcpMmvWriter.addMetric(MetricName.parse(SOME_METRIC_NAME), Semantics.COUNTER, null, 1); pcpMmvWriter.setMetricHelpText(SOME_METRIC_NAME, "Short help", "Long help"); pcpMmvWriter.start(); verify(byteBufferFactory).build(EXPECTED_LENGTH); }
@Override public final void setMetricHelpText(String metricName, String shortHelpText, String longHelpText) { PcpMetricInfo info = getMetricInfo(metricName); if (!info.hasHelpText()) { info.setHelpText(stringStore.createPcpString(shortHelpText), stringStore.createPcpString(longHelpText)); } }
PcpValueInfo(MetricName metricName, PcpMetricInfo metricInfo, Instance instance, Object initialValue, PcpStringStore stringStore) { this.metricName = metricName; this.metricInfo = metricInfo; this.instance = instance; this.initialValue = initialValue; if (metricInfo.getTypeHandler().requiresLargeStorage()) { this.largeValue = stringStore.createPcpString(initialValue.toString()); } else { this.largeValue = null; } }
@Test @SuppressWarnings("unchecked") public void ensureUpdatesAreIgnoredAfterResetIsCalled() throws IOException { MetricName metricName = MetricName.parse(SOME_METRIC_NAME); TypeHandler<CustomType> customTypeTypeHandler = mock(TypeHandler.class); PcpMetricInfo pcpMetricInfo = mock(PcpMetricInfo.class); when(customTypeTypeHandler.getMetricType()).thenReturn(MmvMetricType.DOUBLE); when(metricInfoStore.byName(SOME_METRIC_NAME)).thenReturn(pcpMetricInfo); doReturn(customTypeTypeHandler).when(pcpMetricInfo).getTypeHandler(); when(byteBuffer.slice()).thenReturn(mock(ByteBuffer.class)); pcpMmvWriter.registerType(CustomType.class, customTypeTypeHandler); pcpMmvWriter.start(); pcpMmvWriter.reset(); Mockito.reset(customTypeTypeHandler); pcpMmvWriter.addMetric(metricName, Semantics.COUNTER, ONE, new CustomType()); pcpMmvWriter.updateMetric(metricName, new CustomType()); verify(customTypeTypeHandler, never()).putBytes(any(ByteBuffer.class), any(CustomType.class)); }