@@ -82,7 +82,13 @@ func (in initializer) Init(metrics interface{}, namespace string) error {
82
82
return fmt .Errorf ("expected pointer to metrics struct, got %q" , metricsPtr .Kind ())
83
83
}
84
84
85
- return in .initMetrics (metricsPtr .Elem (), namespace )
85
+ var namespaces []string
86
+
87
+ if namespace != "" {
88
+ namespaces = append (namespaces , namespace )
89
+ }
90
+
91
+ return in .initMetrics (metricsPtr .Elem (), namespaces ... )
86
92
}
87
93
88
94
func (in initializer ) initMetrics (group reflect.Value , namespaces ... string ) error {
@@ -100,8 +106,12 @@ func (in initializer) initMetrics(group reflect.Value, namespaces ...string) err
100
106
}
101
107
} else if fieldType .Type .Kind () == reflect .Struct {
102
108
namespace , ok := fieldType .Tag .Lookup ("namespace" )
103
- if ! ok {
104
- return fmt .Errorf ("field %s does not have the namespace tag defined" , fieldType .Name )
109
+ if ! ok || namespace == "" {
110
+ if err := in .initMetrics (field , namespaces ... ); err != nil {
111
+ return err
112
+ }
113
+
114
+ continue
105
115
}
106
116
if err := in .initMetrics (field , append (namespaces , namespace )... ); err != nil {
107
117
return err
@@ -134,7 +144,7 @@ func (in initializer) initMetricFunc(field reflect.Value, structField reflect.St
134
144
// Validate the input of the metric function, it should have zero or one arguments
135
145
// If it has one argument, it should be a struct correctly tagged with label names
136
146
// If there are no input arguments, this metric will not have labels registered
137
- var labelIndexes = make (map [label ][]int )
147
+ labelIndexes : = make (map [label ][]int )
138
148
if fieldType .NumIn () > 1 {
139
149
return fmt .Errorf ("field %s: expected 1 in arg, got %d" , structField .Name , fieldType .NumIn ())
140
150
} else if fieldType .NumIn () == 1 {
0 commit comments