Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Jan 20, 2025
1 parent 13ae4fb commit 37a34fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class AzureMetadataService {
static final JsonFactory JSON_FACTORY = new JsonFactory();
Expand All @@ -42,7 +43,9 @@ static Supplier<Optional<String>> defaultClient() {
}

// visible for testing
@SuppressWarnings("SystemOut")
static Optional<String> fetchMetadata(URL url) {
System.out.println("fetchMetadata: " + url);
OkHttpClient client =
new OkHttpClient.Builder()
.callTimeout(TIMEOUT)
Expand All @@ -54,6 +57,7 @@ static Optional<String> fetchMetadata(URL url) {

try (Response response = client.newCall(request).execute()) {
int responseCode = response.code();
System.out.println("responseCode: " + responseCode);
if (responseCode != 200) {
logger.log(
Level.FINE,
Expand All @@ -66,8 +70,13 @@ static Optional<String> fetchMetadata(URL url) {
return Optional.empty();
}

return Optional.of(Objects.requireNonNull(response.body()).string());
ResponseBody body = response.body();
System.out.println("body: " + body);
String str = Objects.requireNonNull(body).string();
System.out.println("str: " + str);
return Optional.of(str);
} catch (IOException e) {
System.out.println(e.getMessage());
logger.log(Level.FINE, "Failed to fetch Azure VM metadata", e);
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,31 @@ public int order() {
}

@Override
@SuppressWarnings("SystemOut")
public Resource createResource(ConfigProperties config) {
return client
.get()
System.out.println("AzureVmResourceProvider.createResource");
Optional<String> optional = client.get();
System.out.println("optional.isPresent: " + optional.isPresent());
System.out.println("optional: " + optional);
return optional
.map(body -> parseMetadata(body, COMPUTE_MAPPING, AZURE_VM))
.orElse(Resource.empty());
}

@SuppressWarnings("SystemOut")
static Resource parseMetadata(String body, Map<String, Entry> computeMapping, String platform) {
System.out.println("AzureVmResourceProvider.parseMetadata");
AttributesBuilder builder = azureAttributeBuilder(platform);
try (JsonParser parser = AzureMetadataService.JSON_FACTORY.createParser(body)) {
parser.nextToken();
parseResponse(parser, builder, computeMapping);
} catch (IOException e) {
System.out.println(e.getMessage());
logger.log(Level.FINE, "Can't get Azure VM metadata", e);
}
return Resource.create(builder.build());
Attributes build = builder.build();
System.out.println("build: " + build);
return Resource.create(build);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.SAME_THREAD)
public abstract class MetadataBasedResourceProviderTest {

@RegisterExtension
public static final MockWebServerExtension server = new MockWebServerExtension();

Expand All @@ -50,7 +54,11 @@ private AttributesAssert createResource(Supplier<Optional<String>> client) {
String response = client.get().orElse(null);
System.out.println("response: " + response);
client = () -> Optional.ofNullable(response);
Resource resource = getResourceProvider(client).createResource(null);
ResourceProvider resourceProvider = getResourceProvider(client);
System.out.println("resourceProvider: " + resourceProvider);
Resource resource = resourceProvider.createResource(null);
System.out.println("resource: " + resource);
System.out.println("resource.getAttributes(): " + resource.getAttributes());
return OpenTelemetryAssertions.assertThat(resource.getAttributes());
}

Expand Down

0 comments on commit 37a34fd

Please sign in to comment.