@@ -95,6 +95,15 @@ const (
95
95
96
96
// Part of the error message when the payload contains invalid UTF-8 characters.
97
97
utfErrorString = "string field contains invalid UTF-8"
98
+
99
+ // DetectProjectID is a sentinel value that instructs NewClient to detect the
100
+ // project ID. It is given in place of the projectID argument. NewClient will
101
+ // use the project ID from the given credentials or the default credentials
102
+ // (https://developers.google.com/accounts/docs/application-default-credentials)
103
+ // if no credentials were provided. When providing credentials, not all
104
+ // options will allow NewClient to extract the project ID. Specifically a JWT
105
+ // does not have the project ID encoded.
106
+ DetectProjectID = "*detect-project-id*"
98
107
)
99
108
100
109
var (
@@ -103,8 +112,9 @@ var (
103
112
ErrRedirectProtoPayloadNotSupported = errors .New ("printEntryToStdout: cannot find valid payload" )
104
113
105
114
// For testing:
106
- now = time .Now
107
- toLogEntryInternal = toLogEntryInternalImpl
115
+ now = time .Now
116
+ toLogEntryInternal = toLogEntryInternalImpl
117
+ detectResourceInternal = detectResource
108
118
109
119
// ErrOverflow signals that the number of buffered entries for a Logger
110
120
// exceeds its BufferLimit.
@@ -148,9 +158,12 @@ type Client struct {
148
158
// billingAccounts/ACCOUNT_ID
149
159
// organizations/ORG_ID
150
160
//
151
- // for backwards compatibility, a string with no '/' is also allowed and is interpreted
161
+ // For backwards compatibility, a string with no '/' is also allowed and is interpreted
152
162
// as a project ID.
153
163
//
164
+ // If logging.DetectProjectId is provided as the parent, the parent will be interpreted as a project
165
+ // ID, and its value will be inferred from the environment.
166
+ //
154
167
// By default NewClient uses WriteScope. To use a different scope, call
155
168
// NewClient using a WithScopes option (see https://godoc.org/google.golang.org/api/option#WithScopes).
156
169
func NewClient (ctx context.Context , parent string , opts ... option.ClientOption ) (* Client , error ) {
@@ -192,6 +205,13 @@ func NewClient(ctx context.Context, parent string, opts ...option.ClientOption)
192
205
193
206
func makeParent (parent string ) (string , error ) {
194
207
if ! strings .ContainsRune (parent , '/' ) {
208
+ if parent == DetectProjectID {
209
+ resource := detectResourceInternal ()
210
+ if resource == nil {
211
+ return parent , fmt .Errorf ("could not determine project ID from environment" )
212
+ }
213
+ parent = resource .Labels ["project_id" ]
214
+ }
195
215
return "projects/" + parent , nil
196
216
}
197
217
prefix := strings .Split (parent , "/" )[0 ]
@@ -301,7 +321,7 @@ func (r *loggerRetryer) Retry(err error) (pause time.Duration, shouldRetry bool)
301
321
// characters: [A-Za-z0-9]; and punctuation characters: forward-slash,
302
322
// underscore, hyphen, and period.
303
323
func (c * Client ) Logger (logID string , opts ... LoggerOption ) * Logger {
304
- r := detectResource ()
324
+ r := detectResourceInternal ()
305
325
if r == nil {
306
326
r = monitoredResource (c .parent )
307
327
}
0 commit comments