Skip to content

Commit 3ffeeb7

Browse files
committed
gcp-resources: add connect and read timeout, add response body limit
1 parent 6cb5168 commit 3ffeeb7

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpMetadataConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ final class GcpMetadataConfig {
2727
static final GcpMetadataConfig DEFAULT_INSTANCE = new GcpMetadataConfig();
2828

2929
private static final String DEFAULT_URL = "http://metadata.google.internal/computeMetadata/v1/";
30+
// google-auth-library-java uses 500 ms connect timeout (with up to 3 retries) for its metadata
31+
// ping and sets no read timeout; 1 s is generous for an attribute fetch on the link-local
32+
// metadata server.
33+
private static final int CONNECT_TIMEOUT_MS = 1_000;
34+
private static final int READ_TIMEOUT_MS = 1_000;
35+
private static final int MAX_METADATA_VALUE_CHARS = 4096;
3036
private final String url;
3137
private final Map<String, String> cachedAttributes = new ConcurrentHashMap<>();
3238

@@ -146,13 +152,17 @@ private String fetchAttribute(String attributeName) {
146152
try {
147153
URL url = URI.create(this.url + attributeName).toURL();
148154
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
155+
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
156+
connection.setReadTimeout(READ_TIMEOUT_MS);
149157
connection.setRequestProperty("Metadata-Flavor", "Google");
150158
if (connection.getResponseCode() == 200
151159
&& "Google".equals(connection.getHeaderField("Metadata-Flavor"))) {
152160
InputStream input = connection.getInputStream();
153161
try (BufferedReader reader =
154162
new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
155-
return reader.readLine();
163+
char[] buffer = new char[MAX_METADATA_VALUE_CHARS];
164+
int n = reader.read(buffer, 0, buffer.length);
165+
return n > 0 ? new String(buffer, 0, n) : null;
156166
}
157167
}
158168
} catch (IOException ignore) {

gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpMetadataConfigTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.contrib.gcp.resource;
77

8+
import static org.assertj.core.api.Assertions.assertThat;
89
import static org.junit.jupiter.api.Assertions.assertEquals;
910

1011
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
@@ -138,4 +139,12 @@ void testGetInstanceHostName() {
138139
void testGetInstanceName() {
139140
assertEquals(mockInstanceName, mockMetadataConfig.getInstanceName());
140141
}
142+
143+
@Test
144+
void testOversizedResponseIsTruncatedTo4096Chars() {
145+
String oversized = new String(new char[8192]).replace('\0', 'x');
146+
TestUtils.stubEndpoint("/project/project-id", oversized);
147+
String result = new GcpMetadataConfig("http://localhost:8090/").getProjectId();
148+
assertThat(result).hasSize(4096).isEqualTo(new String(new char[4096]).replace('\0', 'x'));
149+
}
141150
}

0 commit comments

Comments
 (0)