Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 7df511b

Browse files
author
Baha Shaaban
committed
[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>
1 parent ce2814e commit 7df511b

File tree

54 files changed

+2773
-600
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2773
-600
lines changed

Gopkg.lock

Lines changed: 47 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
name = "github.com/stretchr/testify"
5353
version = "1.2.0"
5454

55-
# tally is used only if performance is enabled
56-
# using the pprof build tag (normal sdk build does not use this library)
5755
[[constraint]]
58-
name = "github.com/uber-go/tally"
59-
version = "=3.3.2"
56+
name = "github.com/hyperledger/fabric-lib-go"
57+
version = "1.0.0"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/*
7+
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
8+
Please review third_party pinning scripts and patches for more details.
9+
*/
10+
11+
package disabled
12+
13+
import (
14+
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metrics"
15+
)
16+
17+
type Provider struct{}
18+
19+
func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter { return &Counter{} }
20+
func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge { return &Gauge{} }
21+
func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram { return &Histogram{} }
22+
23+
type Counter struct{}
24+
25+
func (c *Counter) Add(delta float64) {}
26+
func (c *Counter) With(labelValues ...string) metrics.Counter {
27+
return c
28+
}
29+
30+
type Gauge struct{}
31+
32+
func (g *Gauge) Add(delta float64) {}
33+
func (g *Gauge) Set(delta float64) {}
34+
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
35+
return g
36+
}
37+
38+
type Histogram struct{}
39+
40+
func (h *Histogram) Observe(value float64) {}
41+
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
42+
return h
43+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/*
7+
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
8+
Please review third_party pinning scripts and patches for more details.
9+
*/
10+
11+
package namer
12+
13+
import (
14+
"fmt"
15+
"regexp"
16+
"strings"
17+
18+
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metrics"
19+
)
20+
21+
type Namer struct {
22+
namespace string
23+
subsystem string
24+
name string
25+
nameFormat string
26+
labelNames map[string]struct{}
27+
}
28+
29+
func NewCounterNamer(c metrics.CounterOpts) *Namer {
30+
return &Namer{
31+
namespace: c.Namespace,
32+
subsystem: c.Subsystem,
33+
name: c.Name,
34+
nameFormat: c.StatsdFormat,
35+
labelNames: sliceToSet(c.LabelNames),
36+
}
37+
}
38+
39+
func NewGaugeNamer(g metrics.GaugeOpts) *Namer {
40+
return &Namer{
41+
namespace: g.Namespace,
42+
subsystem: g.Subsystem,
43+
name: g.Name,
44+
nameFormat: g.StatsdFormat,
45+
labelNames: sliceToSet(g.LabelNames),
46+
}
47+
}
48+
49+
func NewHistogramNamer(h metrics.HistogramOpts) *Namer {
50+
return &Namer{
51+
namespace: h.Namespace,
52+
subsystem: h.Subsystem,
53+
name: h.Name,
54+
nameFormat: h.StatsdFormat,
55+
labelNames: sliceToSet(h.LabelNames),
56+
}
57+
}
58+
59+
func (n *Namer) validateKey(name string) {
60+
if _, ok := n.labelNames[name]; !ok {
61+
panic("invalid label name: " + name)
62+
}
63+
}
64+
65+
func (n *Namer) FullyQualifiedName() string {
66+
switch {
67+
case n.namespace != "" && n.subsystem != "":
68+
return strings.Join([]string{n.namespace, n.subsystem, n.name}, ".")
69+
case n.namespace != "":
70+
return strings.Join([]string{n.namespace, n.name}, ".")
71+
case n.subsystem != "":
72+
return strings.Join([]string{n.subsystem, n.name}, ".")
73+
default:
74+
return n.name
75+
}
76+
}
77+
78+
func (n *Namer) labelsToMap(labelValues []string) map[string]string {
79+
labels := map[string]string{}
80+
for i := 0; i < len(labelValues); i += 2 {
81+
key := labelValues[i]
82+
n.validateKey(key)
83+
if i == len(labelValues)-1 {
84+
labels[key] = "unknown"
85+
} else {
86+
labels[key] = labelValues[i+1]
87+
}
88+
}
89+
return labels
90+
}
91+
92+
var formatRegexp = regexp.MustCompile(`%{([#?[:alnum:]_]+)}`)
93+
var invalidLabelValueRegexp = regexp.MustCompile(`[.|:\s]`)
94+
95+
func (n *Namer) Format(labelValues ...string) string {
96+
labels := n.labelsToMap(labelValues)
97+
98+
cursor := 0
99+
var segments []string
100+
// iterate over the regex groups and convert to formatters
101+
matches := formatRegexp.FindAllStringSubmatchIndex(n.nameFormat, -1)
102+
for _, m := range matches {
103+
start, end := m[0], m[1]
104+
labelStart, labelEnd := m[2], m[3]
105+
106+
if start > cursor {
107+
segments = append(segments, n.nameFormat[cursor:start])
108+
}
109+
110+
key := n.nameFormat[labelStart:labelEnd]
111+
var value string
112+
switch key {
113+
case "#namespace":
114+
value = n.namespace
115+
case "#subsystem":
116+
value = n.subsystem
117+
case "#name":
118+
value = n.name
119+
case "#fqname":
120+
value = n.FullyQualifiedName()
121+
default:
122+
var ok bool
123+
value, ok = labels[key]
124+
if !ok {
125+
panic(fmt.Sprintf("invalid label in name format: %s", key))
126+
}
127+
value = invalidLabelValueRegexp.ReplaceAllString(value, "_")
128+
}
129+
segments = append(segments, value)
130+
131+
cursor = end
132+
}
133+
134+
// handle any trailing suffix
135+
if cursor != len(n.nameFormat) {
136+
segments = append(segments, n.nameFormat[cursor:])
137+
}
138+
139+
return strings.Join(segments, "")
140+
}
141+
142+
func sliceToSet(set []string) map[string]struct{} {
143+
labelSet := map[string]struct{}{}
144+
for _, s := range set {
145+
labelSet[s] = struct{}{}
146+
}
147+
return labelSet
148+
}

0 commit comments

Comments
 (0)