Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compute/metadata): add context aware functions #9733

Merged
merged 6 commits into from
Apr 15, 2024

Conversation

codyoss
Copy link
Member

@codyoss codyoss commented Apr 9, 2024

This change adds the minimal amount of context aware functionality
so that users can pass a context to all metadata requests. This
does not re-expose all the helper methods this package provides.
We can add context variants for all of these in the future and/or
if we ever create a v2 of this package.

Fixes: #4483

This change adds the minimal amount of context aware functionality
so that users can pass a context to all metadata requests. This
does not re-expose all the helper methods this package provides.
We can add context varients for all of these in the future and/or
if we ever create a v2 of this package.

Fixes: googleapis#4483
@codyoss codyoss requested a review from a team as a code owner April 9, 2024 17:12
@codyoss codyoss requested a review from quartzmo April 9, 2024 17:12
@product-auto-label product-auto-label bot added the api: compute Issues related to the Compute Engine API. label Apr 9, 2024
@codyoss codyoss added the automerge Merge the pull request once unit tests and other checks pass. label Apr 15, 2024
@gcf-merge-on-green gcf-merge-on-green bot merged commit e4eb5b4 into googleapis:main Apr 15, 2024
8 checks passed
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Apr 15, 2024
@codyoss codyoss deleted the ctx-metadata branch April 15, 2024 19:05
@release-please release-please bot mentioned this pull request Apr 15, 2024
gcf-merge-on-green bot pushed a commit that referenced this pull request Apr 15, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>advisorynotifications: 1.4.0</summary>

## [1.4.0](https://togithub.com/googleapis/google-cloud-go/compare/advisorynotifications/v1.3.2...advisorynotifications/v1.4.0) (2024-04-15)


### Features

