Skip to content

Commit c19d3ae

Browse files
committed
set parent endpoint as http client name and use otel state getter function for client transport
1 parent e2d45b7 commit c19d3ae

File tree

6 files changed

+29
-31
lines changed

6 files changed

+29
-31
lines changed

config/lura.go

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ func FromLura(srvCfg config.ServiceConfig) (*Config, error) {
3535
return nil, err
3636
}
3737

38-
// TODO: we might want to move these defaults to the config.go
39-
// file, as is not specific to reading the config from the Lura
40-
// config.
4138
if cfg.Layers == nil {
4239
cfg.Layers = &LayersOpts{}
4340
}

example/server/main.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func main() {
7474
serviceConfig.Port = *port
7575
}
7676

77-
// TODO: how to register the exporter factory
7877
logger, _ := logging.NewLogger(*logLevel, os.Stdout, "[KRAKEND]")
7978

8079
obsConfig, err := kotelconfig.FromLura(serviceConfig)
@@ -89,17 +88,19 @@ func main() {
8988
return
9089
}
9190

91+
otelStateFn := state.GlobalState
92+
9293
bf := func(backendConfig *config.Backend) proxy.Proxy {
9394
reqExec := lura.HTTPRequestExecutorFromConfig(client.NewHTTPClient,
94-
backendConfig, obsConfig.Layers.Backend, obsConfig.SkipPaths)
95+
backendConfig, obsConfig.Layers.Backend, obsConfig.SkipPaths, otelStateFn)
9596
return proxy.NewHTTPProxyWithHTTPExecutor(backendConfig, reqExec, backendConfig.Decoder)
9697
}
97-
bf = lura.BackendFactory(bf, state.GlobalState, obsConfig.Layers.Backend, obsConfig.SkipPaths)
98+
bf = lura.BackendFactory(bf, otelStateFn, obsConfig.Layers.Backend, obsConfig.SkipPaths)
9899

99100
defaultPF := proxy.NewDefaultFactory(bf, logger)
100-
pf := lura.ProxyFactory(defaultPF, state.GlobalState, obsConfig.Layers.Pipe, obsConfig.SkipPaths)
101+
pf := lura.ProxyFactory(defaultPF, otelStateFn, obsConfig.Layers.Pipe, obsConfig.SkipPaths)
101102

