Skip to content

Commit

Permalink
Add one missing ClientAttributesExtractor test (#8774)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Jun 21, 2023
1 parent cef9a5f commit 419fc0f
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.network;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes;
import java.net.InetSocketAddress;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;

class ClientAttributesExtractorInetSocketAddressTest {

static class TestClientAttributesGetter
implements ClientAttributesGetter<InetSocketAddress, Void> {

@Nullable
@Override
public String getClientAddress(InetSocketAddress request) {
// covered in ClientAttributesExtractorTest
return null;
}

@Nullable
@Override
public Integer getClientPort(InetSocketAddress request) {
// covered in ClientAttributesExtractorTest
return null;
}

@Nullable
@Override
public InetSocketAddress getClientInetSocketAddress(
InetSocketAddress request, @Nullable Void response) {
return request;
}
}

@Test
void fullAddress() {
InetSocketAddress address = new InetSocketAddress("api.github.com", 456);
assertThat(address.getAddress().getHostAddress()).isNotNull();

AttributesExtractor<InetSocketAddress, Void> extractor =
ClientAttributesExtractor.create(new TestClientAttributesGetter());

AttributesBuilder startAttributes = Attributes.builder();
extractor.onStart(startAttributes, Context.root(), address);
assertThat(startAttributes.build()).isEmpty();

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), address, null, null);
assertThat(endAttributes.build())
.containsOnly(
entry(NetworkAttributes.CLIENT_SOCKET_ADDRESS, address.getAddress().getHostAddress()),
entry(NetworkAttributes.CLIENT_SOCKET_PORT, 456L));
}

@Test
void noAttributes() {
AttributesExtractor<InetSocketAddress, Void> extractor =
ClientAttributesExtractor.create(new TestClientAttributesGetter());

AttributesBuilder startAttributes = Attributes.builder();
extractor.onStart(startAttributes, Context.root(), null);
assertThat(startAttributes.build()).isEmpty();

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), null, null, null);
assertThat(endAttributes.build()).isEmpty();
}
}

0 comments on commit 419fc0f

Please sign in to comment.