-
Notifications
You must be signed in to change notification settings - Fork 26
Adding support for Dhalion to fetch metrics from multiple metrics pro… #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,109 @@ | ||||||
| package com.microsoft.dhalion; | ||||||
|
|
||||||
| import com.google.common.annotations.VisibleForTesting; | ||||||
| import com.google.inject.AbstractModule; | ||||||
| import com.google.inject.Guice; | ||||||
| import com.google.inject.Injector; | ||||||
| import com.microsoft.dhalion.api.MetricsProvider; | ||||||
| import com.microsoft.dhalion.conf.Config; | ||||||
| import com.microsoft.dhalion.conf.Key; | ||||||
| import com.microsoft.dhalion.core.Measurement; | ||||||
|
|
||||||
| import javax.inject.Inject; | ||||||
| import java.time.Duration; | ||||||
| import java.time.Instant; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.Collection; | ||||||
| import java.util.Collections; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.List; | ||||||
| import java.util.Map; | ||||||
| import java.util.Set; | ||||||
|
|
||||||
| public class CompositeMetricsProvider implements MetricsProvider { | ||||||
|
|
||||||
| private List<MetricsProvider> metricsProviders; | ||||||
| private Map<String, MetricsProvider> metricsProviderMap; | ||||||
| protected Config sysConfig; | ||||||
|
|
||||||
| @Inject | ||||||
| public CompositeMetricsProvider(Config sysConf) throws ClassNotFoundException { | ||||||
| sysConfig = sysConf; | ||||||
| metricsProviders = new ArrayList<>(); | ||||||
| metricsProviderMap = new HashMap<>(); | ||||||
| instantiateMetricsProviders(); | ||||||
| } | ||||||
|
|
||||||
| @VisibleForTesting | ||||||
|
abmodi marked this conversation as resolved.
|
||||||
| protected void instantiateMetricsProviders() throws ClassNotFoundException { | ||||||
| Injector injector = Guice.createInjector(new AbstractModule() { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating an injector here does not seem correct. This injector can only be used for instantiating metrics providers that need sysconfig. This may not be sufficient. If at all there needs to be a way to pass the injector in the constructor and use it. |
||||||
| @Override | ||||||
| protected void configure() { | ||||||
| bind(Config.class).toInstance(sysConfig); | ||||||
| } | ||||||
| }); | ||||||
| String metricsProviderClasses = (String) sysConfig.get(Key.METRICS_PROVIDER_CLASS.value()); | ||||||
|
abmodi marked this conversation as resolved.
|
||||||
| if (!metricsProviderClasses.isEmpty()) { | ||||||
| String[] mpClasses = metricsProviderClasses.split(","); | ||||||
| for (String mpClass : mpClasses) { | ||||||
| Class<MetricsProvider> metricsProviderClass = | ||||||
| (Class<MetricsProvider>) this.getClass().getClassLoader().loadClass(mpClass); | ||||||
| addMetricsProvider(injector.getInstance(metricsProviderClass)); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| protected void addMetricsProvider(MetricsProvider provider) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this |
||||||
| metricsProviders.add(provider); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void initialize() { | ||||||
| for (MetricsProvider metricsProvider : metricsProviders) { | ||||||
| metricsProvider.initialize(); | ||||||
| Set<String> metricTypes = metricsProvider.getMetricTypes(); | ||||||
| if (metricTypes != null) { | ||||||
| for (String metricType : metricsProvider.getMetricTypes()) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse the metric type set above?
Suggested change
|
||||||
| metricsProviderMap.put(metricType, metricsProvider); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should an exception be thrown if more than one metrics provider supports a metric type? |
||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public Collection<Measurement> getMeasurements(Instant startTime, | ||||||
| Duration duration, Collection<String> metrics, | ||||||
| Collection<String> components) { | ||||||
| Collection<Measurement> measurements = new ArrayList<>(); | ||||||
| for (String metric : metrics) { | ||||||
| MetricsProvider provider = metricsProviderMap.get(metric); | ||||||
| if (provider != null) { | ||||||
| measurements.addAll(provider.getMeasurements(startTime, duration, | ||||||
| Collections.singletonList(metric), components)); | ||||||
| } | ||||||
| } | ||||||
| return measurements; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public Set<String> getMetricTypes() { | ||||||
| return metricsProviderMap.keySet(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public Collection<Measurement> getMeasurements(Instant startTime, | ||||||
| Duration duration, String metric, String component) { | ||||||
| MetricsProvider provider = metricsProviderMap.get(metric); | ||||||
| if (provider != null) { | ||||||
| return provider.getMeasurements(startTime, duration, metric, component); | ||||||
| } | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void close() { | ||||||
| for (MetricsProvider metricsProvider : metricsProviders) { | ||||||
| metricsProvider.close(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package com.microsoft.dhalion; | ||
|
|
||
| import com.microsoft.dhalion.api.MetricsProvider; | ||
| import com.microsoft.dhalion.conf.Config; | ||
| import com.microsoft.dhalion.conf.Key; | ||
| import com.microsoft.dhalion.core.Measurement; | ||
| import com.microsoft.dhalion.core.MeasurementsTable; | ||
| import com.microsoft.dhalion.examples.CSVMetricsProvider; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static com.microsoft.dhalion.core.MeasurementsTable.SortKey.TIME_STAMP; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class CompositeMetricsProviderTest { | ||
|
|
||
| private MetricsProvider provider; | ||
|
|
||
| @Before | ||
| public void setup() throws ClassNotFoundException { | ||
| Config conf = mock(Config.class); | ||
| when(conf.get(Key.DATA_DIR.value())).thenReturn(CompositeMetricsProvider.class.getClassLoader().getResource(".").getFile()); | ||
| provider = new MockCompositeMetricsProviderTest(conf); | ||
| provider.initialize(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetMetricTypes() { | ||
| Set<String> metricTypes = provider.getMetricTypes(); | ||
| Assert.assertEquals(metricTypes.size(), 3); | ||
| Assert.assertTrue(metricTypes.contains("MockMetric")); | ||
| Assert.assertTrue(metricTypes.contains("Cpu")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetMeasurements() { | ||
| Instant startTS = Instant.parse("2018-01-08T01:37:36.934Z"); | ||
| String metric = "Cpu"; | ||
| Duration duration = Duration.ofMinutes(2); | ||
| String comp = "NodeA"; | ||
|
|
||
| Collection<String> metricNames = new ArrayList<>(); | ||
| metricNames.add(metric); | ||
| Collection<String> components = new ArrayList<>(); | ||
| components.add(comp); | ||
|
|
||
| MeasurementsTable metrics = MeasurementsTable.of( | ||
| provider.getMeasurements(startTS, duration, metricNames, components)); | ||
| assertEquals(4, metrics.size()); | ||
| assertEquals(4, metrics.type(metric).size()); | ||
| assertEquals(2, metrics.component(comp).instance("1").size()); | ||
| assertEquals(2, metrics.component(comp).instance("3").size()); | ||
|
|
||
| Iterator<Measurement> measurements = metrics.component(comp).instance("1").sort(false, TIME_STAMP).get().iterator(); | ||
| assertEquals("2018-01-08T01:36:36.934Z", measurements.next().instant().toString()); | ||
| assertEquals("2018-01-08T01:37:36.934Z", measurements.next().instant().toString()); | ||
|
|
||
| metrics = MeasurementsTable.of( | ||
| provider.getMeasurements(startTS, duration, "MockMetric", "abc")); | ||
| assertEquals(metrics.size(), 1); | ||
|
|
||
| Assert.assertNull( | ||
| provider.getMeasurements(startTS, duration, "WrongMetric", "abc")); | ||
| } | ||
|
|
||
| class MockCompositeMetricsProviderTest extends CompositeMetricsProvider { | ||
| MockCompositeMetricsProviderTest(Config sysConf) throws ClassNotFoundException { | ||
| super(sysConf); | ||
| } | ||
|
|
||
| @Override | ||
| protected void instantiateMetricsProviders() throws ClassNotFoundException { | ||
| addMetricsProvider(new CSVMetricsProvider(sysConfig)); | ||
| addMetricsProvider(new MockMetricsProvider()); | ||
| } | ||
| } | ||
|
|
||
| class MockMetricsProvider implements MetricsProvider { | ||
| @Override public Collection<Measurement> getMeasurements(Instant startTime, | ||
| Duration duration, Collection<String> metrics, | ||
| Collection<String> components) { | ||
| List<Measurement> measurements = new ArrayList<>(); | ||
| for (String component : components) { | ||
| for (String metric : metrics) { | ||
| measurements.addAll(getMeasurements(startTime, duration, metric, component)); | ||
| } | ||
| } | ||
| return measurements; | ||
| } | ||
|
|
||
| @Override public Set<String> getMetricTypes() { | ||
| return Collections.singleton("MockMetric"); | ||
| } | ||
|
|
||
| @Override public Collection<Measurement> getMeasurements(Instant startTime, | ||
| Duration duration, String metric, String component) { | ||
| return Collections.singleton(new Measurement(component, "1", metric, startTime, 100)); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class needs some a javadoc to help users know the behavior of this implementation