...

Source file src/edge-infra.dev/pkg/lib/fog/operation.go

Documentation: edge-infra.dev/pkg/lib/fog

     1  package fog
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	guuid "github.com/google/uuid"
     8  )
     9  
    10  type operationCtxKey struct{}
    11  
    12  // OperationFields provides a keysAndValues string array compatible interface for the Operation field in GCP,
    13  // for use in fmt and logr.Logger style logging. e.g., logr.Logger.WithValues(OperationField(id)...)
    14  func OperationFields(opID string) []interface{} {
    15  	ret := make([]interface{}, 2)
    16  	ret[0] = fmt.Sprintf("%sid", gcpOperationKey)
    17  	ret[1] = opID
    18  	return ret
    19  }
    20  
    21  // Adds operation ID to context
    22  func SetOperationContext(ctx context.Context) context.Context {
    23  	c := context.WithValue(ctx, operationCtxKey{}, guuid.New().String())
    24  	return c
    25  }
    26  
    27  func OperationID(ctx context.Context) string {
    28  	if o, ok := ctx.Value(operationCtxKey{}).(string); ok {
    29  		return o
    30  	}
    31  	return ""
    32  }
    33  

View as plain text