...

Source file src/google.golang.org/grpc/channelz/internal/protoconv/server.go

Documentation: google.golang.org/grpc/channelz/internal/protoconv

     1  /*
     2   *
     3   * Copyright 2024 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package protoconv
    20  
    21  import (
    22  	"time"
    23  
    24  	"google.golang.org/grpc/codes"
    25  	"google.golang.org/grpc/internal/channelz"
    26  	"google.golang.org/grpc/status"
    27  	"google.golang.org/protobuf/types/known/timestamppb"
    28  
    29  	channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
    30  )
    31  
    32  func serverToProto(sm *channelz.Server) *channelzpb.Server {
    33  	s := &channelzpb.Server{}
    34  	s.Ref = &channelzpb.ServerRef{ServerId: sm.ID, Name: sm.RefName}
    35  
    36  	s.Data = &channelzpb.ServerData{
    37  		CallsStarted:   sm.ServerMetrics.CallsStarted.Load(),
    38  		CallsSucceeded: sm.ServerMetrics.CallsSucceeded.Load(),
    39  		CallsFailed:    sm.ServerMetrics.CallsFailed.Load(),
    40  	}
    41  
    42  	if ts := timestamppb.New(time.Unix(0, sm.ServerMetrics.LastCallStartedTimestamp.Load())); ts.IsValid() {
    43  		s.Data.LastCallStartedTimestamp = ts
    44  	}
    45  	lss := sm.ListenSockets()
    46  	sockets := make([]*channelzpb.SocketRef, 0, len(lss))
    47  	for id, ref := range lss {
    48  		sockets = append(sockets, &channelzpb.SocketRef{SocketId: id, Name: ref})
    49  	}
    50  	s.ListenSocket = sockets
    51  	return s
    52  }
    53  
    54  // GetServers returns the protobuf representation of the servers starting at
    55  // startID (max of len), and returns end=true if no servers exist with higher
    56  // IDs.
    57  func GetServers(startID int64, len int) (servers []*channelzpb.Server, end bool) {
    58  	srvs, end := channelz.GetServers(startID, len)
    59  	for _, srv := range srvs {
    60  		servers = append(servers, serverToProto(srv))
    61  	}
    62  	return servers, end
    63  }
    64  
    65  // GetServer returns the protobuf representation of the server with the given
    66  // ID.
    67  func GetServer(id int64) (*channelzpb.Server, error) {
    68  	srv := channelz.GetServer(id)
    69  	if srv == nil {
    70  		return nil, status.Errorf(codes.NotFound, "requested server %d not found", id)
    71  	}
    72  	return serverToProto(srv), nil
    73  }
    74  

View as plain text