grpc/grpc-go · error

cannot have a leading slash

Error message

cannot have a leading slash

What it means

Returned by validateMethodString in the observability package when a method pattern string starts with '/'. Method patterns in the observability config use the form service/method WITHOUT a leading slash (unlike the full gRPC method path which includes one). The leading slash is reserved for the runtime method identifier, not for config patterns.

Solutions

  1. Remove the leading slash from method patterns in the observability config: use foo.Bar/Baz, not /foo.Bar/Baz.
  2. When generating config programmatically, strip the leading / from grpc method full names before inserting them.

Example fix

// before (GRPC_GCP_OBSERVABILITY_CONFIG)
{"cloud_logging":{"client_rpc_events":[{"methods":["/foo.Bar/Baz"]}]}}
// after
{"cloud_logging":{"client_rpc_events":[{"methods":["foo.Bar/Baz"]}]}}
Defensive patterns

Strategy: validation

Validate before calling

// Strip leading slash before inserting into observability config:
method := strings.TrimPrefix(fullMethodName, "/")
if strings.HasPrefix(method, "/") {
    return fmt.Errorf("method pattern must not have a leading slash: %s", method)
}

Type guard

func isValidMethodPattern(m string) bool {
    return !strings.HasPrefix(m, "/") && strings.Contains(m, "/")
}

Prevention

When it happens

Trigger: Setting a method entry in the observability CloudLogging config (clientRPCEvents.methods or serverRPCEvents.methods) to a value like /foo.Bar/Baz instead of foo.Bar/Baz. The config is parsed from GRPC_GCP_OBSERVABILITY_CONFIG env var or the config file.

Common situations: Developers copy the method name from gRPC server logs or reflection output (which include the leading /) into the observability config. The inconsistency between the gRPC method path format (/pkg.Svc/Method) and the config pattern format (pkg.Svc/Method) is the root cause.

Related errors


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

Appendix: 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 0c51461d27)