...

Source file src/google.golang.org/grpc/channelz/internal/protoconv/subchannel.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 subChannelToProto(cm *channelz.SubChannel) *channelzpb.Subchannel {
    33  	sc := &channelzpb.Subchannel{}
    34  	sc.Ref = &channelzpb.SubchannelRef{SubchannelId: cm.ID, Name: cm.RefName}
    35  
    36  	sc.Data = &channelzpb.ChannelData{
    37  		State:          connectivityStateToProto(cm.ChannelMetrics.State.Load()),
    38  		Target:         strFromPointer(cm.ChannelMetrics.Target.Load()),
    39  		CallsStarted:   cm.ChannelMetrics.CallsStarted.Load(),
    40  		CallsSucceeded: cm.ChannelMetrics.CallsSucceeded.Load(),
    41  		CallsFailed:    cm.ChannelMetrics.CallsFailed.Load(),
    42  	}
    43  	if ts := timestamppb.New(time.Unix(0, cm.ChannelMetrics.LastCallStartedTimestamp.Load())); ts.IsValid() {
    44  		sc.Data.LastCallStartedTimestamp = ts
    45  	}
    46  
    47  	skts := cm.Sockets()
    48  	sockets := make([]*channelzpb.SocketRef, 0, len(skts))
    49  	for id, ref := range skts {
    50  		sockets = append(sockets, &channelzpb.SocketRef{SocketId: id, Name: ref})
    51  	}
    52  	sc.SocketRef = sockets
    53  	sc.Data.Trace = channelTraceToProto(cm.Trace())
    54  	return sc
    55  }
    56  
    57  // GetSubChannel returns the protobuf representation of the subchannel with the
    58  // given ID.
    59  func GetSubChannel(id int64) (*channelzpb.Subchannel, error) {
    60  	subChan := channelz.GetSubChannel(id)
    61  	if subChan == nil {
    62  		return nil, status.Errorf(codes.NotFound, "requested sub channel %d not found", id)
    63  	}
    64  	return subChannelToProto(subChan), nil
    65  }
    66  

View as plain text