I provide another way which is set the credentials manually. For local development and running on GCP, like Cloud Function, Compute Engine.
You can use a service account and grant this service account proper permission. e.g. Cloud Trace Admin
Set private_key
, client_email
and projectId
options for any GCP client library. These options you can get from the service account json file.
For example, I am using Cloud Trace Node.js client library in my cloud functions.
Before I set the credentials
and projectId
, I got this error:
Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information. at GoogleAuth. (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:248:31) at step (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:47:23) at Object.next (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:28:53) at fulfilled (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)
You can pass the credentials
and projectId
as environment variables. After setting credentials
and projectId
, the error is gone.
const tracer = require('@google-cloud/trace-agent').start({
samplingRate: 0,
bufferSize: 1,
credentials: {
client_email: process.env.CLIENT_EMAIL,
private_key: process.env.TRACE_AGENT_ADMIN,
},
projectId: process.env.X_GOOGLE_GCLOUD_PROJECT || process.env.PROJECT_ID,
});
X_GOOGLE_GCLOUD_PROJECT
is a built-in environment variable for cloud function runtime