* **advisorynotifications:** Add GetSettings and UpdateSettings methods at the Project-level to advisorynotifications.googleapis.com ([dbcdfd7](https://togithub.com/googleapis/google-cloud-go/commit/dbcdfd7843be16573b1683466852242a84891456))
</details>

<details><summary>ai: 0.4.0</summary>

## [0.4.0](https://togithub.com/googleapis/google-cloud-go/compare/ai/v0.3.4...ai/v0.4.0) (2024-04-15)


### Features

* **ai/generativelanguage:** Add question_answering and fact_verification task types for AQA ([#9745](https://togithub.com/googleapis/google-cloud-go/issues/9745)) ([cca3f47](https://togithub.com/googleapis/google-cloud-go/commit/cca3f47c895e7cac07d7d48ab3c4850b265a710f))
* **ai/generativelanguage:** Add rest binding for tuned models ([8892943](https://togithub.com/googleapis/google-cloud-go/commit/8892943b169060f8ba7be227cd65680696c494a0))
* **ai/generativelanguage:** Add system instructions ([dd7c8e5](https://togithub.com/googleapis/google-cloud-go/commit/dd7c8e5a206ca6fab7d05e2591a36ea706e5e9f1))
</details>

<details><summary>asset: 1.19.0</summary>

## [1.19.0](https://togithub.com/googleapis/google-cloud-go/compare/asset/v1.18.1...asset/v1.19.0) (2024-04-15)


### Features

* **asset:** Add tag key id support ([fe85be0](https://togithub.com/googleapis/google-cloud-go/commit/fe85be03d1e6ba69182ff1045a3faed15aa00128))
</details>

<details><summary>backupdr: 0.1.0</summary>

## 0.1.0 (2024-04-15)


### Features

* **backupdr:** Management Server APIs ([#9713](https://togithub.com/googleapis/google-cloud-go/issues/9713)) ([e7389cd](https://togithub.com/googleapis/google-cloud-go/commit/e7389cdbe9552eadc394d6ea0fa34d53e76ad4ae))
* **backupdr:** New client(s) ([#9715](https://togithub.com/googleapis/google-cloud-go/issues/9715)) ([a578fc1](https://togithub.com/googleapis/google-cloud-go/commit/a578fc1a7540a5a5499bdb8b1b921b29267ff2fa))
</details>

<details><summary>batch: 1.8.4</summary>

## [1.8.4](https://togithub.com/googleapis/google-cloud-go/compare/batch/v1.8.3...batch/v1.8.4) (2024-04-15)


### Documentation

* **batch:** State one Resource Allowance per region per project limitation on v1alpha ([dbcdfd7](https://togithub.com/googleapis/google-cloud-go/commit/dbcdfd7843be16573b1683466852242a84891456))
* **batch:** Update comments on ServiceAccount email and scopes fields ([#9734](https://togithub.com/googleapis/google-cloud-go/issues/9734)) ([4d5a342](https://togithub.com/googleapis/google-cloud-go/commit/4d5a3429cec6816d50bdf284063dddf1971b79cf))
</details>

<details><summary>cloudquotas: 0.2.0</summary>

## [0.2.0](https://togithub.com/googleapis/google-cloud-go/compare/cloudquotas/v0.1.3...cloudquotas/v0.2.0) (2024-04-15)


### Features

* **cloudquotas:** Add `rollout_info` field to `QuotaDetails` message ([2cdc40a](https://togithub.com/googleapis/google-cloud-go/commit/2cdc40a0b4288f5ab5f2b2b8f5c1d6453a9c81ec))
</details>

<details><summary>compute/metadata: 0.3.0</summary>

## [0.3.0](https://togithub.com/googleapis/google-cloud-go/compare/compute/metadata/v0.2.3...compute/metadata/v0.3.0) (2024-04-15)


### Features

* **compute/metadata:** Add context aware functions  ([#9733](https://togithub.com/googleapis/google-cloud-go/issues/9733)) ([e4eb5b4](https://togithub.com/googleapis/google-cloud-go/commit/e4eb5b46ee2aec9d2fc18300bfd66015e25a0510))
</details>

<details><summary>discoveryengine: 1.7.0</summary>

## [1.7.0](https://togithub.com/googleapis/google-cloud-go/compare/discoveryengine/v1.6.0...discoveryengine/v1.7.0) (2024-04-15)


### Features

* **discoveryengine:** Add answer generation APIs ([8892943](https://togithub.com/googleapis/google-cloud-go/commit/8892943b169060f8ba7be227cd65680696c494a0))
* **discoveryengine:** Promote recommendation service to v1 ([8892943](https://togithub.com/googleapis/google-cloud-go/commit/8892943b169060f8ba7be227cd65680696c494a0))
* **discoveryengine:** Support import data from Cloud Spanner, BigTable, SQL and Firestore ([cca3f47](https://togithub.com/googleapis/google-cloud-go/commit/cca3f47c895e7cac07d7d48ab3c4850b265a710f))
* **discoveryengine:** Support import data from Cloud Spanner, BigTable, SQL and Firestore ([#9708](https://togithub.com/googleapis/google-cloud-go/issues/9708)) ([560f121](https://togithub.com/googleapis/google-cloud-go/commit/560f121b0914edb19b26011b6a0e805c17899230))
</details>

<details><summary>documentai: 1.27.0</summary>

## [1.27.0](https://togithub.com/googleapis/google-cloud-go/compare/documentai/v1.26.1...documentai/v1.27.0) (2024-04-15)


### Features

* **documentai:** Support a new Layout Processor in Document AI ([2cdc40a](https://togithub.com/googleapis/google-cloud-go/commit/2cdc40a0b4288f5ab5f2b2b8f5c1d6453a9c81ec))
</details>

<details><summary>maps: 1.7.2</summary>

## [1.7.2](https://togithub.com/googleapis/google-cloud-go/compare/maps/v1.7.1...maps/v1.7.2) (2024-04-15)


### Documentation

* **maps/places:** Fix designation of Text Search ([#9728](https://togithub.com/googleapis/google-cloud-go/issues/9728)) ([ce55ad6](https://togithub.com/googleapis/google-cloud-go/commit/ce55ad694f21cacfa608e9b9952ee31f8d566e49))
* **maps/places:** Fix typo in PriceLevel enum ([#9669](https://togithub.com/googleapis/google-cloud-go/issues/9669)) ([264a6dc](https://togithub.com/googleapis/google-cloud-go/commit/264a6dcddbffaec987dce1dc00f6550c263d2df7))
* **maps/routing:** Various formatting and grammar fixes for proto documentation ([cca3f47](https://togithub.com/googleapis/google-cloud-go/commit/cca3f47c895e7cac07d7d48ab3c4850b265a710f))
</details>

<details><summary>monitoring: 1.18.2</summary>

## [1.18.2](https://togithub.com/googleapis/google-cloud-go/compare/monitoring/v1.18.1...monitoring/v1.18.2) (2024-04-15)


### Documentation

* **monitoring/apiv3:** Various updates ([8892943](https://togithub.com/googleapis/google-cloud-go/commit/8892943b169060f8ba7be227cd65680696c494a0))
</details>

<details><summary>networkmanagement: 1.13.1</summary>

## [1.13.1](https://togithub.com/googleapis/google-cloud-go/compare/networkmanagement/v1.13.0...networkmanagement/v1.13.1) (2024-04-15)


### Documentation

* **networkmanagement:** Update possible firewall rule actions comment ([fe85be0](https://togithub.com/googleapis/google-cloud-go/commit/fe85be03d1e6ba69182ff1045a3faed15aa00128))
</details>

<details><summary>security: 1.16.0</summary>

## [1.16.0](https://togithub.com/googleapis/google-cloud-go/compare/security/v1.15.6...security/v1.16.0) (2024-04-15)


### Features

* **security/privateca:** Add encoding format to `.google.cloud.security.privateca.v1.CaPool` Resource ([2cdc40a](https://togithub.com/googleapis/google-cloud-go/commit/2cdc40a0b4288f5ab5f2b2b8f5c1d6453a9c81ec))
</details>

<details><summary>securitycenter: 1.29.0</summary>

## [1.29.0](https://togithub.com/googleapis/google-cloud-go/compare/securitycenter/v1.28.0...securitycenter/v1.29.0) (2024-04-15)


### Features

* **securitycenter:** Add Notebook field to finding's list of attributes ([fe85be0](https://togithub.com/googleapis/google-cloud-go/commit/fe85be03d1e6ba69182ff1045a3faed15aa00128))


### Documentation

* **securitycenter:** Fixed backtick and double quotes mismatch in security_marks.proto ([dbcdfd7](https://togithub.com/googleapis/google-cloud-go/commit/dbcdfd7843be16573b1683466852242a84891456))
</details>

<details><summary>shopping: 0.5.0</summary>

## [0.5.0](https://togithub.com/googleapis/google-cloud-go/compare/shopping/v0.4.0...shopping/v0.5.0) (2024-04-15)


### Features

* **shopping/merchant/inventories:** Fix inventories sub-API publication by adding correct child_type in the API proto ([#9750](https://togithub.com/googleapis/google-cloud-go/issues/9750)) ([6a7cd4f](https://togithub.com/googleapis/google-cloud-go/commit/6a7cd4f70373fe7c60dcba12636a3d92617e7b66))
* **shopping/merchant/reports:** Add click potential to Reports sub-API publication ([#9738](https://togithub.com/googleapis/google-cloud-go/issues/9738)) ([4d0547f](https://togithub.com/googleapis/google-cloud-go/commit/4d0547fc59d73cb013d35c9b52f8683a0d57af67))
* **shopping:** New client(s) ([#9741](https://togithub.com/googleapis/google-cloud-go/issues/9741)) ([1b2aebd](https://togithub.com/googleapis/google-cloud-go/commit/1b2aebd50e78de7e39bf2ab1ea12ea02aab58717))
* **shopping:** New clients ([#9746](https://togithub.com/googleapis/google-cloud-go/issues/9746)) ([cee2900](https://togithub.com/googleapis/google-cloud-go/commit/cee290011a43e4037ce2de24014fc60dc9a9c141))
</details>

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
bwplotka added a commit to GoogleCloudPlatform/prometheus-engine that referenced this pull request Jun 11, 2024
Fixes b/344740239 (edge case with GKE Metadata Server and GKE sandbox).

* Added debug logging.
* Updated metadata deps to get googleapis/google-cloud-go#9733 & use timeout-ed context
* Moved risky logic from FromFlags, see code comment why.
* Added regession test.

### Alternatives

Everything we do in FromFlags or constructor is within readines period. We
could consider moving potentially "slow" things on slow network or metadata srv
to exporter.Run. This could be questionable as for GMP to work
we at end need export functionality to work, so delaying that information or
making it surface in separation to readiness might not be helpful.

Signed-off-by: bwplotka <bwplotka@google.com>
bwplotka added a commit to GoogleCloudPlatform/prometheus-engine that referenced this pull request Jun 11, 2024
Fixes b/344740239 (edge case with GKE Metadata Server and GKE sandbox).

* Added debug logging.
* Updated metadata deps to get googleapis/google-cloud-go#9733 & use timeout-ed context
* Moved risky logic from FromFlags, see code comment why.
* Added regession test.

### Alternatives

Everything we do in FromFlags or constructor is within readines period. We
could consider moving potentially "slow" things on slow network or metadata srv
to exporter.Run. This could be questionable as for GMP to work
we at end need export functionality to work, so delaying that information or
making it surface in separation to readiness might not be helpful.

Signed-off-by: bwplotka <bwplotka@google.com>
bwplotka added a commit to GoogleCloudPlatform/prometheus-engine that referenced this pull request Jun 11, 2024
Fixes b/344740239 (edge case with GKE Metadata Server and GKE sandbox).

* Added debug logging.
* Updated metadata deps to get googleapis/google-cloud-go#9733 & use timeout-ed context
* Moved risky logic from FromFlags, see code comment why.
* Added regession test.

### Alternatives

Everything we do in FromFlags or constructor is within readines period. We
could consider moving potentially "slow" things on slow network or metadata srv
to exporter.Run. This could be questionable as for GMP to work
we at end need export functionality to work, so delaying that information or
making it surface in separation to readiness might not be helpful.

Signed-off-by: bwplotka <bwplotka@google.com>
bwplotka added a commit to GoogleCloudPlatform/prometheus-engine that referenced this pull request Jun 11, 2024
Fixes b/344740239 (edge case with GKE Metadata Server and GKE sandbox).

* Added debug logging.
* Updated metadata deps to get googleapis/google-cloud-go#9733 & use timeout-ed context
* Moved risky logic from FromFlags, see code comment why.
* Added regession test.

### Alternatives

Everything we do in FromFlags or constructor is within readines period. We
could consider moving potentially "slow" things on slow network or metadata srv
to exporter.Run. This could be questionable as for GMP to work
we at end need export functionality to work, so delaying that information or
making it surface in separation to readiness might not be helpful.

Signed-off-by: bwplotka <bwplotka@google.com>
bwplotka added a commit to bwplotka/google-cloud-go that referenced this pull request Jun 12, 2024
Hi!

The [GMP product got hit by the case](GoogleCloudPlatform/prometheus-engine#1021) where calls to metadata server
were hanging "forever" (GKE sandbox with disabled metadata server). Thanks to
recent work in this library, the clients support retry with backoff with timeouts.

However, still a single call was taking ~14s and since we made multiple of those, it took down
the service (readiness probe of 30s).

We can hackly workaround things (as we did in [here](GoogleCloudPlatform/prometheus-engine@e02408c)), by copying some code around, but I think the
above issue proves it would be helpful to actually add context to all metadata functions and methods.

I saw googleapis#9733 we started small, but
in this PR I actually spent 1h to add context to all things for consistency.

I self-reviewed carefuly, and tried to be consistent and extra safe, but careful
review is needed, given little testing.

Some minor extra changes (we can split to new commit/PR):
* Added comment around 14s worst case
* Added comment about OnGCE semantics which was a bit surprising for us.

Alternative would be something like

```
// WithContext sets the context that will be used for all client methods that
// does not support context. This means that e.g. the context passed in
// GetWithContext will override the context in WithContext
func (c *Client) WithContext(ctx context.Context) {
	c.ctx = ctx
}

```

.. but the logic is not trivial and we introdue some state to the global variable (defaultClient), which
can be source of problems if someone sets context for them, but another package in same binary resues it etc.

Given it's trivial to add (and use) new methods, I just did it.

Signed-off-by: bwplotka <bwplotka@gmail.com>
bwplotka added a commit to bwplotka/google-cloud-go that referenced this pull request Jun 12, 2024
Hi!

The [GMP product got hit by the case](GoogleCloudPlatform/prometheus-engine#1021) where calls to metadata server
were hanging "forever" (GKE sandbox with disabled metadata server). Thanks to
recent work in this library, the clients support retry with backoff with timeouts.

However, still a single call was taking ~14s and since we made multiple of those, it took down
the service (readiness probe of 30s).

We can hackly workaround things (as we did in [here](GoogleCloudPlatform/prometheus-engine@e02408c)), by copying some code around, but I think the
above issue proves it would be helpful to actually add context to all metadata functions and methods.

I saw googleapis#9733 we started small, but
in this PR I actually spent 1h to add context to all things for consistency.

I self-reviewed carefuly, and tried to be consistent and extra safe, but careful
review is needed, given little testing.

Some minor extra changes (we can split to new commit/PR):
* Added comment around 14s worst case
* Added comment about OnGCE semantics which was a bit surprising for us.
* When reviewing tests, I updated one test for best practices.

Alternative would be something like

```
// WithContext sets the context that will be used for all client methods that
// does not support context. This means that e.g. the context passed in
// GetWithContext will override the context in WithContext
func (c *Client) WithContext(ctx context.Context) {
	c.ctx = ctx
}

```

.. but the logic is not trivial and we introdue some state to the global variable (defaultClient), which
can be source of problems if someone sets context for them, but another package in same binary resues it etc.

Given it's trivial to add (and use) new methods, I just did it.

Signed-off-by: bwplotka <bwplotka@gmail.com>
bwplotka added a commit to GoogleCloudPlatform/prometheus-engine that referenced this pull request Jun 12, 2024
Fixes b/344740239 (edge case with GKE Metadata Server and GKE sandbox).

* Added debug logging.
* Updated metadata deps to get
googleapis/google-cloud-go#9733 & use timeout-ed
context
* Moved risky logic from FromFlags, see code comment why.
* Added regession test.

Regression test fails on prev flow (no context propagation)
 

![image](https://github.com/GoogleCloudPlatform/prometheus-engine/assets/6950331/3c0d797a-66c3-4a82-8156-72e798171e71)

### Alternatives

Everything we do in FromFlags or constructor is within readines period.
We
could consider moving potentially "slow" things on slow network or
metadata srv
to exporter.Run. This could be questionable as for GMP to work
we at end need export functionality to work, so delaying that
information or
making it surface in separation to readiness might not be helpful.

EDIT: Added PR against metadata so we can get rid of custom code in the
future: googleapis/google-cloud-go#10370
bwplotka added a commit to GoogleCloudPlatform/prometheus-engine that referenced this pull request Jun 12, 2024
Fixes b/344740239 (edge case with GKE Metadata Server and GKE sandbox).

* Added debug logging.
* Updated metadata deps to get googleapis/google-cloud-go#9733 & use timeout-ed context
* Moved risky logic from FromFlags, see code comment why.
* Added regession test.

### Alternatives

Everything we do in FromFlags or constructor is within readines period. We
could consider moving potentially "slow" things on slow network or metadata srv
to exporter.Run. This could be questionable as for GMP to work
we at end need export functionality to work, so delaying that information or
making it surface in separation to readiness might not be helpful.

Signed-off-by: bwplotka <bwplotka@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: compute Issues related to the Compute Engine API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compute/metadata: accept request context
2 participants