102-
handlerF := otelgin.New(krakendgin.EndpointHandler, state.GlobalState,
103+
handlerF := otelgin.New(krakendgin.EndpointHandler, otelStateFn,
103104
obsConfig.Layers.Router, obsConfig.SkipPaths)
104105
// setup the krakend router
105106
routerFactory := krakendgin.NewFactory(krakendgin.Config{

http/client/client.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import (
4242
// InstrumentedHTTPClient creates a new instrumented http client with the options provided.
4343
// If the provided options are nil, the default options (with everything enabled) will be used.
4444
// Check the [TransportOptions] struct for details..
45-
func InstrumentedHTTPClient(c *http.Client, t *TransportOptions, clientName string) (*http.Client, error) {
45+
func InstrumentedHTTPClient(c *http.Client, t *TransportOptions, clientName string,
46+
getState state.GetterFn) (*http.Client, error) {
4647
// the default options is to have everything activated
4748
// TODO: we might want to return the provided client if no transport options are provided ?
4849
// TODO: we might want to return an error if no transport options are provided ?
@@ -57,7 +58,7 @@ func InstrumentedHTTPClient(c *http.Client, t *TransportOptions, clientName stri
5758
ReadPayload: true,
5859
DetailedConnection: false,
5960
},
60-
OTELInstance: state.GlobalState,
61+
OTELInstance: getState,
6162
}
6263
if t != nil {
6364
opts = *t

http/client/transport.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ type Transport struct {
5959
// TODO:
6060
// StartOptions trace.StartOptions
6161

62-
// TODO: probably combine the ENDPOINT URL + URL PATTER
63-
spanName string // the name for this span (we can use the URL Pattern
64-
6562
tracesOpts TransportTracesOptions
6663
metricsOpts TransportMetricsOptions
6764
getOTELInstance state.GetterFn
@@ -109,17 +106,17 @@ func readWrapperBuilder(metricsOpts *TransportMetricsOptions, tracesOpts *Transp
109106

110107
// NewRoundTripper creates an instrumented round tripper.
111108
func NewRoundTripper(base http.RoundTripper, metricsOpts TransportMetricsOptions,
112-
tracesOpts TransportTracesOptions, spanName string, stateGetter state.GetterFn,
109+
tracesOpts TransportTracesOptions, clientName string, stateGetter state.GetterFn,
113110
) http.RoundTripper {
114-
rt := newTransport(base, metricsOpts, tracesOpts, spanName, stateGetter)
111+
rt := newTransport(base, metricsOpts, tracesOpts, clientName, stateGetter)
115112
if rt == nil {
116113
return base
117114
}
118115
return rt
119116
}
120117

121118
func newTransport(base http.RoundTripper, metricsOpts TransportMetricsOptions,
122-
tracesOpts TransportTracesOptions, spanName string, stateGetter state.GetterFn,
119+
tracesOpts TransportTracesOptions, clientName string, stateGetter state.GetterFn,
123120
) *Transport {
124121
if !tracesOpts.Enabled() && !metricsOpts.Enabled() {
125122
return nil
@@ -146,12 +143,11 @@ func newTransport(base http.RoundTripper, metricsOpts TransportMetricsOptions,
146143
return &Transport{
147144
base: base,
148145
propagator: otel.GetTextMapPropagator(),
149-
spanName: spanName,
150146
getOTELInstance: stateGetter,
151147
tracesOpts: tracesOpts,
152148
metricsOpts: metricsOpts,
153-
metrics: newTransportMetrics(&metricsOpts, meter),
154-
traces: newTransportTraces(&tracesOpts, tracer, spanName),
149+
metrics: newTransportMetrics(&metricsOpts, meter, clientName),
150+
traces: newTransportTraces(&tracesOpts, tracer, clientName),
155151
readerWrapper: readWrapperBuilder(&metricsOpts, &tracesOpts, meter, tracer),
156152
}
157153
}

http/client/transport_metrics.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ type transportMetrics struct {
6666
getConnLatency metric.Float64Histogram
6767
dnsLatency metric.Float64Histogram
6868
tlsLatency metric.Float64Histogram
69+
70+
// to identify the source of the request (in KrakenD the front facing endpoint)
71+
clientName string
6972
}
7073

71-
func newTransportMetrics(metricsOpts *TransportMetricsOptions, meter metric.Meter) *transportMetrics {
74+
func newTransportMetrics(metricsOpts *TransportMetricsOptions, meter metric.Meter, clientName string) *transportMetrics {
7275
if meter == nil {
7376
return nil
7477
}
@@ -98,15 +101,14 @@ func (m *transportMetrics) report(rtt *roundTripTracking, attrs []attribute.KeyV
98101
return
99102
}
100103

101-
var attrM []attribute.KeyValue
104+
attrM := make([]attribute.KeyValue, len(attrs), len(attrs)+2)
105+
copy(attrM, attrs)
106+
if len(m.clientName) > 0 {
107+
attrM = append(attrM, attribute.Key("clientname").String(m.clientName))
108+
}
102109
if rtt.err == nil {
103-
attrM = make([]attribute.KeyValue, len(attrs), len(attrs)+1)
104-
copy(attrM, attrs)
105-
attrM = append(attrM, semconv.HTTPResponseStatusCode(int(rtt.resp.StatusCode)))
106-
} else {
107110
// if we fail on the client side, we do not have a status code ..
108-
// attrM = append(attrM, semconv.HTTPResponseStatusCode(int(0))
109-
attrM = attrs
111+
attrM = append(attrM, semconv.HTTPResponseStatusCode(int(rtt.resp.StatusCode)))
110112
}
111113
attrOpt := metric.WithAttributeSet(attribute.NewSet(attrM...))
112114

lura/backend.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
otelconfig "github.com/krakend/krakend-otel/config"
1414
clienthttp "github.com/krakend/krakend-otel/http/client"
15+
otelstate "github.com/krakend/krakend-otel/state"
1516
)
1617

1718
var defaultOpts = otelconfig.BackendOpts{
@@ -35,7 +36,8 @@ var defaultOpts = otelconfig.BackendOpts{
3536
// for the backend requests.
3637
func HTTPRequestExecutorFromConfig(clientFactory transport.HTTPClientFactory,
3738
cfg *config.Backend, opts *otelconfig.BackendOpts, skipPaths []string,
38-
) transport.HTTPRequestExecutor {
39+
getState otelstate.GetterFn) transport.HTTPRequestExecutor {
40+
3941
for _, sp := range skipPaths {
4042
if cfg.ParentEndpoint == sp {
4143
// TODO: check if there might be some othe executor that is
@@ -87,8 +89,7 @@ func HTTPRequestExecutorFromConfig(clientFactory transport.HTTPClientFactory,
8789
}
8890

8991
// TODO: check how we want to deal with this "clientName"
90-
endpoint := "" // we need to have the endpoint accessible in the backend config structure
91-
clientName := endpoint + urlPattern
92+
clientName := cfg.ParentEndpoint + "." + urlPattern
9293

9394
t := clienthttp.TransportOptions{
9495
MetricsOpts: clienthttp.TransportMetricsOptions{
@@ -106,7 +107,7 @@ func HTTPRequestExecutorFromConfig(clientFactory transport.HTTPClientFactory,
106107
}
107108

108109
return func(ctx context.Context, req *http.Request) (*http.Response, error) {
109-
c, err := clienthttp.InstrumentedHTTPClient(clientFactory(ctx), &t, clientName)
110+
c, err := clienthttp.InstrumentedHTTPClient(clientFactory(ctx), &t, clientName, getState)
110111
if err != nil {
111112
return nil, err
112113
}

0 commit comments

Comments
 (0)