grpc/grpc-go · error

cannot have a leading slash

Error message

cannot have a leading slash

What it means

Returned by validateMethodString (gcp/observability/config.go:61) when the method string passed to an observability logging config starts with '/'. Observability method filters use the form <service>/<method> WITHOUT a leading slash (the leading slash is the gRPC wire format, but the config rejects it). It is reached via validateLogEventMethod -> validateLoggingEvents -> unmarshalAndVerifyConfig while parsing GRPC_GCP_OBSERVABILITY_CONFIG.

Source

Thrown at gcp/observability/config.go:61

	// Step 2: Check default credential
	credentials, err := google.FindDefaultCredentials(ctx, gcplogging.WriteScope)
	if err != nil {
		logger.Infof("Failed to locate Google Default Credential: %v", err)
		return ""
	}
	if credentials.ProjectID == "" {
		logger.Infof("Failed to find project ID in default credential: %v", err)
		return ""
	}
	logger.Infof("Found project ID from Google Default Credential: %v", credentials.ProjectID)
	return credentials.ProjectID
}

// validateMethodString validates whether the string passed in is a valid
// pattern.
func validateMethodString(method string) error {
	if strings.HasPrefix(method, "/") {
		return errors.New("cannot have a leading slash")
	}
	serviceMethod := strings.Split(method, "/")
	if len(serviceMethod) != 2 {
		return errors.New("/ must come in between service and method, only one /")
	}
	if serviceMethod[1] == "" {
		return errors.New("method name must be non empty")
	}
	if serviceMethod[0] == "*" {
		return errors.New("cannot have service wildcard * i.e. (*/m)")
	}
	return nil
}

func validateLogEventMethod(methods []string, exclude bool) error {
	for _, method := range methods {
		if method == "*" {
			if exclude {

View on GitHub (pinned to 03255a9237)

Solutions

  1. Remove the leading '/' from each method entry — use pkg.Svc/Method.
  2. If generating the config programmatically, strings.TrimPrefix(method, "/") each entry.
  3. Validate your config JSON against the documented form before deploying.

Example fix

// before (env var)
GRPC_GCP_OBSERVABILITY_CONFIG='{"project_id":"p","cloud_logging":{"client_rpc_events":[{"methods":["/foo.Bar/Baz"]}]}}'
// error: cannot have a leading slash

// after
GRPC_GCP_OBSERVABILITY_CONFIG='{"project_id":"p","cloud_logging":{"client_rpc_events":[{"methods":["foo.Bar/Baz"]}]}}'
Defensive patterns

Strategy: validation

Validate before calling

// Mirror the library's rule to catch bad entries before/after config parse.
func validateMethod(method string) error {
    if strings.HasPrefix(method, "/") {
        return fmt.Errorf("method %q: cannot have a leading slash", method)
    }
    parts := strings.Split(method, "/")
    if len(parts) != 2 {
        return fmt.Errorf("method %q: need exactly one '/'", method)
    }
    if parts[1] == "" {
        return fmt.Errorf("method %q: method name empty", method)
    }
    return nil
}
for _, m := range cfgMethods {
    m = strings.TrimPrefix(m, "/") // auto-fix leading slash
    if err := validateMethod(m); err != nil { return err }
}

Try / catch

cfg, err := parseObservabilityConfig()
if err != nil { log.Fatalf("observability config: %v", err) }

Prevention

When it happens

Trigger: Setting client_rpc_events/server_rpc_events methods entries like "/pkg.Svc/Method" (with leading slash) in the observability JSON config. Also triggered programmatically if a caller invokes validateMethodString with such input.

Common situations: Copy-pasting the method name from grpc method invocations (which use /pkg.Svc/Method on the wire) into the config; docs confusion between wire form and config form; auto-generating config from protobuf full names and prepending '/'

Related errors


AI-assisted analysis of grpc/grpc-go@03255a9237 (2026-08-07). Data as JSON: /api/errors/7bf004dbdefd3c4a. Report an issue: GitHub.