-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FABG-813] Ch Client Metrics-Switch to Fabric impl
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
Showing
54 changed files
with
2,773 additions
and
600 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
internal/github.com/hyperledger/fabric/common/metrics/disabled/provider.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
148 changes: 148 additions & 0 deletions
148
internal/github.com/hyperledger/fabric/common/metrics/internal/namer/namer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.