github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/asyncworker/helpers.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package asyncworker
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"reflect"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    25  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    26  )
    27  
    28  func (s *workStatus) IsWorking() bool {
    29  	return s.working
    30  }
    31  
    32  func validateWork(work *Work) (err error) {
    33  	if work == nil {
    34  		return fmt.Errorf("nil work")
    35  	} else if work.Fn == nil {
    36  		return fmt.Errorf("nil work Fn")
    37  	} else if work.DeliveredAt.IsZero() {
    38  		return fmt.Errorf("zero work DeliveredAt")
    39  	}
    40  
    41  	fnType := reflect.TypeOf(work.Fn)
    42  	if fnType.Kind() != reflect.Func {
    43  		return fmt.Errorf("work Fn isn't a function")
    44  	} else if fnType.NumOut() != 1 || !fnType.Out(0).Implements(reflect.TypeOf(&err).Elem()) {
    45  		return fmt.Errorf("work Fn doesn't return only an error, numOut: %d", fnType.NumOut())
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // parseContextMetricEmitter parses and returns metrics.MetricEmitter object from given context
    52  // returns false if not found
    53  func parseContextMetricEmitter(ctx context.Context) (metrics.MetricEmitter, bool) {
    54  	v := ctx.Value(contextKeyMetricEmitter)
    55  	if v == nil {
    56  		return metrics.DummyMetrics{}, false
    57  	}
    58  
    59  	if emitter, ok := v.(metrics.MetricEmitter); ok {
    60  		return emitter, true
    61  	}
    62  	return metrics.DummyMetrics{}, false
    63  }
    64  
    65  // parseContextMetricName parses and returns metricName from given context
    66  // returns false if not found
    67  func parseContextMetricName(ctx context.Context) (string, bool) {
    68  	v := ctx.Value(contextKeyMetricName)
    69  	if v == nil {
    70  		return "", false
    71  	}
    72  
    73  	if name, ok := v.(string); ok {
    74  		return name, true
    75  	}
    76  	return "", false
    77  }
    78  
    79  // EmitAsyncedMetrics emit metrics through metricEmitter and metricName parsed from context
    80  func EmitAsyncedMetrics(ctx context.Context, tags ...metrics.MetricTag) error {
    81  	emitter, ok := parseContextMetricEmitter(ctx)
    82  	if !ok {
    83  		general.Errorf("failed to get metrics-emitter")
    84  		return nil
    85  	}
    86  
    87  	name, ok := parseContextMetricName(ctx)
    88  	if !ok {
    89  		general.Errorf("failed to get metrics-name")
    90  		return nil
    91  	}
    92  
    93  	return emitter.StoreInt64(name, 1, metrics.MetricTypeNameRaw, tags...)
    94  }
    95  
    96  // EmitAsyncedMetrics emit metrics through metricEmitter parsed from context and provided metricName & metricValue & tags
    97  func EmitCustomizedAsyncedMetrics(ctx context.Context, metricName string, metricValue int64, tags ...metrics.MetricTag) error {
    98  	emitter, ok := parseContextMetricEmitter(ctx)
    99  	if !ok {
   100  		general.Errorf("failed to get metrics-emitter")
   101  		return nil
   102  	}
   103  
   104  	briefWorkName, ok := parseContextMetricName(ctx)
   105  	if ok {
   106  		tags = append(tags, metrics.MetricTag{
   107  			Key: "briefWorkName",
   108  			Val: briefWorkName,
   109  		})
   110  	}
   111  
   112  	return emitter.StoreInt64(metricName, metricValue, metrics.MetricTypeNameRaw, tags...)
   113  }