grpcserver

package
v2.58.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 4 Imported by: 0

README

grpcserver

gRPC server interceptors for LabKit v2. The gRPC counterpart to httpserver.

Correlation IDs

UnaryServerCorrelationInterceptor and StreamServerCorrelationInterceptor resolve a correlation ID for every incoming call and make it available to the handler via correlation.ExtractFromContext:

srv := grpc.NewServer(
	grpc.ChainUnaryInterceptor(grpcserver.UnaryServerCorrelationInterceptor()),
	grpc.ChainStreamInterceptor(grpcserver.StreamServerCorrelationInterceptor()),
)

Chain them outermost so every later interceptor and the handler observe the ID.

Behaviour, mirroring httpserver.CorrelationIDMiddleware:

  • Reads x-gitlab-correlation-id from incoming metadata (CorrelationIDHeader). This is LabKit v1's gRPC header, so v1 clients propagate into v2 servers unchanged. Metadata keys are case-insensitive.
  • Validates the value with correlation.Sanitize: trimmed, non-empty, at most correlation.MaxIDLen (255) characters, only [a-zA-Z0-9_-]. Anything else is replaced with a freshly generated UUID, which prevents log injection and downstream poisoning from caller-supplied values.
  • Echoes the resolved ID back to the client as a response header under the same key.

Note: v1's correlation/grpc interceptors accepted the incoming value verbatim. Services migrating from v1 gain validation; callers sending IDs outside the allowed character set will see them replaced. Generated IDs are UUIDs (as on v2's HTTP path), not v1's ULIDs.

Do not mix with v1: v1 and v2 store the ID under different context keys, so an ID injected here is invisible to v1's correlation and log packages and correlation_id silently disappears from v1 access logs. Migrate the correlation interceptor and the access logger together.

Documentation

Overview

Package grpcserver provides gRPC server interceptors that integrate with LabKit v2's correlation and logging packages. It is the gRPC counterpart to package httpserver.

Chain the correlation interceptors outermost so every later interceptor and the service handler observe the correlation ID via correlation.ExtractFromContext:

srv := grpc.NewServer(
	grpc.ChainUnaryInterceptor(grpcserver.UnaryServerCorrelationInterceptor()),
	grpc.ChainStreamInterceptor(grpcserver.StreamServerCorrelationInterceptor()),
)

Index

Constants

View Source
const CorrelationIDHeader = "x-gitlab-correlation-id"

CorrelationIDHeader is the incoming metadata key read for a caller-supplied correlation ID, and the response header key the resolved ID is echoed back on. It matches LabKit v1's gRPC header so v1 clients propagate into v2 servers unchanged. gRPC metadata keys are case-insensitive.

Variables

This section is empty.

Functions

func StreamServerCorrelationInterceptor

func StreamServerCorrelationInterceptor() grpc.StreamServerInterceptor

StreamServerCorrelationInterceptor is the stream equivalent of UnaryServerCorrelationInterceptor. The handler receives a wrapped stream whose Context carries the correlation ID.

func UnaryServerCorrelationInterceptor

func UnaryServerCorrelationInterceptor() grpc.UnaryServerInterceptor

UnaryServerCorrelationInterceptor returns a unary server interceptor that reads the correlation ID from incoming metadata, validates it with correlation.Sanitize (generating a UUID when absent or invalid), injects it into the handler context, and echoes it back as a response header.

Chain it outermost so later interceptors and the handler see the ID.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL