forked from bazelbuild/intellij
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEVEN-355] Implement tracing task duration (#17)
- Loading branch information
Serge Belov
authored
Aug 3, 2022
1 parent
afb4b9e
commit 4c2dcf2
Showing
6 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
base/src/com/google/idea/blaze/base/analytics/OtlpTraceSpan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.google.idea.blaze.base.analytics; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.Tracer; | ||
|
||
import java.util.Objects; | ||
|
||
public final class OtlpTraceSpan implements TraceSpan { | ||
|
||
private static final String SCOPE_NAME = "sync"; | ||
private final Span span; | ||
|
||
private OtlpTraceSpan(Span span) { | ||
Objects.requireNonNull(span); | ||
this.span = span; | ||
} | ||
|
||
public static TraceSpan create(String name) { | ||
Tracer tracer = OtlpTracer.create(SCOPE_NAME); | ||
Span span = tracer | ||
.spanBuilder(name) | ||
.startSpan(); | ||
return new OtlpTraceSpan(span); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
span.end(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
base/src/com/google/idea/blaze/base/analytics/OtlpTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.google.idea.blaze.base.analytics; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
|
||
public final class OtlpTracer { | ||
|
||
private static final String OTLP_HOST = "localhost"; | ||
private static final int OTLP_PORT = 4317; | ||
private static final String SERVICE_NAME = "canva.devenv.bazelbuild-intellij"; | ||
|
||
// If we cannot open the specified port, we assume that | ||
// opentelemetry-collector is not running and no-op the implementaton. | ||
private static OpenTelemetry openTelemetry = isSocketListening(OTLP_HOST, OTLP_PORT) | ||
? initializeOpenTelemetrySdk(SERVICE_NAME, OTLP_HOST, OTLP_PORT) | ||
: OpenTelemetry.noop(); | ||
|
||
public static Tracer create(String scopeName) { | ||
return openTelemetry.getTracer(scopeName); | ||
} | ||
|
||
private static OpenTelemetry initializeOpenTelemetrySdk(String serviceName, String host, int port) { | ||
Attributes attributes = Attributes.builder() | ||
.put(ResourceAttributes.SERVICE_NAME, serviceName) | ||
.build(); | ||
|
||
Resource serviceResource = Resource | ||
.create(attributes); | ||
|
||
OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.builder() | ||
.setEndpoint(endpointAsString(host, port)) | ||
.build(); | ||
|
||
SpanProcessor processor = SimpleSpanProcessor.create(exporter); | ||
|
||
SdkTracerProvider tracerProvider = SdkTracerProvider.builder() | ||
.addSpanProcessor(processor) | ||
.setResource(serviceResource) | ||
.build(); | ||
|
||
return OpenTelemetrySdk.builder() | ||
.setTracerProvider(tracerProvider) | ||
.buildAndRegisterGlobal(); | ||
} | ||
|
||
private static boolean isSocketListening(String host, int port) { | ||
try (Socket ignored = new Socket(host, port)) { | ||
return true; | ||
} catch (IOException ignored) { | ||
return false; | ||
} | ||
} | ||
|
||
private static String endpointAsString(String host, int port) { | ||
return "http://" + host + ":" + port; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.google.idea.blaze.base.analytics; | ||
|
||
/** | ||
* close() exports the span. | ||
*/ | ||
public interface TraceSpan extends AutoCloseable { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters