-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea505d
commit ecb2125
Showing
6 changed files
with
379 additions
and
158 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...urces/src/main/java/io/opentelemetry/contrib/azure/resource/AzureAksResourceProvider.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,76 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.azure.resource; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.semconv.incubating.CloudIncubatingAttributes; | ||
import io.opentelemetry.semconv.incubating.K8sIncubatingAttributes; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public class AzureAksResourceProvider extends CloudResourceProvider { | ||
|
||
private static final Map<String, AzureVmResourceProvider.Entry> COMPUTE_MAPPING = new HashMap<>(); | ||
|
||
static { | ||
COMPUTE_MAPPING.put( | ||
"resourceGroupName", | ||
new AzureVmResourceProvider.Entry( | ||
K8sIncubatingAttributes.K8S_CLUSTER_NAME, AzureAksResourceProvider::parseClusterName)); | ||
} | ||
|
||
// visible for testing | ||
static String parseClusterName(String resourceGroup) { | ||
// Code inspired by | ||
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/datadogexporter/internal/hostmetadata/internal/azure/provider.go#L36 | ||
String[] splitAll = resourceGroup.split("_"); | ||
if (splitAll.length == 4 && splitAll[0].equalsIgnoreCase("mc")) { | ||
return splitAll[splitAll.length - 2]; | ||
} | ||
return resourceGroup; | ||
} | ||
|
||
// Environment variable that is set when running on Kubernetes | ||
static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST"; | ||
private final Supplier<Optional<String>> client; | ||
private final Map<String, String> environment; | ||
|
||
// SPI | ||
public AzureAksResourceProvider() { | ||
this(AzureMetadataService.defaultClient(), System.getenv()); | ||
} | ||
|
||
// visible for testing | ||
public AzureAksResourceProvider( | ||
Supplier<Optional<String>> client, Map<String, String> environment) { | ||
this.client = client; | ||
this.environment = environment; | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// run after the fast cloud resource providers that only check environment variables | ||
// and before the AKS provider | ||
return 100; | ||
} | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties configProperties) { | ||
if (environment.get(KUBERNETES_SERVICE_HOST) == null) { | ||
return Resource.empty(); | ||
} | ||
return client | ||
.get() | ||
.map( | ||
body -> | ||
AzureVmResourceProvider.parseMetadata( | ||
body, COMPUTE_MAPPING, CloudIncubatingAttributes.CloudPlatformValues.AZURE_AKS)) | ||
.orElse(Resource.empty()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...resources/src/main/java/io/opentelemetry/contrib/azure/resource/AzureMetadataService.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,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.azure.resource; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class AzureMetadataService { | ||
static final JsonFactory JSON_FACTORY = new JsonFactory(); | ||
private static final URL METADATA_URL; | ||
|
||
static { | ||
try { | ||
METADATA_URL = new URL("http://169.254.169.254/metadata/instance?api-version=2021-02-01"); | ||
} catch (MalformedURLException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private AzureMetadataService() {} | ||
|
||
private static final Duration TIMEOUT = Duration.ofSeconds(1); | ||
|
||
private static final Logger logger = Logger.getLogger(AzureMetadataService.class.getName()); | ||
|
||
static Supplier<Optional<String>> defaultClient() { | ||
return () -> fetchMetadata(METADATA_URL); | ||
} | ||
|
||
// visible for testing | ||
static Optional<String> fetchMetadata(URL url) { | ||
OkHttpClient client = | ||
new OkHttpClient.Builder() | ||
.callTimeout(TIMEOUT) | ||
.connectTimeout(TIMEOUT) | ||
.readTimeout(TIMEOUT) | ||
.build(); | ||
|
||
Request request = new Request.Builder().url(url).get().addHeader("Metadata", "true").build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
int responseCode = response.code(); | ||
if (responseCode != 200) { | ||
logger.log( | ||
Level.FINE, | ||
"Error response from " | ||
+ url | ||
+ " code (" | ||
+ responseCode | ||
+ ") text " | ||
+ response.message()); | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(Objects.requireNonNull(response.body()).string()); | ||
} catch (IOException e) { | ||
logger.log(Level.FINE, "Failed to fetch Azure VM metadata", e); | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.