Skip to content

Commit

Permalink
Support GOOGLE_CLOUD_LABELS too (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Jun 15, 2022
1 parent d46a823 commit f8e2f18
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-onions-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@honestfoodcompany/pubsub': patch
---

Prefer GOOGLE_CLOUD_LABELS over PUBSUB_LABELS env var
2 changes: 1 addition & 1 deletion docs/getting-started/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The framework expects the following environment variables. They can be added to
| `root-dir` | `PUBSUB_ROOT_DIR` | must be the path to your project's pubsub directory. |
| `google-application-credentials` | `GOOGLE_APPLICATION_CREDENTIALS` | see <https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account> to generate this |
| `project-id` | `GOOGLE_CLOUD_PROJECT` | the project-id in Google Cloud Platform |
| `labels` | `PUBSUB_LABELS` | Labels in stringified JSON format |
| `labels` | `GOOGLE_CLOUD_LABELS` | Labels in stringified JSON format |
| `server-port` | `PUBSUB_SERVER_PORT` | PORT at which the pubsub should run the server at |
| `health-server` | `PUBSUB_HEALTH_SERVER` | If value assigned is true this would run a server showing health state and return 500 if not healthy |

Expand Down
4 changes: 2 additions & 2 deletions docs/subscribing/Labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Subscription Service can have global labels in `defaultSubscriberOptions`, these

### Env Var

We also support `PUBSUB_LABELS` env var which can be a stringified JSON. These work as the default labels added to each subscription. Example:
We also support `GOOGLE_CLOUD_LABELS` env var which can be a stringified JSON. These work as the default labels added to each subscription. Example:

```
# .env file
PUBSUB_LABELS='{"service": "hfc-pubsub", "env": "prod"}'
GOOGLE_CLOUD_LABELS='{"service": "hfc-pubsub", "env": "prod"}'
```

## Subscriber Specific Labels
Expand Down
2 changes: 1 addition & 1 deletion src/cli/env.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const envMapping: any = {
projectId: 'GOOGLE_CLOUD_PROJECT',
googleApplicationCredentials: 'GOOGLE_APPLICATION_CREDENTIALS',
rootDir: 'PUBSUB_ROOT_DIR',
labels: 'PUBSUB_LABELS',
labels: 'GOOGLE_CLOUD_LABELS',
driver: 'PUBSUB_DRIVER',
healthServer: 'PUBSUB_HEALTH_SERVER',
serverPort: 'PUBSUB_SERVER_PORT',
Expand Down
8 changes: 5 additions & 3 deletions src/client/googlePubSub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,18 @@ export default class GooglePubSubAdapter implements PubSubClientV2 {
const ackDeadlineSeconds = subscriberOptions?.ackDeadline;
const labels = subscriberOptions?.labels || {};
try {
if (process.env.PUBSUB_LABELS) {
const parsedLabels = JSON.parse(process.env.PUBSUB_LABELS);
const labelsStringified =
process.env.GOOGLE_CLOUD_LABELS || process.env.PUBSUB_LABELS;
if (labelsStringified) {
const parsedLabels = JSON.parse(labelsStringified);
Object.entries(parsedLabels).forEach(([key, val]) => {
if (labels[key] == null) {
labels[key] = val as string;
}
});
}
} catch (err) {
this.log('Invalid PUBSUB_LABELS');
this.log('Invalid GOOGLE_CLOUD_LABELS');
}
return {
...subscriberOptions,
Expand Down

0 comments on commit f8e2f18

Please sign in to comment.