Skip to content

Commit

Permalink
[FABG-813] Ch Client Metrics-Switch to Fabric impl
Browse files Browse the repository at this point in the history
	New MetricsConfig support has been added to load metrics config into
	the SDK and create a ClientMetrics instance for capturing metrics.

	Also fixed gometalinter tool as v2 tag is not available anymore.
	dependencies.sh has been updated with a more recent tag: v2.0.12

Change-Id: I3bb1809e0b7a6a18194cdca622e7834ebb80543a
Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
  • Loading branch information
Baha Shaaban committed Feb 5, 2019
1 parent ce2814e commit 7df511b
Show file tree
Hide file tree
Showing 54 changed files with 2,773 additions and 600 deletions.
86 changes: 47 additions & 39 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
name = "github.com/stretchr/testify"
version = "1.2.0"

# tally is used only if performance is enabled
# using the pprof build tag (normal sdk build does not use this library)
[[constraint]]
name = "github.com/uber-go/tally"
version = "=3.3.2"
name = "github.com/hyperledger/fabric-lib-go"
version = "1.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/

package disabled

import (
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metrics"
)

type Provider struct{}

func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter { return &Counter{} }
func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge { return &Gauge{} }
func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram { return &Histogram{} }

type Counter struct{}

func (c *Counter) Add(delta float64) {}
func (c *Counter) With(labelValues ...string) metrics.Counter {
return c
}

type Gauge struct{}

func (g *Gauge) Add(delta float64) {}
func (g *Gauge) Set(delta float64) {}
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
return g
}

type Histogram struct{}

func (h *Histogram) Observe(value float64) {}
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
return h
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/

package namer

import (
"fmt"
"regexp"
"strings"

"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metrics"
)

type Namer struct {
namespace string
subsystem string
name string
nameFormat string
labelNames map[string]struct{}
}

func NewCounterNamer(c metrics.CounterOpts) *Namer {
return &Namer{
namespace: c.Namespace,
subsystem: c.Subsystem,
name: c.Name,
nameFormat: c.StatsdFormat,
labelNames: sliceToSet(c.LabelNames),
}
}

func NewGaugeNamer(g metrics.GaugeOpts) *Namer {
return &Namer{
namespace: g.Namespace,
subsystem: g.Subsystem,
name: g.Name,
nameFormat: g.StatsdFormat,
labelNames: sliceToSet(g.LabelNames),
}
}

func NewHistogramNamer(h metrics.HistogramOpts) *Namer {
return &Namer{
namespace: h.Namespace,
subsystem: h.Subsystem,
name: h.Name,
nameFormat: h.StatsdFormat,
labelNames: sliceToSet(h.LabelNames),
}
}

func (n *Namer) validateKey(name string) {
if _, ok := n.labelNames[name]; !ok {
panic("invalid label name: " + name)
}
}

func (n *Namer) FullyQualifiedName() string {
switch {
case n.namespace != "" && n.subsystem != "":
return strings.Join([]string{n.namespace, n.subsystem, n.name}, ".")
case n.namespace != "":
return strings.Join([]string{n.namespace, n.name}, ".")
case n.subsystem != "":
return strings.Join([]string{n.subsystem, n.name}, ".")
default:
return n.name
}
}

func (n *Namer) labelsToMap(labelValues []string) map[string]string {
labels := map[string]string{}
for i := 0; i < len(labelValues); i += 2 {
key := labelValues[i]
n.validateKey(key)
if i == len(labelValues)-1 {
labels[key] = "unknown"
} else {
labels[key] = labelValues[i+1]
}
}
return labels
}

var formatRegexp = regexp.MustCompile(`%{([#?[:alnum:]_]+)}`)
var invalidLabelValueRegexp = regexp.MustCompile(`[.|:\s]`)

func (n *Namer) Format(labelValues ...string) string {
labels := n.labelsToMap(labelValues)

cursor := 0
var segments []string
// iterate over the regex groups and convert to formatters
matches := formatRegexp.FindAllStringSubmatchIndex(n.nameFormat, -1)
for _, m := range matches {
start, end := m[0], m[1]
labelStart, labelEnd := m[2], m[3]

if start > cursor {
segments = append(segments, n.nameFormat[cursor:start])
}

key := n.nameFormat[labelStart:labelEnd]
var value string
switch key {
case "#namespace":
value = n.namespace
case "#subsystem":
value = n.subsystem
case "#name":
value = n.name
case "#fqname":
value = n.FullyQualifiedName()
default:
var ok bool
value, ok = labels[key]
if !ok {
panic(fmt.Sprintf("invalid label in name format: %s", key))
}
value = invalidLabelValueRegexp.ReplaceAllString(value, "_")
}
segments = append(segments, value)

cursor = end
}

// handle any trailing suffix
if cursor != len(n.nameFormat) {
segments = append(segments, n.nameFormat[cursor:])
}

return strings.Join(segments, "")
}

func sliceToSet(set []string) map[string]struct{} {
labelSet := map[string]struct{}{}
for _, s := range set {
labelSet[s] = struct{}{}
}
return labelSet
}
Loading

0 comments on commit 7df511b

Please sign in to comment.