github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/remotestorage/events_interceptor.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remotestorage
    16  
    17  import (
    18  	"context"
    19  
    20  	"google.golang.org/grpc"
    21  
    22  	eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
    23  	"github.com/dolthub/dolt/go/libraries/events"
    24  )
    25  
    26  func EventsUnaryClientInterceptor(collector *events.Collector) grpc.UnaryClientInterceptor {
    27  	return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    28  		errCount := int32(0)
    29  		if event, ok := remoteAPIMethodsToEvents[method]; ok {
    30  			evt := events.NewEvent(event)
    31  			defer func() {
    32  				if errCount > 0 {
    33  					counter := events.NewCounter(eventsapi.MetricID_REMOTEAPI_RPC_ERROR)
    34  					counter.Add(errCount)
    35  					evt.AddMetric(counter)
    36  				}
    37  				collector.CloseEventAndAdd(evt)
    38  			}()
    39  		}
    40  		err := invoker(ctx, method, req, reply, cc, opts...)
    41  		if err != nil {
    42  			errCount++
    43  		}
    44  		return err
    45  	}
    46  }
    47  
    48  var remoteAPIMethodsToEvents map[string]eventsapi.ClientEventType = map[string]eventsapi.ClientEventType{
    49  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/GetRepoMetadata":      eventsapi.ClientEventType_REMOTEAPI_GET_REPO_METADATA,
    50  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/HasChunks":            eventsapi.ClientEventType_REMOTEAPI_HAS_CHUNKS,
    51  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/GetDownloadLocations": eventsapi.ClientEventType_REMOTEAPI_GET_DOWNLOAD_LOCATIONS,
    52  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/GetUploadLocations":   eventsapi.ClientEventType_REMOTEAPI_GET_UPLOAD_LOCATIONS,
    53  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/Rebase":               eventsapi.ClientEventType_REMOTEAPI_REBASE,
    54  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/Root":                 eventsapi.ClientEventType_REMOTEAPI_ROOT,
    55  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/Commit":               eventsapi.ClientEventType_REMOTEAPI_COMMIT,
    56  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/ListTableFiles":       eventsapi.ClientEventType_REMOTEAPI_LIST_TABLE_FILES,
    57  	"/dolt.services.remotesapi.v1alpha1.ChunkStoreService/AddTableFiles":        eventsapi.ClientEventType_REMOTEAPI_ADD_TABLE_FILES,
    58  }