fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

Use sync.OnceValue for metrics registration (#815)

Inspired by a recent Sourcegraph refactor.

+26 -40
+16 -23
cmd/zoekt-sourcegraph-indexserver/main.go
··· 132 132 Help: "Counts the number of repos we stopped tracking.", 133 133 }) 134 134 135 - clientMetricsOnce sync.Once 136 - clientMetrics *grpcprom.ClientMetrics 135 + // clientMetricsOnce returns a singleton instance of the client metrics 136 + // that are shared across all gRPC clients that this process creates. 137 + // 138 + // This function panics if the metrics cannot be registered with the default 139 + // Prometheus registry. 140 + clientMetricsOnce = sync.OnceValue(func() *grpcprom.ClientMetrics { 141 + clientMetrics := grpcprom.NewClientMetrics( 142 + grpcprom.WithClientCounterOptions(), 143 + grpcprom.WithClientHandlingTimeHistogram(), // record the overall request latency for a gRPC request 144 + grpcprom.WithClientStreamRecvHistogram(), // record how long it takes for a client to receive a message during a streaming RPC 145 + grpcprom.WithClientStreamSendHistogram(), // record how long it takes for a client to send a message during a streaming RPC 146 + ) 147 + prometheus.DefaultRegisterer.MustRegister(clientMetrics) 148 + return clientMetrics 149 + }) 137 150 ) 138 151 139 152 // 1 MB; match https://sourcegraph.sgdev.org/github.com/sourcegraph/sourcegraph/-/blob/cmd/symbols/internal/symbols/search.go#L22 ··· 1505 1518 const defaultGRPCMessageReceiveSizeBytes = 90 * 1024 * 1024 // 90 MB 1506 1519 1507 1520 func dialGRPCClient(addr string, logger sglog.Logger, additionalOpts ...grpc.DialOption) (proto.ZoektConfigurationServiceClient, error) { 1508 - metrics := mustGetClientMetrics() 1521 + metrics := clientMetricsOnce() 1509 1522 1510 1523 // If the service seems to be unavailable, this 1511 1524 // will retry after [1s, 2s, 4s, 8s, 16s] with a jitterFraction of .1 ··· 1557 1570 1558 1571 client := proto.NewZoektConfigurationServiceClient(cc) 1559 1572 return client, nil 1560 - } 1561 - 1562 - // mustGetClientMetrics returns a singleton instance of the client metrics 1563 - // that are shared across all gRPC clients that this process creates. 1564 - // 1565 - // This function panics if the metrics cannot be registered with the default 1566 - // Prometheus registry. 1567 - func mustGetClientMetrics() *grpcprom.ClientMetrics { 1568 - clientMetricsOnce.Do(func() { 1569 - clientMetrics = grpcprom.NewClientMetrics( 1570 - grpcprom.WithClientCounterOptions(), 1571 - grpcprom.WithClientHandlingTimeHistogram(), // record the overall request latency for a gRPC request 1572 - grpcprom.WithClientStreamRecvHistogram(), // record how long it takes for a client to receive a message during a streaming RPC 1573 - grpcprom.WithClientStreamSendHistogram(), // record how long it takes for a client to send a message during a streaming RPC 1574 - ) 1575 - 1576 - prometheus.DefaultRegisterer.MustRegister(clientMetrics) 1577 - }) 1578 - 1579 - return clientMetrics 1580 1573 } 1581 1574 1582 1575 // addDefaultPort adds a default port to a URL if one is not specified.
+10 -17
cmd/zoekt-webserver/main.go
··· 639 639 } 640 640 641 641 func newGRPCServer(logger sglog.Logger, streamer zoekt.Streamer, additionalOpts ...grpc.ServerOption) *grpc.Server { 642 - metrics := mustGetServerMetrics() 642 + metrics := serverMetricsOnce() 643 643 644 644 opts := []grpc.ServerOption{ 645 645 grpc.ChainStreamInterceptor( ··· 689 689 Help: "The total number of search requests that zoekt received", 690 690 }) 691 691 692 - serverMetricsOnce sync.Once 693 - serverMetrics *grpcprom.ServerMetrics 694 - ) 695 - 696 - // mustGetServerMetrics returns a singleton instance of the server metrics 697 - // that are shared across all gRPC servers that this process creates. 698 - // 699 - // This function panics if the metrics cannot be registered with the default 700 - // Prometheus registry. 701 - func mustGetServerMetrics() *grpcprom.ServerMetrics { 702 - serverMetricsOnce.Do(func() { 703 - serverMetrics = grpcprom.NewServerMetrics( 692 + // serviceMetricsOnce returns a singleton instance of the server metrics 693 + // that are shared across all gRPC servers that this process creates. 694 + // 695 + // This function panics if the metrics cannot be registered with the default 696 + // Prometheus registry. 697 + serverMetricsOnce = sync.OnceValue(func() *grpcprom.ServerMetrics { 698 + serverMetrics := grpcprom.NewServerMetrics( 704 699 grpcprom.WithServerCounterOptions(), 705 700 grpcprom.WithServerHandlingTimeHistogram(), // record the overall response latency for a gRPC request) 706 701 ) 707 - 708 702 prometheus.DefaultRegisterer.MustRegister(serverMetrics) 703 + return serverMetrics 709 704 }) 710 - 711 - return serverMetrics 712 - } 705 + )