Skip to content

Commit

Permalink
Merge pull request #36 from krakend/fix_examples
Browse files Browse the repository at this point in the history
Remove the "layers" object in extra_config opentelemetry
  • Loading branch information
kpacha authored Oct 30, 2024
2 parents 327d5ff + 9304a17 commit fc4107f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 152 deletions.
19 changes: 19 additions & 0 deletions config/lura.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,22 @@ func LuraExtraCfg(extraCfg luraconfig.ExtraConfig) (*ConfigData, error) {

return cfg, nil
}

func LuraLayerExtraCfg(extraCfg luraconfig.ExtraConfig) (*LayersOpts, error) {
tmp, ok := extraCfg[Namespace]
if !ok {
return nil, ErrNoConfig
}

buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(tmp); err != nil {
return nil, err
}

cfg := new(LayersOpts)
if err := json.NewDecoder(buf).Decode(cfg); err != nil {
return nil, err
}

return cfg, nil
}
78 changes: 30 additions & 48 deletions example/docker_compose/conf/krakend_back/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,20 @@
"endpoint": "/back_combination/{id}",
"extra_config": {
"telemetry/opentelemetry": {
"layers": {
"global": {
"metrics_static_attributes": [
{
"key": "my_metric_global_override_attr",
"value": "my_metric_global_override_val"
}
],
"traces_static_attributes": [
{
"key": "my_trace_global_override_attr",
"value": "my_trace_global_override_val"
}
]
},
"proxy": {
"metrics_static_attributes": [
{
"key": "my_metric_proxy_override_attr",
"value": "my_metric_proxy_override_val"
}
],
,
"traces_static_attributes": [
{
"key": "my_trace_proxy_override_attr",
"value": "my_trace_proxy_override_val"
}
]
}
"proxy": {
"metrics_static_attributes": [
{
"key": "my_metric_proxy_override_attr",
"value": "my_metric_proxy_override_val"
}
],
,
"traces_static_attributes": [
{
"key": "my_trace_proxy_override_attr",
"value": "my_trace_proxy_override_val"
}
]
}
},
"backend": [
Expand All @@ -74,24 +58,22 @@
},
"extra_config": {
"telemetry/opentelemetry": {
"layers": {
"backend": {
"metrics": {
"static_attributes": [
{
"key": "my_metric_backend_override_attr",
"value": "my_metric_backend_override_val"
}
]
},
"traces": {
"static_attributes": [
{
"key": "my_trace_backend_override_attr",
"value": "my_trace_backend_override_val"
}
]
}
"backend": {
"metrics": {
"static_attributes": [
{
"key": "my_metric_backend_override_attr",
"value": "my_metric_backend_override_val"
}
]
},
"traces": {
"static_attributes": [
{
"key": "my_trace_backend_override_attr",
"value": "my_trace_backend_override_val"
}
]
}
}
}
Expand Down
94 changes: 32 additions & 62 deletions state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import (

type Config interface {
OTEL() OTEL
// GlobalOpts gets the configuration at the service level.
GlobalOpts() *config.GlobalOpts

// Gets the OTEL instance for a given endpoint
EndpointOTEL(cfg *luraconfig.EndpointConfig) OTEL
// EndpointPipeOpts retrieve "proxy" level configuration for a given
// endpoint.
EndpointPipeOpts(cfg *luraconfig.EndpointConfig) *config.PipeOpts
// EndpointBackendOpts should return a config for all the child
// backend of this endpoint.
//
// Deprecated: the interface should only need to fetch the BackendOpts
// from a luraconfig.Backend when configuring at the Backend level:
// the BackendOpts function must be used instead.
EndpointBackendOpts(cfg *luraconfig.Backend) *config.BackendOpts

BackendOTEL(cfg *luraconfig.Backend) OTEL
Expand Down Expand Up @@ -39,44 +48,30 @@ func (*StateConfig) EndpointOTEL(_ *luraconfig.EndpointConfig) OTEL {
return GlobalState()
}

// EndpointPipeOpts checks if there is an override for pipe ("proxy")
// options at the endpoint levels a fully replaces (it DOES NOT MERGE
// attributes) the existing config from the service level configuration.
// If none of those configs are found, it falls back to the defaults.
func (s *StateConfig) EndpointPipeOpts(cfg *luraconfig.EndpointConfig) *config.PipeOpts {
var sOpts *config.PipeOpts
var extraPOpts *config.PipeOpts

var opts *config.PipeOpts
if s != nil && s.cfgData.Layers != nil {
sOpts = s.cfgData.Layers.Pipe
opts = s.cfgData.Layers.Pipe
}

cfgExtra, err := config.LuraExtraCfg(cfg.ExtraConfig)
if err == nil && cfgExtra != nil && cfgExtra.Layers != nil {
extraPOpts = cfgExtra.Layers.Pipe
cfgLayer, err := config.LuraLayerExtraCfg(cfg.ExtraConfig)
if err == nil && cfgLayer != nil && cfgLayer.Pipe != nil {
opts = cfgLayer.Pipe
}

if extraPOpts == nil {
if sOpts == nil {
return new(config.PipeOpts)
}
return sOpts
} else if sOpts == nil {
return extraPOpts
if opts == nil {
return new(config.PipeOpts)
}

pOpts := new(config.PipeOpts)
*pOpts = *sOpts

pOpts.MetricsStaticAttributes = append(
pOpts.MetricsStaticAttributes,
cfgExtra.Layers.Pipe.MetricsStaticAttributes...,
)

pOpts.TracesStaticAttributes = append(
pOpts.TracesStaticAttributes,
cfgExtra.Layers.Pipe.TracesStaticAttributes...,
)

return pOpts
return opts
}

// EndpointBackendOpts is a bad interface function, as is should receive
// as a param a luraconfig.Endpoint .. but also makes no sense to have it
// because we only need the backend configuration at
func (s *StateConfig) EndpointBackendOpts(cfg *luraconfig.Backend) *config.BackendOpts {
return s.mergedBackendOpts(cfg)
}
Expand All @@ -90,45 +85,20 @@ func (s *StateConfig) BackendOpts(cfg *luraconfig.Backend) *config.BackendOpts {
}

func (s *StateConfig) mergedBackendOpts(cfg *luraconfig.Backend) *config.BackendOpts {
var extraBOpts *config.BackendOpts
var sOpts *config.BackendOpts

var opts *config.BackendOpts
if s != nil && s.cfgData.Layers != nil {
sOpts = s.cfgData.Layers.Backend
opts = s.cfgData.Layers.Backend
}

cfgExtra, err := config.LuraExtraCfg(cfg.ExtraConfig)
if err == nil && cfgExtra != nil && cfgExtra.Layers != nil {
extraBOpts = cfgExtra.Layers.Backend
cfgLayer, err := config.LuraLayerExtraCfg(cfg.ExtraConfig)
if err == nil && cfgLayer != nil && cfgLayer.Backend != nil {
opts = cfgLayer.Backend
}

if extraBOpts == nil {
if sOpts == nil {
return new(config.BackendOpts)
}
return sOpts
} else if sOpts == nil {
return extraBOpts
}

bOpts := new(config.BackendOpts)
*bOpts = *sOpts

if extraBOpts.Metrics != nil {
bOpts.Metrics.StaticAttributes = append(
bOpts.Metrics.StaticAttributes,
cfgExtra.Layers.Backend.Metrics.StaticAttributes...,
)
if opts == nil {
return new(config.BackendOpts)
}

if extraBOpts.Traces != nil {
bOpts.Traces.StaticAttributes = append(
bOpts.Traces.StaticAttributes,
cfgExtra.Layers.Backend.Traces.StaticAttributes...,
)
}

return bOpts
return opts
}

func (s *StateConfig) SkipEndpoint(endpoint string) bool {
Expand Down
Loading

0 comments on commit fc4107f

Please sign in to comment.