Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 71dba4a

Browse files
authored
Replace OpenCensusTraceLoggingEnhancer(String) constructor with factory method. (#1535)
opencensus-contrib-log-correlation-stackdriver is still experimental, so this is a good time to improve the API.
1 parent 5e60f5a commit 71dba4a

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,20 @@ public OpenCensusTraceLoggingEnhancer() {
6363
this(lookUpProjectId());
6464
}
6565

66+
private OpenCensusTraceLoggingEnhancer(@Nullable String projectId) {
67+
this.projectId = projectId == null ? "" : projectId;
68+
this.tracePrefix = "projects/" + this.projectId + "/traces/";
69+
}
70+
6671
/**
67-
* Constructs a {@code OpenCensusTraceLoggingEnhancer} with the given project ID.
72+
* Returns a {@code OpenCensusTraceLoggingEnhancer} with the given project ID.
6873
*
69-
* @param projectId the project ID for this instance.
74+
* @param projectId the project ID to be used by the logging enhancer.
75+
* @return a {@code OpenCensusTraceLoggingEnhancer} with the given project ID.
7076
* @since 0.17
7177
*/
72-
public OpenCensusTraceLoggingEnhancer(@Nullable String projectId) {
73-
this.projectId = projectId == null ? "" : projectId;
74-
this.tracePrefix = "projects/" + this.projectId + "/traces/";
78+
public static OpenCensusTraceLoggingEnhancer create(@Nullable String projectId) {
79+
return new OpenCensusTraceLoggingEnhancer(projectId);
7580
}
7681

7782
private static String lookUpProjectId() {

contrib/log_correlation/stackdriver/src/test/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class OpenCensusTraceLoggingEnhancerTest {
6060
public void enhanceLogEntry_AddSampledSpanToLogEntry() {
6161
LogEntry logEntry =
6262
getEnhancedLogEntry(
63-
new OpenCensusTraceLoggingEnhancer("my-test-project-3"),
63+
OpenCensusTraceLoggingEnhancer.create("my-test-project-3"),
6464
new TestSpan(
6565
SpanContext.create(
6666
TraceId.fromLowerBase16("4c6af40c499951eb7de2777ba1e4fefa"),
@@ -76,7 +76,7 @@ public void enhanceLogEntry_AddSampledSpanToLogEntry() {
7676
public void enhanceLogEntry_AddNonSampledSpanToLogEntry() {
7777
LogEntry logEntry =
7878
getEnhancedLogEntry(
79-
new OpenCensusTraceLoggingEnhancer("my-test-project-6"),
79+
OpenCensusTraceLoggingEnhancer.create("my-test-project-6"),
8080
new TestSpan(
8181
SpanContext.create(
8282
TraceId.fromLowerBase16("72c905c76f99e99974afd84dc053a480"),
@@ -92,7 +92,7 @@ public void enhanceLogEntry_AddNonSampledSpanToLogEntry() {
9292
public void enhanceLogEntry_AddBlankSpanToLogEntry() {
9393
LogEntry logEntry =
9494
getEnhancedLogEntry(
95-
new OpenCensusTraceLoggingEnhancer("my-test-project-7"), BlankSpan.INSTANCE);
95+
OpenCensusTraceLoggingEnhancer.create("my-test-project-7"), BlankSpan.INSTANCE);
9696
assertThat(logEntry.getTrace())
9797
.isEqualTo("projects/my-test-project-7/traces/00000000000000000000000000000000");
9898
assertThat(logEntry.getSpanId()).isEqualTo("0000000000000000");
@@ -102,7 +102,7 @@ public void enhanceLogEntry_AddBlankSpanToLogEntry() {
102102
public void enhanceLogEntry_ConvertNullProjectIdToEmptyString() {
103103
LogEntry logEntry =
104104
getEnhancedLogEntry(
105-
new OpenCensusTraceLoggingEnhancer(null),
105+
OpenCensusTraceLoggingEnhancer.create(null),
106106
new TestSpan(
107107
SpanContext.create(
108108
TraceId.fromLowerBase16("bfb4248a24325a905873a1d43001d9a0"),

0 commit comments

Comments
 (0)