Skip to content

Commit 69e2f74

Browse files
authored
fix(sdk-rs): send headers correctly for usage reporting (#7364)
1 parent 6adbbb2 commit 69e2f74

File tree

8 files changed

+431
-59
lines changed

8 files changed

+431
-59
lines changed

.changeset/rude-cats-lead.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'hive-console-sdk-rs': patch
3+
---
4+
5+
Fix the bug where reports were not being sent correctly due to missing headers

.github/workflows/publish-rust.yaml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,43 @@ jobs:
3232
echo 'rust_changed=true' >> $GITHUB_OUTPUT
3333
fi
3434
35-
publish-rust:
35+
test-rust:
3636
needs: detect-changes
3737
if: needs.detect-changes.outputs.rust_changed == 'true'
38+
runs-on: ubuntu-22.04
39+
steps:
40+
- name: Checkout
41+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
42+
with:
43+
fetch-depth: 2
44+
45+
- name: setup environment
46+
uses: ./.github/actions/setup
47+
with:
48+
actor: test-rust
49+
codegen: false
50+
51+
- name: Install Protoc
52+
uses: arduino/setup-protoc@v3
53+
with:
54+
repo-token: ${{ secrets.GITHUB_TOKEN }}
55+
56+
- name: Install Rust
57+
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1
58+
with:
59+
toolchain: '1.90.0'
60+
default: true
61+
override: true
62+
63+
- name: Cache Rust
64+
uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2
65+
66+
- name: Run tests
67+
run: cargo test
68+
69+
publish-rust:
70+
needs: [detect-changes, test-rust]
71+
if: needs.detect-changes.outputs.rust_changed == 'true'
3872
strategy:
3973
fail-fast: false
4074
matrix:

.github/workflows/release-alpha.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,69 @@ jobs:
138138
cliVersion: ${{ needs.extract-cli-version.outputs.version }}
139139
publishLatest: false
140140
secrets: inherit
141+
142+
cargo:
143+
needs: [npm]
144+
runs-on: ubuntu-22.04
145+
permissions:
146+
pull-requests: write
147+
actions: write
148+
contents: write
149+
steps:
150+
- name: checkout
151+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
152+
with:
153+
fetch-depth: 2
154+
token: ${{ secrets.BOT_GITHUB_TOKEN }}
155+
156+
- name: setup environment
157+
uses: ./.github/actions/setup
158+
with:
159+
codegen: false # no need to run because release script will run it anyway
160+
actor: alpha-rust
161+
162+
- uses: dtolnay/rust-toolchain@stable
163+
with:
164+
toolchain: stable
165+
166+
- name: extract published plugin version
167+
id: plugin-crate
168+
if:
169+
needs.npm.outputs.published && contains(needs.npm.outputs.publishedPackages,
170+
'"hive-apollo-router-plugin"')
171+
run: |
172+
cargo install set-cargo-version
173+
echo '${{needs.npm.outputs.publishedPackages}}' > published.json
174+
175+
VERSION=`echo $(jq -r '.[] | select(.name | endswith("hive-apollo-router-plugin")).version' published.json)`
176+
set-cargo-version ./packages/libraries/router/Cargo.toml $VERSION
177+
178+
- name: extract published sdk version
179+
id: sdk-crate
180+
if:
181+
needs.npm.outputs.published && contains(needs.npm.outputs.publishedPackages,
182+
'"hive-console-sdk-rs"')
183+
run: |
184+
cargo install set-cargo-version
185+
echo '${{needs.npm.outputs.publishedPackages}}' > published.json
186+
187+
VERSION=`echo $(jq -r '.[] | select(.name | endswith("hive-console-sdk-rs")).version' published.json)`
188+
set-cargo-version ./packages/libraries/sdk-rs/Cargo.toml $VERSION
189+
190+
echo "crate_publish=true" >> $GITHUB_OUTPUT
191+
192+
# Find and replace the version of "hive-console-sdk" dependency in router Cargo.toml
193+
if [ -f ./packages/libraries/router/Cargo.toml ]; then
194+
SDK_VERSION_LINE=$(grep -n 'hive-console-sdk' ./packages/libraries/router/Cargo.toml | head -n 1 | cut -d: -f1)
195+
if [ ! -z "$SDK_VERSION_LINE" ]; then
196+
sed -i "${SDK_VERSION_LINE}s/version = \".*\"/version = \"$VERSION\"/" ./packages/libraries/router/Cargo.toml
197+
fi
198+
fi
199+
200+
- name: release to Crates.io
201+
if:
202+
steps.sdk-crate.outputs.crate_publish == 'true' ||
203+
steps.plugin-crate.outputs.crate_publish == 'true'
204+
run: |
205+
cargo login ${{ secrets.CARGO_REGISTRY_TOKEN }}
206+
cargo publish --allow-dirty --no-verify

.github/workflows/release-stable.yaml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,10 @@ jobs:
112112
VERSION: ${{ steps.cli.outputs.version }}
113113
run: pnpm oclif promote --no-xz --sha ${GITHUB_SHA:0:7} --version $VERSION
114114

115-
- name: extract published Crate version
116-
id: rust-crate
115+
- name: release to Crates.io
117116
if:
118117
steps.changesets.outputs.published && contains(steps.changesets.outputs.publishedPackages,
119118
'"hive-apollo-router-plugin"')
120-
run: |
121-
echo '${{steps.changesets.outputs.publishedPackages}}' > published.json
122-
VERSION=`echo $(jq -r '.[] | select(.name | endswith("hive-apollo-router-plugin")).version' published.json)`
123-
echo "crate_version=$VERSION" >> $GITHUB_OUTPUT
124-
echo "crate_publish=true" >> $GITHUB_OUTPUT
125-
126-
- name: release to Crates.io
127-
if: steps.rust-crate.outputs.crate_publish == 'true'
128119
run: |
129120
cargo login ${{ secrets.CARGO_REGISTRY_TOKEN }}
130121
cargo publish --allow-dirty --no-verify

configs/cargo/Cargo.lock

Lines changed: 87 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/libraries/router/src/usage.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,14 @@ mod hive_usage_tests {
432432
plugin::{test::MockSupergraphService, Plugin, PluginInit},
433433
services::supergraph,
434434
};
435+
use http::header::{AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
435436
use httpmock::{Method::POST, Mock, MockServer};
436437
use jsonschema::Validator;
437438
use serde_json::json;
438439
use tower::ServiceExt;
439440

441+
use crate::consts::PLUGIN_VERSION;
442+
440443
use super::{Config, UsagePlugin};
441444

442445
lazy_static::lazy_static! {
@@ -481,17 +484,26 @@ mod hive_usage_tests {
481484

482485
fn activate_usage_mock(&'_ self) -> Mock<'_> {
483486
self.mocked_upstream.mock(|when, then| {
484-
when.method(POST).path("/usage").matches(|r| {
485-
// This mock also validates that the content of the reported usage is valid
486-
// when it comes to the JSON schema validation.
487-
// if it does not match, the request matching will fail and this will lead
488-
// to a failed assertion
489-
let body = r.body.as_ref().unwrap();
490-
let body = String::from_utf8(body.to_vec()).unwrap();
491-
let body = serde_json::from_str(&body).unwrap();
492-
493-
SCHEMA_VALIDATOR.is_valid(&body)
494-
});
487+
when.method(POST)
488+
.path("/usage")
489+
.header(CONTENT_TYPE.as_str(), "application/json")
490+
.header(
491+
USER_AGENT.as_str(),
492+
format!("hive-apollo-router/{}", PLUGIN_VERSION),
493+
)
494+
.header(AUTHORIZATION.as_str(), "Bearer 123")
495+
.header("X-Usage-API-Version", "2")
496+
.matches(|r| {
497+
// This mock also validates that the content of the reported usage is valid
498+
// when it comes to the JSON schema validation.
499+
// if it does not match, the request matching will fail and this will lead
500+
// to a failed assertion
501+
let body = r.body.as_ref().unwrap();
502+
let body = String::from_utf8(body.to_vec()).unwrap();
503+
let body = serde_json::from_str(&body).unwrap();
504+
505+
SCHEMA_VALIDATOR.is_valid(&body)
506+
});
495507
then.status(200);
496508
})
497509
}
@@ -576,21 +588,4 @@ mod hive_usage_tests {
576588
mock.assert();
577589
mock.assert_hits(1);
578590
}
579-
580-
#[tokio::test]
581-
async fn invalid_query_reported() {
582-
let instance = UsageTestHelper::new().await;
583-
let req = supergraph::Request::fake_builder()
584-
.query("query {")
585-
.build()
586-
.unwrap();
587-
let mock = instance.activate_usage_mock();
588-
589-
instance.execute_operation(req).await.next_response().await;
590-
591-
instance.wait_for_processing().await;
592-
593-
mock.assert();
594-
mock.assert_hits(1);
595-
}
596591
}

0 commit comments

Comments
 (0)