Skip to content

Add customizer interface for spring boot integration #434

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions analytics-spring-boot-starter/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ In your Spring Boot configuration (`application.properties`, environment, or
similar), provide the property `segment.analytics.writeKey`. The
autoconfiguration will register an `Analytics` bean in the Spring context
ready for use.

=== Customization

To customize the `Analytics` client you can define a `SegmentAnalyticsCustomizer` bean,
which receives the builder instance.

For example if you want to use a regional endpoint you can define a customizer like so:

```java
@Bean
SegmentAnalyticsCustomizer segmentAnalyticsCustomizer() {
return builder -> builder.endpoint("events.eu1.segmentapis.com");
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.segment.analytics.autoconfigure;

import com.segment.analytics.Analytics;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -20,7 +21,9 @@ public class SegmentAnalyticsAutoConfiguration {
@Autowired private SegmentProperties properties;

@Bean
public Analytics segmentAnalytics() {
return Analytics.builder(properties.getWriteKey()).build();
public Analytics segmentAnalytics(ObjectProvider<SegmentAnalyticsCustomizer> customizerProvider) {
Analytics.Builder builder = Analytics.builder(properties.getWriteKey());
customizerProvider.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
Comment on lines +25 to +27
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.segment.analytics.autoconfigure;

import com.segment.analytics.Analytics;

/**
* Callback interface that can be used to customize a {@link com.segment.analytics.Analytics.Builder
* Analytics.Builder}.
*
* @author Koen Punt
*/
@FunctionalInterface
public interface SegmentAnalyticsCustomizer {

/**
* Callback to customize a {@link com.segment.analytics.Analytics.Builder Analytics.Builder}
* instance.
*
* @param analyticsBuilder the analytics builder to customize
*/
void customize(Analytics.Builder analyticsBuilder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.segment.analytics.autoconfigure.SegmentAnalyticsAutoConfiguration