|
4 | 4 | package request
|
5 | 5 |
|
6 | 6 | import (
|
7 |
| - "strconv" |
8 |
| - |
9 | 7 | "go.opentelemetry.io/otel/attribute"
|
10 | 8 | semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
|
11 | 9 |
|
12 | 10 | "go.opentelemetry.io/obi/pkg/export/attributes"
|
13 | 11 | attr "go.opentelemetry.io/obi/pkg/export/attributes/names"
|
14 | 12 | )
|
15 | 13 |
|
16 |
| -// SpanOTELGetters returns the attributes.Getter function that returns the |
| 14 | +// spanOTELGetters returns the attributes.Getter function that returns the |
17 | 15 | // OTEL attribute.KeyValue of a given attribute name.
|
18 | 16 | //
|
19 | 17 | //nolint:cyclop
|
20 |
| -func SpanOTELGetters(name attr.Name) (attributes.Getter[*Span, attribute.KeyValue], bool) { |
| 18 | +func spanOTELGetters(name attr.Name) (attributes.Getter[*Span, attribute.KeyValue], bool) { |
21 | 19 | var getter attributes.Getter[*Span, attribute.KeyValue]
|
22 | 20 | switch name {
|
23 | 21 | case attr.Client:
|
@@ -122,101 +120,15 @@ func SpanOTELGetters(name attr.Name) (attributes.Getter[*Span, attribute.KeyValu
|
122 | 120 | return getter, getter != nil
|
123 | 121 | }
|
124 | 122 |
|
125 |
| -// SpanPromGetters returns the attributes.Getter function that returns the |
| 123 | +// spanPromGetters returns the attributes.Getter function that returns the |
126 | 124 | // Prometheus string value of a given attribute name.
|
127 | 125 | //
|
128 | 126 | //nolint:cyclop
|
129 |
| -func SpanPromGetters(attrName attr.Name) (attributes.Getter[*Span, string], bool) { |
130 |
| - var getter attributes.Getter[*Span, string] |
131 |
| - switch attrName { |
132 |
| - case attr.HTTPRequestMethod: |
133 |
| - getter = func(s *Span) string { return s.Method } |
134 |
| - case attr.HTTPResponseStatusCode: |
135 |
| - getter = func(s *Span) string { return strconv.Itoa(s.Status) } |
136 |
| - case attr.HTTPRoute: |
137 |
| - getter = func(s *Span) string { return s.Route } |
138 |
| - case attr.HTTPUrlPath: |
139 |
| - getter = func(s *Span) string { return s.Path } |
140 |
| - case attr.Client, attr.ClientAddr: |
141 |
| - getter = PeerAsClient |
142 |
| - case attr.Server, attr.ServerAddr: |
143 |
| - getter = func(s *Span) string { |
144 |
| - if s.Type == EventTypeHTTPClient { |
145 |
| - return HTTPClientHost(s) |
146 |
| - } |
147 |
| - return HostAsServer(s) |
148 |
| - } |
149 |
| - case attr.ServerPort: |
150 |
| - getter = func(s *Span) string { return strconv.Itoa(s.HostPort) } |
151 |
| - case attr.RPCMethod: |
152 |
| - getter = func(s *Span) string { return s.Path } |
153 |
| - case attr.RPCSystem: |
154 |
| - getter = func(_ *Span) string { return "grpc" } |
155 |
| - case attr.RPCGRPCStatusCode: |
156 |
| - getter = func(s *Span) string { return strconv.Itoa(s.Status) } |
157 |
| - case attr.DBOperation: |
158 |
| - getter = func(span *Span) string { return span.Method } |
159 |
| - case attr.ErrorType: |
160 |
| - getter = func(span *Span) string { |
161 |
| - if SpanStatusCode(span) == StatusCodeError { |
162 |
| - return "error" |
163 |
| - } |
164 |
| - return "" |
165 |
| - } |
166 |
| - case attr.DBSystemName: |
167 |
| - getter = func(span *Span) string { |
168 |
| - switch span.Type { |
169 |
| - case EventTypeSQLClient: |
170 |
| - return span.DBSystemName().Value.AsString() |
171 |
| - case EventTypeRedisClient, EventTypeRedisServer: |
172 |
| - return semconv.DBSystemRedis.Value.AsString() |
173 |
| - case EventTypeMongoClient: |
174 |
| - return semconv.DBSystemMongoDB.Value.AsString() |
175 |
| - } |
176 |
| - return "unknown" |
177 |
| - } |
178 |
| - case attr.DBCollectionName: |
179 |
| - getter = func(span *Span) string { |
180 |
| - if span.Type == EventTypeSQLClient { |
181 |
| - return span.DBSystemName().Value.AsString() |
182 |
| - } |
183 |
| - if span.Type == EventTypeMongoClient { |
184 |
| - return span.Path |
185 |
| - } |
186 |
| - return "" |
187 |
| - } |
188 |
| - case attr.MessagingSystem: |
189 |
| - getter = func(span *Span) string { |
190 |
| - if span.Type == EventTypeKafkaClient || span.Type == EventTypeKafkaServer { |
191 |
| - return "kafka" |
192 |
| - } |
193 |
| - return "unknown" |
194 |
| - } |
195 |
| - case attr.MessagingDestination: |
196 |
| - getter = func(span *Span) string { |
197 |
| - if span.Type == EventTypeKafkaClient || span.Type == EventTypeKafkaServer { |
198 |
| - return span.Path |
199 |
| - } |
200 |
| - return "" |
201 |
| - } |
202 |
| - case attr.ServiceInstanceID: |
203 |
| - getter = func(s *Span) string { return s.Service.UID.Instance } |
204 |
| - // resource metadata values below. Unlike OTEL, they are included here because they |
205 |
| - // belong to the metric, instead of the Resource |
206 |
| - case attr.Instance: |
207 |
| - getter = func(s *Span) string { return s.Service.UID.Instance } |
208 |
| - case attr.Job: |
209 |
| - getter = func(s *Span) string { return s.Service.Job() } |
210 |
| - case attr.ServiceName: |
211 |
| - getter = func(s *Span) string { return s.Service.UID.Name } |
212 |
| - case attr.ServiceNamespace: |
213 |
| - getter = func(s *Span) string { return s.Service.UID.Namespace } |
214 |
| - case attr.CudaKernelName: |
215 |
| - getter = func(s *Span) string { return s.Method } |
216 |
| - case attr.CudaMemcpyKind: |
217 |
| - getter = func(s *Span) string { return CudaMemcpyName(s.SubType) } |
218 |
| - default: |
219 |
| - getter = func(s *Span) string { return s.Service.Metadata[attrName] } |
| 127 | +func spanPromGetters(attrName attr.Name) (attributes.Getter[*Span, string], bool) { |
| 128 | + if otelGetter, ok := spanOTELGetters(attrName); ok { |
| 129 | + return func(span *Span) string { return otelGetter(span).Value.Emit() }, true |
220 | 130 | }
|
221 |
| - return getter, getter != nil |
| 131 | + // unlike the OTEL getters, when the attribute is not found, we need to look for it |
| 132 | + // in the metadata section |
| 133 | + return func(s *Span) string { return s.Service.Metadata[attrName] }, true |
222 | 134 | }
|
0 commit comments