@Bean Map<String, SignalFxScopeConfiguration> signalFxScopeConfigurationMap(SignalFxConfigurationProperties signalFxConfigurationProperties) { return signalFxConfigurationProperties.getAccounts().stream() .collect(Collectors.toMap(SignalFxManagedAccount::getName, accountConfig -> SignalFxScopeConfiguration.builder() .defaultScopeKey(accountConfig.getDefaultScopeKey()) .defaultLocationKey(accountConfig.getDefaultLocationKey()) .build())); }
public SimpleSignalFlowProgramBuilder withScope(SignalFxCanaryScope canaryScope) { scopeKeys.addAll(Optional.ofNullable(canaryScope.getExtendedScopeParams()).orElse(new HashMap<>()) .keySet().stream().filter(key -> !key.startsWith("_")).collect(Collectors.toList())); canaryScope.setScopeKey(Optional.ofNullable(canaryScope.getScopeKey()) .orElseGet(() -> Optional.ofNullable(scopeConfiguration.getDefaultScopeKey()) .orElseThrow(() -> new IllegalArgumentException("The SignalFx account must define a default scope key or " + "it must be supplied in the extendedScopeParams in the `_scope_key` key")))); Optional.ofNullable(Optional.ofNullable(canaryScope.getLocationKey()) .orElse(Optional.ofNullable(scopeConfiguration.getDefaultLocationKey()).orElse(null))) .ifPresent( locationKey -> { canaryScope.setLocationKey(locationKey); scopeKeys.add(locationKey); }); scopeKeys.add(canaryScope.getScopeKey()); filterSegments.add(buildFilterSegmentFromScope(canaryScope)); return this; }
@Test public void test_that_when_there_are_extra_scope_query_pairs_that_the_program_builds_as_expected() { String metricName = "request.count"; String aggregationMethod = "mean"; SignalFxCanaryScope scope = new SignalFxCanaryScope(); SignalFxScopeConfiguration scopeConfiguration = new SignalFxScopeConfiguration(); scope.setScope("1.0.0"); scope.setScopeKey("version"); scope.setExtendedScopeParams(ImmutableMap.of("_scope_key", "version")); SimpleSignalFlowProgramBuilder builder = SimpleSignalFlowProgramBuilder .create(metricName, aggregationMethod, scopeConfiguration); builder.withScope(scope); String expected = "data('request.count', filter=" + "filter('version', '1.0.0'))" + ".mean(by=['version']).publish()"; assertEquals(expected, builder.build()); }
@Test public void test_that_the_program_builder_builds_the_expected_program_with_no_query_pairs() { String metricName = "request.count"; String aggregationMethod = "mean"; SignalFxCanaryScope scope = new SignalFxCanaryScope(); SignalFxScopeConfiguration scopeConfiguration = new SignalFxScopeConfiguration(); scope.setScope("1.0.0"); scope.setScopeKey("version"); scope.setExtendedScopeParams(ImmutableMap.of( "env", "production", "_scope_key", "version")); SimpleSignalFlowProgramBuilder builder = SimpleSignalFlowProgramBuilder .create(metricName, aggregationMethod, scopeConfiguration); builder.withScope(scope); String expected = "data('request.count', filter=" + "filter('version', '1.0.0') " + "and filter('env', 'production'))" + ".mean(by=['env', 'version']).publish()"; assertEquals(expected, builder.build()); }
@DataProvider public static Object[][] locationScopeProvider() { return new Object[][]{ { new SignalFxCanaryScope() .setScopeKey("version") .setLocationKey("region") .setScope("1.0.0") .setLocation("us-west-2") .setExtendedScopeParams(ImmutableMap.of( "env", "production", "_scope_key", "version", "_location_key", "region")), SignalFxScopeConfiguration.builder().build() }, { new SignalFxCanaryScope() .setScopeKey("version") .setScope("1.0.0") .setLocation("us-west-2") .setExtendedScopeParams(ImmutableMap.of("env", "production")), SignalFxScopeConfiguration.builder() .defaultLocationKey("region") .build() } }; } @Test
public SimpleSignalFlowProgramBuilder withScope(SignalFxCanaryScope canaryScope) { scopeKeys.addAll(Optional.ofNullable(canaryScope.getExtendedScopeParams()).orElse(new HashMap<>()) .keySet().stream().filter(key -> !key.startsWith("_")).collect(Collectors.toList())); canaryScope.setScopeKey(Optional.ofNullable(canaryScope.getScopeKey()) .orElseGet(() -> Optional.ofNullable(scopeConfiguration.getDefaultScopeKey()) .orElseThrow(() -> new IllegalArgumentException("The SignalFx account must define a default scope key or " + "it must be supplied in the extendedScopeParams in the `_scope_key` key")))); Optional.ofNullable(Optional.ofNullable(canaryScope.getLocationKey()) .orElse(Optional.ofNullable(scopeConfiguration.getDefaultLocationKey()).orElse(null))) .ifPresent( locationKey -> { canaryScope.setLocationKey(locationKey); scopeKeys.add(locationKey); }); scopeKeys.add(canaryScope.getScopeKey()); filterSegments.add(buildFilterSegmentFromScope(canaryScope)); return this; }
@Test public void test_that_the_program_builder_builds_the_expected_program_1_qp() { String metricName = "request.count"; String aggregationMethod = "mean"; SignalFxCanaryScope scope = new SignalFxCanaryScope(); SignalFxScopeConfiguration scopeConfiguration = new SignalFxScopeConfiguration(); scope.setScope("1.0.0"); scope.setScopeKey("version"); scope.setExtendedScopeParams(ImmutableMap.of( "env", "production", "_scope_key", "version")); SimpleSignalFlowProgramBuilder builder = SimpleSignalFlowProgramBuilder .create(metricName, aggregationMethod, scopeConfiguration); builder.withQueryPair(new QueryPair("app", "cms")); builder.withScope(scope); String expected = "data('request.count', filter=" + "filter('app', 'cms') " + "and filter('version', '1.0.0') " + "and filter('env', 'production'))" + ".mean(by=['env', 'version']).publish()"; assertEquals(expected, builder.build()); }
@Test public void test_that_if_a_default_scope_key_was_defined_it_is_used_if_not_overwritten() { String metricName = "request.count"; String aggregationMethod = "mean"; SignalFxCanaryScope scope = new SignalFxCanaryScope(); SignalFxScopeConfiguration scopeConfiguration = SignalFxScopeConfiguration.builder() .defaultScopeKey("server_group") .build(); scope.setScope("my_microservice-control-v1"); SimpleSignalFlowProgramBuilder builder = SimpleSignalFlowProgramBuilder .create(metricName, aggregationMethod, scopeConfiguration); builder.withScope(scope); String expected = "data('request.count', filter=" + "filter('server_group', 'my_microservice-control-v1'))" + ".mean(by=['server_group']).publish()"; assertEquals(expected, builder.build()); } }
@Test public void test_that_the_program_builder_builds_the_expected_program_with_extra_scope_qp_pairs() { String metricName = "request.count"; String aggregationMethod = "mean"; SignalFxCanaryScope scope = new SignalFxCanaryScope(); SignalFxScopeConfiguration scopeConfiguration = new SignalFxScopeConfiguration(); scope.setScope("1.0.0"); scope.setScopeKey("version"); scope.setExtendedScopeParams(ImmutableMap.of( "env", "production", "region", "us-west-2", "_scope_key", "version")); SimpleSignalFlowProgramBuilder builder = SimpleSignalFlowProgramBuilder .create(metricName, aggregationMethod, scopeConfiguration); builder.withQueryPair(new QueryPair("app", "cms")); builder.withQueryPair(new QueryPair("response_code", "400")); builder.withQueryPair(new QueryPair("uri", "/v2/auth/iam-principal")); builder.withScope(scope); String expected = "data('request.count', filter=" + "filter('app', 'cms') " + "and filter('response_code', '400') " + "and filter('uri', '/v2/auth/iam-principal') " + "and filter('version', '1.0.0') " + "and filter('env', 'production') " + "and filter('region', 'us-west-2'))" + ".mean(by=['env', 'region', 'version']).publish()"; assertEquals(expected, builder.build()); }
@Bean Map<String, SignalFxScopeConfiguration> signalFxScopeConfigurationMap(SignalFxConfigurationProperties signalFxConfigurationProperties) { return signalFxConfigurationProperties.getAccounts().stream() .collect(Collectors.toMap(SignalFxManagedAccount::getName, accountConfig -> SignalFxScopeConfiguration.builder() .defaultScopeKey(accountConfig.getDefaultScopeKey()) .defaultLocationKey(accountConfig.getDefaultLocationKey()) .build())); }
@Test public void test_that_the_program_builder_builds_the_expected_program_no_location_key() { String metricName = "request.count"; String aggregationMethod = "mean"; SignalFxCanaryScope scope = new SignalFxCanaryScope(); SignalFxScopeConfiguration scopeConfiguration = new SignalFxScopeConfiguration(); scope.setScope("1.0.0"); scope.setScopeKey("version"); scope.setExtendedScopeParams(ImmutableMap.of( "env", "production", "_scope_key", "version")); SimpleSignalFlowProgramBuilder builder = SimpleSignalFlowProgramBuilder .create(metricName, aggregationMethod, scopeConfiguration); builder.withQueryPair(new QueryPair("app", "cms")); builder.withQueryPair(new QueryPair("response_code", "400")); builder.withQueryPair(new QueryPair("uri", "/v2/auth/iam-principal")); builder.withScope(scope); String expected = "data('request.count', filter=" + "filter('app', 'cms') " + "and filter('response_code', '400') " + "and filter('uri', '/v2/auth/iam-principal') " + "and filter('version', '1.0.0') " + "and filter('env', 'production'))" + ".mean(by=['env', 'version']).publish()"; assertEquals(expected, builder.build()); }