@@ -3,10 +3,12 @@ package lura
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "strings"
6
7
"time"
7
8
8
9
"go.opentelemetry.io/otel/attribute"
9
10
"go.opentelemetry.io/otel/metric"
11
+ "go.opentelemetry.io/otel/semconv/v1.21.0"
10
12
"go.opentelemetry.io/otel/trace"
11
13
12
14
"github.com/luraproject/lura/v2/config"
34
36
)
35
37
)
36
38
37
- // Middleare creates a proxy that instruments the proxy it wraps by creating an span if enabled,
39
+ // Middleware creates a proxy that instruments the proxy it wraps by creating an span if enabled,
38
40
// and report the duration of this stage in metrics if enabled.
39
- func Middleware (name string , stage string , gsf state.GetterFn , metricsEnabled bool , tracesEnabled bool ) proxy.Middleware {
41
+ func Middleware (staticAttrs []attribute.KeyValue , gsf state.GetterFn , metricsEnabled bool , tracesEnabled bool ,
42
+ spanName string ) proxy.Middleware {
43
+
40
44
if gsf == nil {
41
45
gsf = state .GlobalState
42
46
}
@@ -62,10 +66,7 @@ func Middleware(name string, stage string, gsf state.GetterFn, metricsEnabled bo
62
66
if err != nil {
63
67
reportMetrics = false
64
68
}
65
- metricAttrs := metric .WithAttributes (
66
- attribute .String ("krakend.name" , name ),
67
- attribute .String ("krakend.stage" , stage ),
68
- )
69
+ metricAttrs := metric .WithAttributes (staticAttrs ... )
69
70
70
71
reportTrace := tracesEnabled
71
72
tracer := gs .Tracer ()
@@ -83,15 +84,15 @@ func Middleware(name string, stage string, gsf state.GetterFn, metricsEnabled bo
83
84
ctx = trace .ContextWithSpan (ctx , ginSpan )
84
85
}
85
86
// start the new Context, for the stage:
86
- ctx , span = tracer .Start (ctx , name )
87
+ ctx , span = tracer .Start (ctx , spanName )
87
88
if ginSpan != nil {
88
89
// we need to update the key with the new span, otherwise, deeper middlewares
89
90
// (like when from pipe -> proxy), would get the span from the parent, instead
90
91
// of the one in the context
91
92
ctx = context .WithValue (ctx , state .KrakenDContextOTELStrKey , span )
92
93
}
93
94
// TODO: CHECK that we have this attribute set !!
94
- span .SetAttributes (attribute . String ( "krakend.stage" , stage ) )
95
+ span .SetAttributes (staticAttrs ... )
95
96
}
96
97
97
98
startedAt := time .Now ()
@@ -154,7 +155,11 @@ func ProxyFactory(pf proxy.Factory, gsfn state.GetterFn, opts *kotelconfig.PipeO
154
155
// in original opencensus, we prefixed the endpoint with a `pipe` prefix
155
156
// return Middleware("pipe-" + cfg.Endpoint)(next), nil
156
157
urlPattern := kotelconfig .NormalizeURLPattern (cfg .Endpoint )
157
- return Middleware (urlPattern , "pipe" , gsfn , metricsEnabled , tracesEnabled )(next ), nil
158
+ staticAttrs := []attribute.KeyValue {
159
+ semconv .URLPath (urlPattern ),
160
+ attribute .String ("krakend.stage" , "pipe" ),
161
+ }
162
+ return Middleware (staticAttrs , gsfn , metricsEnabled , tracesEnabled , urlPattern )(next ), nil
158
163
}
159
164
}
160
165
@@ -177,6 +182,17 @@ func BackendFactory(bf proxy.BackendFactory, gsfn state.GetterFn, opts *kotelcon
177
182
}
178
183
}
179
184
urlPattern := kotelconfig .NormalizeURLPattern (cfg .URLPattern )
180
- return Middleware (urlPattern , "backend" , gsfn , metricsEnabled , tracesEnabled )(next )
185
+ parentEndpoint := kotelconfig .NormalizeURLPattern (cfg .ParentEndpoint )
186
+ hosts := strings .Join (cfg .Host , "_" )
187
+ // TODO: check if we want to have the hosts as static attribute
188
+ staticAttrs := []attribute.KeyValue {
189
+ semconv .URLPath (urlPattern ),
190
+ attribute .String ("krakend.stage" , "backend" ),
191
+ attribute .String ("krakend.endpoint" , parentEndpoint ),
192
+ semconv .ServerAddress (hosts ),
193
+ }
194
+ // TODO: check if we want to have the hosts, to paths that are the same, but for different hosts
195
+ spanName := strings .Join (cfg .Host , "_" ) + urlPattern
196
+ return Middleware (staticAttrs , gsfn , metricsEnabled , tracesEnabled , spanName )(next )
181
197
}
182
198
}
0 commit comments