-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
instance.go
757 lines (669 loc) · 24 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package pf
import (
"context"
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/ptypes/empty"
"github.com/apache/pulsar-client-go/pulsar"
log "github.com/apache/pulsar/pulsar-function-go/logutil"
pb "github.com/apache/pulsar/pulsar-function-go/pb"
prometheus_client "github.com/prometheus/client_model/go"
)
type goInstance struct {
function function
context *FunctionContext
producer pulsar.Producer
consumers map[string]pulsar.Consumer
client pulsar.Client
lastHealthCheckTS int64
properties map[string]string
stats StatWithLabelValues
}
func (gi *goInstance) getMetricsLabels() []string {
// e.g. metrics_labels = []string{"test-tenant","test-tenant/test-namespace", "test-name", "1234", "test-cluster",
// "test-tenant/test-namespace/test-name"}
metricsLabels := []string{
gi.context.GetFuncTenant(),
gi.context.GetTenantAndNamespace(),
gi.context.GetFuncName(),
gi.context.GetFuncID(),
gi.context.GetClusterName(),
gi.context.GetTenantAndNamespaceAndName(),
}
return metricsLabels
}
// newGoInstance init goInstance and init function context
func newGoInstance() *goInstance {
goInstance := &goInstance{
context: NewFuncContext(),
consumers: make(map[string]pulsar.Consumer),
}
now := time.Now()
goInstance.context.outputMessage = func(topic string) pulsar.Producer {
producer, err := goInstance.getProducer(topic)
if err != nil {
log.Fatal(err)
}
return producer
}
goInstance.lastHealthCheckTS = now.UnixNano()
goInstance.properties = make(map[string]string)
goInstance.stats = NewStatWithLabelValues(goInstance.getMetricsLabels()...)
return goInstance
}
func (gi *goInstance) processSpawnerHealthCheckTimer(tkr *time.Ticker) {
log.Info("Starting processSpawnerHealthCheckTimer")
now := time.Now()
maxIdleTime := gi.context.GetMaxIdleTime()
timeSinceLastCheck := now.UnixNano() - gi.lastHealthCheckTS
if (timeSinceLastCheck) > (maxIdleTime) {
log.Error("Haven't received health check from spawner in a while. Stopping instance...")
gi.close()
tkr.Stop()
}
}
func (gi *goInstance) startScheduler() {
if gi.context.instanceConf.expectedHealthCheckInterval > 0 {
log.Info("Starting Scheduler")
go func() {
log.Info("Started Scheduler")
tkr := time.NewTicker(time.Millisecond * 1000 * gi.context.GetExpectedHealthCheckIntervalAsDuration())
for range tkr.C {
log.Info("Starting Timer")
go gi.processSpawnerHealthCheckTimer(tkr)
}
}()
}
}
func (gi *goInstance) startFunction(function function) error {
gi.function = function
// start process spawner health check timer
now := time.Now()
gi.lastHealthCheckTS = now.UnixNano()
gi.startScheduler()
err := gi.setupClient()
if err != nil {
log.Errorf("setup client failed, error is:%v", err)
return err
}
err = gi.setupProducer()
if err != nil {
log.Errorf("setup producer failed, error is:%v", err)
return err
}
channel, err := gi.setupConsumer()
if err != nil {
log.Errorf("setup consumer failed, error is:%v", err)
return err
}
err = gi.setupLogHandler()
if err != nil {
log.Errorf("setup log appender failed, error is:%v", err)
return err
}
idleDuration := getIdleTimeout(time.Millisecond * gi.context.instanceConf.killAfterIdle)
idleTimer := time.NewTimer(idleDuration)
defer idleTimer.Stop()
servicer := InstanceControlServicer{goInstance: gi}
servicer.serve(gi)
metricsServicer := NewMetricsServicer(gi)
metricsServicer.serve()
defer metricsServicer.close()
CLOSE:
for {
select {
case cm := <-channel:
msgInput := cm.Message
atMostOnce := gi.context.instanceConf.funcDetails.ProcessingGuarantees == pb.ProcessingGuarantees_ATMOST_ONCE
atLeastOnce := gi.context.instanceConf.funcDetails.ProcessingGuarantees == pb.ProcessingGuarantees_ATLEAST_ONCE
autoAck := gi.context.instanceConf.funcDetails.AutoAck
if autoAck && atMostOnce {
gi.ackInputMessage(msgInput)
}
gi.stats.incrTotalReceived()
gi.addLogTopicHandler()
gi.stats.setLastInvocation()
gi.stats.processTimeStart()
output, err := gi.handlerMsg(msgInput)
if err != nil {
log.Errorf("handler message error:%v", err)
if autoAck && atLeastOnce {
gi.nackInputMessage(msgInput)
}
gi.stats.incrTotalUserExceptions(err)
return err
}
gi.stats.processTimeEnd()
gi.processResult(msgInput, output)
case <-idleTimer.C:
close(channel)
break CLOSE
}
// reset the idle timer and drain if appropriate before the next loop
if !idleTimer.Stop() {
<-idleTimer.C
}
idleTimer.Reset(idleDuration)
}
gi.closeLogTopic()
gi.close()
return nil
}
const (
authPluginToken = "org.apache.pulsar.client.impl.auth.AuthenticationToken"
authPluginNone = ""
)
func (gi *goInstance) setupClient() error {
ic := gi.context.instanceConf
clientOpts := pulsar.ClientOptions{
URL: ic.pulsarServiceURL,
TLSTrustCertsFilePath: ic.tlsTrustCertsPath,
TLSAllowInsecureConnection: ic.tlsAllowInsecure,
TLSValidateHostname: ic.tlsHostnameVerification,
}
switch ic.authPlugin {
case authPluginToken:
switch {
case strings.HasPrefix(ic.authParams, "file://"):
clientOpts.Authentication = pulsar.NewAuthenticationTokenFromFile(ic.authParams[7:])
case strings.HasPrefix(ic.authParams, "token:"):
clientOpts.Authentication = pulsar.NewAuthenticationToken(ic.authParams[6:])
case ic.authParams == "":
return fmt.Errorf("auth plugin %s given, but authParams is empty", authPluginToken)
default:
return fmt.Errorf(`unknown token format - expecting "file://" or "token:" prefix`)
}
case authPluginNone:
clientOpts.Authentication, _ = pulsar.NewAuthentication("", "") // ret: auth.NewAuthDisabled()
default:
return fmt.Errorf("unknown auth provider: %s", ic.authPlugin)
}
client, err := pulsar.NewClient(clientOpts)
if err != nil {
log.Errorf("create client error:%v", err)
gi.stats.incrTotalSysExceptions(err)
return err
}
gi.client = client
return nil
}
func (gi *goInstance) setupProducer() error {
if gi.context.instanceConf.funcDetails.Sink.Topic != "" && len(gi.context.instanceConf.funcDetails.Sink.Topic) > 0 {
log.Debugf("Setting up producer for topic %s", gi.context.instanceConf.funcDetails.Sink.Topic)
producer, err := gi.getProducer(gi.context.instanceConf.funcDetails.Sink.Topic)
if err != nil {
log.Fatal(err)
}
gi.producer = producer
return nil
}
return nil
}
func (gi *goInstance) getProducer(topicName string) (pulsar.Producer, error) {
properties := getProperties(getDefaultSubscriptionName(
gi.context.instanceConf.funcDetails.Tenant,
gi.context.instanceConf.funcDetails.Namespace,
gi.context.instanceConf.funcDetails.Name), gi.context.instanceConf.instanceID)
batchBuilderType := pulsar.DefaultBatchBuilder
compressionType := pulsar.LZ4
if gi.context.instanceConf.funcDetails.Sink.ProducerSpec != nil {
switch gi.context.instanceConf.funcDetails.Sink.ProducerSpec.CompressionType {
case pb.CompressionType_NONE:
compressionType = pulsar.NoCompression
case pb.CompressionType_ZLIB:
compressionType = pulsar.ZLib
case pb.CompressionType_ZSTD:
compressionType = pulsar.ZSTD
default:
compressionType = pulsar.LZ4 // go doesn't support SNAPPY yet
}
batchBuilder := gi.context.instanceConf.funcDetails.Sink.ProducerSpec.BatchBuilder
if batchBuilder != "" {
if batchBuilder == "KEY_BASED" {
batchBuilderType = pulsar.KeyBasedBatchBuilder
}
}
}
producer, err := gi.client.CreateProducer(pulsar.ProducerOptions{
Topic: topicName,
Properties: properties,
CompressionType: compressionType,
BatchingMaxPublishDelay: time.Millisecond * 10,
BatcherBuilderType: batchBuilderType,
SendTimeout: 0,
// Set send timeout to be infinity to prevent potential deadlock with consumer
// that might happen when consumer is blocked due to unacked messages
})
if err != nil {
gi.stats.incrTotalSysExceptions(err)
log.Errorf("create producer error:%s", err.Error())
return nil, err
}
return producer, err
}
func (gi *goInstance) setupConsumer() (chan pulsar.ConsumerMessage, error) {
subscriptionType := pulsar.Shared
if int32(gi.context.instanceConf.funcDetails.Source.SubscriptionType) == pb.SubscriptionType_value["FAILOVER"] {
subscriptionType = pulsar.Failover
}
funcDetails := gi.context.instanceConf.funcDetails
subscriptionName := funcDetails.Tenant + "/" + funcDetails.Namespace + "/" + funcDetails.Name
if funcDetails.Source != nil && funcDetails.Source.SubscriptionName != "" {
subscriptionName = funcDetails.Source.SubscriptionName
}
properties := getProperties(getDefaultSubscriptionName(
funcDetails.Tenant,
funcDetails.Namespace,
funcDetails.Name), gi.context.instanceConf.instanceID)
channel := make(chan pulsar.ConsumerMessage)
var (
consumer pulsar.Consumer
topicName *TopicName
err error
)
for topic, consumerConf := range funcDetails.Source.InputSpecs {
topicName, err = ParseTopicName(topic)
if err != nil {
return nil, err
}
log.Debugf("Setting up consumer for topic: %s with subscription name: %s", topicName.Name, subscriptionName)
if consumerConf.ReceiverQueueSize != nil {
if consumerConf.IsRegexPattern {
consumer, err = gi.client.Subscribe(pulsar.ConsumerOptions{
TopicsPattern: topicName.Name,
ReceiverQueueSize: int(consumerConf.ReceiverQueueSize.Value),
SubscriptionName: subscriptionName,
Properties: properties,
Type: subscriptionType,
MessageChannel: channel,
})
} else {
consumer, err = gi.client.Subscribe(pulsar.ConsumerOptions{
Topic: topicName.Name,
SubscriptionName: subscriptionName,
Properties: properties,
Type: subscriptionType,
ReceiverQueueSize: int(consumerConf.ReceiverQueueSize.Value),
MessageChannel: channel,
})
}
} else {
if consumerConf.IsRegexPattern {
consumer, err = gi.client.Subscribe(pulsar.ConsumerOptions{
TopicsPattern: topicName.Name,
SubscriptionName: subscriptionName,
Properties: properties,
Type: subscriptionType,
MessageChannel: channel,
})
} else {
consumer, err = gi.client.Subscribe(pulsar.ConsumerOptions{
Topic: topicName.Name,
SubscriptionName: subscriptionName,
Properties: properties,
Type: subscriptionType,
MessageChannel: channel,
})
}
}
if err != nil {
log.Errorf("create consumer error:%s", err.Error())
gi.stats.incrTotalSysExceptions(err)
return nil, err
}
gi.consumers[topicName.Name] = consumer
}
return channel, nil
}
func (gi *goInstance) handlerMsg(input pulsar.Message) (output []byte, err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gi.context.SetCurrentRecord(input)
ctx = NewContext(ctx, gi.context)
msgInput := input.Payload()
return gi.function.process(ctx, msgInput)
}
func (gi *goInstance) processResult(msgInput pulsar.Message, output []byte) {
atLeastOnce := gi.context.instanceConf.funcDetails.ProcessingGuarantees == pb.ProcessingGuarantees_ATLEAST_ONCE
autoAck := gi.context.instanceConf.funcDetails.AutoAck
// If the function had an output and the user has specified an output topic, the output needs to be sent to the
// assigned output topic.
if output != nil && gi.context.instanceConf.funcDetails.Sink.Topic != "" {
asyncMsg := pulsar.ProducerMessage{
Payload: output,
}
// Dispatch an async send for the message with callback in case of error.
gi.producer.SendAsync(context.Background(), &asyncMsg,
func(_ pulsar.MessageID, _ *pulsar.ProducerMessage, err error) {
// Callback after message async send:
// If there was an error, the SDK is entrusted with responding, and we have at-least-once delivery
// semantics, ensure we nack so someone else can get it, in case we are the only handler. Then mark
// exception and fail out.
if err != nil {
if autoAck && atLeastOnce {
gi.nackInputMessage(msgInput)
}
gi.stats.incrTotalSysExceptions(err)
log.Fatal(err)
}
// Otherwise the message succeeded. If the SDK is entrusted with responding and we are using
// atLeastOnce delivery semantics, ack the message.
if autoAck && atLeastOnce {
gi.ackInputMessage(msgInput)
}
gi.stats.incrTotalProcessedSuccessfully()
},
)
return
}
// No output from the function or no output topic. Ack if we need to and mark the success before rturning.
if autoAck && atLeastOnce {
gi.ackInputMessage(msgInput)
}
gi.stats.incrTotalProcessedSuccessfully()
}
// ackInputMessage doesn't produce any result, or the user doesn't want the result.
func (gi *goInstance) ackInputMessage(inputMessage pulsar.Message) {
log.Debugf("ack input message topic name is: %s", inputMessage.Topic())
gi.respondMessage(inputMessage, true)
}
func (gi *goInstance) nackInputMessage(inputMessage pulsar.Message) {
gi.respondMessage(inputMessage, false)
}
func (gi *goInstance) respondMessage(inputMessage pulsar.Message, ack bool) {
topicName, err := ParseTopicName(inputMessage.Topic())
if err != nil {
log.Errorf("unable respond to message ID %s - invalid topic: %v", messageIDStr(inputMessage), err)
return
}
// consumers are indexed by topic name only (no partition)
if ack {
gi.consumers[topicName.NameWithoutPartition()].Ack(inputMessage)
return
}
gi.consumers[topicName.NameWithoutPartition()].Nack(inputMessage)
}
func getIdleTimeout(timeoutMilliSecond time.Duration) time.Duration {
if timeoutMilliSecond <= 0 {
return time.Duration(math.MaxInt64)
}
return timeoutMilliSecond
}
func (gi *goInstance) setupLogHandler() error {
if gi.context.instanceConf.funcDetails.GetLogTopic() != "" {
gi.context.logAppender = NewLogAppender(
gi.client, //pulsar client
gi.context.instanceConf.funcDetails.GetLogTopic(), //log topic
getDefaultSubscriptionName(gi.context.instanceConf.funcDetails.Tenant, //fqn
gi.context.instanceConf.funcDetails.Namespace,
gi.context.instanceConf.funcDetails.Name),
)
return gi.context.logAppender.Start()
}
return nil
}
func (gi *goInstance) addLogTopicHandler() {
// Clear StrEntry regardless gi.context.logAppender is set or not
defer func() {
log.StrEntry = nil
}()
if gi.context.logAppender == nil {
return
}
for _, logByte := range log.StrEntry {
gi.context.logAppender.Append([]byte(logByte))
}
}
func (gi *goInstance) closeLogTopic() {
log.Info("closing log topic...")
if gi.context.logAppender == nil {
return
}
gi.context.logAppender.Stop()
gi.context.logAppender = nil
}
func (gi *goInstance) close() {
log.Info("closing go instance...")
if gi.producer != nil {
gi.producer.Close()
}
if gi.consumers != nil {
for _, consumer := range gi.consumers {
consumer.Close()
}
}
if gi.client != nil {
gi.client.Close()
}
}
func (gi *goInstance) healthCheck() *pb.HealthCheckResult {
now := time.Now()
gi.lastHealthCheckTS = now.UnixNano()
healthCheckResult := pb.HealthCheckResult{Success: true}
return &healthCheckResult
}
func (gi *goInstance) getFunctionStatus() *pb.FunctionStatus {
status := pb.FunctionStatus{}
status.Running = true
totalReceived := gi.getTotalReceived()
totalProcessedSuccessfully := gi.getTotalProcessedSuccessfully()
totalUserExceptions := gi.getTotalUserExceptions()
totalSysExceptions := gi.getTotalSysExceptions()
avgProcessLatencyMs := gi.getAvgProcessLatency()
lastInvocation := gi.getLastInvocation()
status.NumReceived = int64(totalReceived)
status.NumSuccessfullyProcessed = int64(totalProcessedSuccessfully)
status.NumUserExceptions = int64(totalUserExceptions)
status.InstanceId = strconv.Itoa(gi.context.instanceConf.instanceID)
status.NumUserExceptions = int64(totalUserExceptions)
for _, exPair := range gi.stats.latestUserException {
toAdd := pb.FunctionStatus_ExceptionInformation{}
toAdd.ExceptionString = exPair.exception.Error()
toAdd.MsSinceEpoch = exPair.timestamp
status.LatestUserExceptions = append(status.LatestUserExceptions, &toAdd)
}
status.NumSystemExceptions = int64(totalSysExceptions)
for _, exPair := range gi.stats.latestSysException {
toAdd := pb.FunctionStatus_ExceptionInformation{}
toAdd.ExceptionString = exPair.exception.Error()
toAdd.MsSinceEpoch = exPair.timestamp
status.LatestSystemExceptions = append(status.LatestSystemExceptions, &toAdd)
}
status.AverageLatency = float64(avgProcessLatencyMs)
status.LastInvocationTime = int64(lastInvocation)
return &status
}
func (gi *goInstance) getMetrics() *pb.MetricsData {
totalReceived := gi.getTotalReceived()
totalProcessedSuccessfully := gi.getTotalProcessedSuccessfully()
totalUserExceptions := gi.getTotalUserExceptions()
totalSysExceptions := gi.getTotalSysExceptions()
avgProcessLatencyMs := gi.getAvgProcessLatency()
lastInvocation := gi.getLastInvocation()
totalReceived1min := gi.getTotalReceived1min()
totalProcessedSuccessfully1min := gi.getTotalProcessedSuccessfully1min()
totalUserExceptions1min := gi.getTotalUserExceptions1min()
totalSysExceptions1min := gi.getTotalSysExceptions1min()
//avg_process_latency_ms_1min := gi.get_avg_process_latency_1min()
userMetricsMap := gi.getUserMetricsMap()
metricsData := pb.MetricsData{}
// total metrics
metricsData.ReceivedTotal = int64(totalReceived)
metricsData.ProcessedSuccessfullyTotal = int64(totalProcessedSuccessfully)
metricsData.SystemExceptionsTotal = int64(totalSysExceptions)
metricsData.UserExceptionsTotal = int64(totalUserExceptions)
metricsData.AvgProcessLatency = float64(avgProcessLatencyMs)
metricsData.LastInvocation = int64(lastInvocation)
// 1min metrics
metricsData.ReceivedTotal_1Min = int64(totalReceived1min)
metricsData.ProcessedSuccessfullyTotal_1Min = int64(totalProcessedSuccessfully1min)
metricsData.SystemExceptionsTotal_1Min = int64(totalSysExceptions1min)
metricsData.UserExceptionsTotal_1Min = int64(totalUserExceptions1min)
// user metrics
metricsData.UserMetrics = userMetricsMap
return &metricsData
}
func (gi *goInstance) getAndResetMetrics() *pb.MetricsData {
metricsData := gi.getMetrics()
gi.resetMetrics()
return metricsData
}
func (gi *goInstance) resetMetrics() *empty.Empty {
gi.stats.reset()
return &empty.Empty{}
}
// This method is used to get the required metrics for Prometheus.
// Note that this doesn't distinguish between parallel function instances!
func (gi *goInstance) getMatchingMetricFunc() func(lbl *prometheus_client.LabelPair) bool {
matchMetricFunc := func(lbl *prometheus_client.LabelPair) bool {
return *lbl.Name == "fqfn" && *lbl.Value == gi.context.GetTenantAndNamespaceAndName()
}
return matchMetricFunc
}
func (gi *goInstance) getMatchingMetricFromRegistry(metricName string) prometheus_client.Metric {
filteredMetricFamilies := gi.getFilteredMetricFamilies(metricName)
if len(filteredMetricFamilies) == 0 {
return prometheus_client.Metric{}
}
metricFunc := gi.getMatchingMetricFunc()
matchingMetric := getFirstMatch(filteredMetricFamilies[0].Metric, metricFunc)
return *matchingMetric
}
func (gi *goInstance) getFilteredMetricFamilies(metricName string) []*prometheus_client.MetricFamily {
metricFamilies, err := reg.Gather()
if err != nil {
log.Errorf("Something went wrong when calling reg.Gather(), the metricName is: %s", metricName)
}
matchFamilyFunc := func(vect *prometheus_client.MetricFamily) bool {
return *vect.Name == metricName
}
filteredMetricFamilies := filter(metricFamilies, matchFamilyFunc)
if len(filteredMetricFamilies) > 1 {
// handle this.
log.Errorf("Too many metric families for metricName: %s " + metricName)
}
return filteredMetricFamilies
}
func (gi *goInstance) getTotalReceived() float32 {
// "pulsar_function_" + "received_total", NewGaugeVec.
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalReceived)
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalProcessedSuccessfully() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalSuccessfullyProcessed)
// "pulsar_function_" + "processed_successfully_total", NewGaugeVec.
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalSysExceptions() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalSystemExceptions)
// "pulsar_function_"+ "system_exceptions_total", NewGaugeVec.
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalUserExceptions() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalUserExceptions)
// "pulsar_function_" + "user_exceptions_total", NewGaugeVec
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getAvgProcessLatency() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + ProcessLatencyMs)
// "pulsar_function_" + "process_latency_ms", SummaryVec.
count := metric.GetSummary().SampleCount
sum := metric.GetSummary().SampleSum
if *count <= 0.0 {
return 0.0
}
return float32(*sum) / float32(*count)
}
func (gi *goInstance) getLastInvocation() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + LastInvocation)
// "pulsar_function_" + "last_invocation", GaugeVec.
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalProcessedSuccessfully1min() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalSuccessfullyProcessed1min)
// "pulsar_function_" + "processed_successfully_total_1min", GaugeVec.
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalSysExceptions1min() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalSystemExceptions1min)
// "pulsar_function_" + "system_exceptions_total_1min", GaugeVec
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalUserExceptions1min() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalUserExceptions1min)
// "pulsar_function_" + "user_exceptions_total_1min", GaugeVec
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getTotalReceived1min() float32 {
metric := gi.getMatchingMetricFromRegistry(PulsarFunctionMetricsPrefix + TotalReceived1min)
// "pulsar_function_" + "received_total_1min", GaugeVec
val := metric.GetGauge().Value
return float32(*val)
}
func (gi *goInstance) getUserMetricsMap() map[string]float64 {
userMetricMap := map[string]float64{}
filteredMetricFamilies := gi.getFilteredMetricFamilies(PulsarFunctionMetricsPrefix + UserMetric)
if len(filteredMetricFamilies) == 0 {
return userMetricMap
}
for _, m := range filteredMetricFamilies[0].GetMetric() {
var isFuncMetric bool
var userLabelName string
VERIFY_USER_METRIC:
for _, l := range m.GetLabel() {
switch l.GetName() {
case "fqfn":
if l.GetValue() == gi.context.GetTenantAndNamespaceAndName() {
isFuncMetric = true
if userLabelName != "" {
break VERIFY_USER_METRIC
}
}
case "metric":
userLabelName = l.GetValue()
if isFuncMetric {
break VERIFY_USER_METRIC
}
}
}
if isFuncMetric && userLabelName != "" {
summary := m.GetSummary()
count := summary.GetSampleCount()
if count <= 0 {
userMetricMap[userLabelName] = 0
} else {
userMetricMap[userLabelName] = summary.GetSampleSum() / float64(count)
}
}
}
return userMetricMap
}