Skip to content

Commit

Permalink
Apply Square Java Code Styles (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
making authored Jan 16, 2025
1 parent cc32107 commit 6393a0e
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public String getResourceAttributePrefix() {
public void accept(Resource resource, Span.Builder builder) {
resource.getAttributesList().stream()
.filter(kv -> !kv.getKey().equals(SemanticConventionsAttributes.SERVICE_NAME))
.forEach(kv -> builder.putTag(resourceAttributePrefix + kv.getKey(), ProtoUtils.valueToString(kv.getValue())));
.forEach(kv -> builder.putTag(resourceAttributePrefix + kv.getKey(),
ProtoUtils.valueToString(kv.getValue())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
import io.opentelemetry.proto.logs.v1.ResourceLogs;
import io.opentelemetry.proto.logs.v1.ScopeLogs;
import io.opentelemetry.proto.logs.v1.SeverityNumber;
import zipkin2.Span;
import zipkin2.internal.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import zipkin2.Span;
import zipkin2.internal.Nullable;

import static zipkin2.collector.otel.http.SpanTranslator.bytesToLong;
import static zipkin2.collector.otel.http.SpanTranslator.nanoToMills;
Expand Down Expand Up @@ -53,7 +52,8 @@ public LogEventTranslator build() {
}

private LogEventTranslator(Builder builder) {
this.resourceMapper = builder.resourceMapper == null ? DefaultOtelResourceMapper.create() : builder.resourceMapper;
this.resourceMapper = builder.resourceMapper == null ? DefaultOtelResourceMapper.create()
: builder.resourceMapper;
}

List<Span> translate(ExportLogsServiceRequest logs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.trace.v1.ScopeSpans;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import zipkin2.Callback;
import zipkin2.Span;
import zipkin2.collector.Collector;
Expand All @@ -28,12 +33,6 @@
import zipkin2.collector.CollectorSampler;
import zipkin2.storage.StorageComponent;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class OpenTelemetryHttpCollector extends CollectorComponent
implements ServerConfigurator {

Expand Down Expand Up @@ -93,7 +92,8 @@ public OpenTelemetryHttpCollector build() {
OpenTelemetryHttpCollector(Builder builder) {
collector = builder.delegate.build();
metrics = builder.metrics;
otelResourceMapper = builder.otelResourceMapper == null ? DefaultOtelResourceMapper.create() : builder.otelResourceMapper;
otelResourceMapper = builder.otelResourceMapper == null ? DefaultOtelResourceMapper.create()
: builder.otelResourceMapper;
}

@Override
Expand Down Expand Up @@ -121,7 +121,8 @@ public void reconfigure(ServerBuilder sb) {
}

static final class OtlpProtoV1TracesHttpService extends AbstractHttpService {
private static final Logger LOG = Logger.getLogger(OtlpProtoV1TracesHttpService.class.getName());
private static final Logger LOG =
Logger.getLogger(OtlpProtoV1TracesHttpService.class.getName());

final OpenTelemetryHttpCollector collector;

Expand Down Expand Up @@ -149,7 +150,8 @@ protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) {
}
collector.metrics.incrementBytes(content.length());
try {
ExportTraceServiceRequest request = ExportTraceServiceRequest.parseFrom(UnsafeByteOperations.unsafeWrap(content.byteBuf().nioBuffer()).newCodedInput());
ExportTraceServiceRequest request = ExportTraceServiceRequest.parseFrom(
UnsafeByteOperations.unsafeWrap(content.byteBuf().nioBuffer()).newCodedInput());
collector.metrics.incrementMessages();
try {
List<Span> spans = spanTranslator.translate(request);
Expand Down Expand Up @@ -204,7 +206,8 @@ protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws
}
collector.metrics.incrementBytes(content.length());
try {
ExportLogsServiceRequest request = ExportLogsServiceRequest.parseFrom(UnsafeByteOperations.unsafeWrap(content.byteBuf().nioBuffer()).newCodedInput());
ExportLogsServiceRequest request = ExportLogsServiceRequest.parseFrom(
UnsafeByteOperations.unsafeWrap(content.byteBuf().nioBuffer()).newCodedInput());
collector.metrics.incrementMessages();
try {
List<Span> spans = logEventTranslator.translate(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
*/
package zipkin2.collector.otel.http;

import java.util.function.BiConsumer;

import io.opentelemetry.proto.resource.v1.Resource;
import java.util.function.BiConsumer;

/**
* The interface to map OpenTelemetry Resource to Zipkin Span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
*/
package zipkin2.collector.otel.http;

import java.util.List;

import com.google.protobuf.TextFormat;
import io.opentelemetry.proto.common.v1.AnyValue;
import io.opentelemetry.proto.common.v1.KeyValue;
import java.util.List;

import static java.util.stream.Collectors.joining;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
*/
package zipkin2.collector.otel.http;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.protobuf.ByteString;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.common.v1.AnyValue;
Expand All @@ -23,6 +17,11 @@
import io.opentelemetry.proto.trace.v1.Span.SpanKind;
import io.opentelemetry.proto.trace.v1.Status;
import io.opentelemetry.proto.trace.v1.Status.StatusCode;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import zipkin2.Endpoint;

import static java.util.concurrent.TimeUnit.NANOSECONDS;
Expand Down Expand Up @@ -51,7 +50,6 @@ final class SpanTranslator {
this(DefaultOtelResourceMapper.create());
}


List<zipkin2.Span> translate(ExportTraceServiceRequest otelSpans) {
List<zipkin2.Span> spans = new ArrayList<>();
List<ResourceSpans> spansList = otelSpans.getResourceSpansList();
Expand Down Expand Up @@ -79,7 +77,8 @@ private zipkin2.Span generateSpan(Span spanData, InstrumentationScope scope, Res
long endTimestamp = nanoToMills(spanData.getEndTimeUnixNano());
Map<String, AnyValue> attributesMap = spanData.getAttributesList()
.stream()
.collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue, (a, b) -> b /* The latter wins */));
.collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue,
(a, b) -> b /* The latter wins */));
zipkin2.Span.Builder spanBuilder = zipkin2.Span.newBuilder();
byte[] traceIdBytes = spanData.getTraceId().toByteArray();
long high = bytesToLong(traceIdBytes, 0);
Expand Down Expand Up @@ -110,10 +109,12 @@ private zipkin2.Span generateSpan(Span spanData, InstrumentationScope scope, Res
Status status = spanData.getStatus();
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md#status
if (status.getCode() != Status.StatusCode.STATUS_CODE_UNSET) {
String codeValue = status.getCode().toString().replace("STATUS_CODE_", ""); // either OK or ERROR
String codeValue =
status.getCode().toString().replace("STATUS_CODE_", ""); // either OK or ERROR
spanBuilder.putTag(SemanticConventionsAttributes.OTEL_STATUS_CODE, codeValue);
// add the error tag, if it isn't already in the source span.
if (status.getCode() == StatusCode.STATUS_CODE_ERROR && !attributesMap.containsKey(ERROR_TAG)) {
if (status.getCode() == StatusCode.STATUS_CODE_ERROR && !attributesMap.containsKey(
ERROR_TAG)) {
spanBuilder.putTag(ERROR_TAG, status.getMessage());
}
}
Expand Down Expand Up @@ -153,8 +154,10 @@ private static Endpoint getLocalEndpoint(Map<String, AnyValue> attributesMap, Re
.orElse(null);
if (serviceName != null) {
Endpoint.Builder endpoint = Endpoint.newBuilder().serviceName(serviceName.getStringValue());
AnyValue networkLocalAddress = attributesMap.get(SemanticConventionsAttributes.NETWORK_LOCAL_ADDRESS);
AnyValue networkLocalPort = attributesMap.get(SemanticConventionsAttributes.NETWORK_LOCAL_PORT);
AnyValue networkLocalAddress =
attributesMap.get(SemanticConventionsAttributes.NETWORK_LOCAL_ADDRESS);
AnyValue networkLocalPort =
attributesMap.get(SemanticConventionsAttributes.NETWORK_LOCAL_PORT);
if (networkLocalAddress != null) {
endpoint.ip(networkLocalAddress.getStringValue());
}
Expand All @@ -170,7 +173,8 @@ private static Endpoint getLocalEndpoint(Map<String, AnyValue> attributesMap, Re
private static Endpoint getRemoteEndpoint(Map<String, AnyValue> attributesMap, SpanKind kind) {
if (kind == SpanKind.SPAN_KIND_CLIENT || kind == SpanKind.SPAN_KIND_PRODUCER) {
AnyValue peerService = attributesMap.get(SemanticConventionsAttributes.PEER_SERVICE);
AnyValue networkPeerAddress = attributesMap.get(SemanticConventionsAttributes.NETWORK_PEER_ADDRESS);
AnyValue networkPeerAddress =
attributesMap.get(SemanticConventionsAttributes.NETWORK_PEER_ADDRESS);
String serviceName = null;
// TODO: Implement fallback mechanism?
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md#otlp---zipkin
Expand All @@ -181,7 +185,8 @@ private static Endpoint getRemoteEndpoint(Map<String, AnyValue> attributesMap, S
}
if (serviceName != null) {
Endpoint.Builder endpoint = Endpoint.newBuilder().serviceName(serviceName);
AnyValue networkPeerPort = attributesMap.get(SemanticConventionsAttributes.NETWORK_PEER_PORT);
AnyValue networkPeerPort =
attributesMap.get(SemanticConventionsAttributes.NETWORK_PEER_PORT);
if (networkPeerAddress != null) {
endpoint.ip(networkPeerAddress.getStringValue());
}
Expand Down
Loading

0 comments on commit 6393a0e

Please sign in to comment.