Copyright The OpenTelemetry Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package correlation

import (
	
	
	

	
	
)
Temporary header name until W3C finalizes format. https://github.com/open-telemetry/opentelemetry-specification/blob/18b2752ebe6c7f0cdd8c7b2bcbdceb0ae3f5ad95/specification/correlationcontext/api.md#header-name
const correlationContextHeader = "otcorrelations"
CorrelationContext propagates Key:Values in W3C CorrelationContext format. nolint:golint
DefaultHTTPPropagator returns the default context correlation HTTP propagator.
Inject implements HTTPInjector.
func (CorrelationContext) ( context.Context,  propagation.HTTPSupplier) {
	 := MapFromContext()
	 := true
	var  strings.Builder
	.Foreach(func( label.KeyValue) bool {
		if ! {
			.WriteRune(',')
		}
		 = false
		.WriteString(url.QueryEscape(strings.TrimSpace((string)(.Key))))
		.WriteRune('=')
		.WriteString(url.QueryEscape(strings.TrimSpace(.Value.Emit())))
		return true
	})
	if .Len() > 0 {
		 := .String()
		.Set(correlationContextHeader, )
	}
}
Extract implements HTTPExtractor.
func (CorrelationContext) ( context.Context,  propagation.HTTPSupplier) context.Context {
	 := .Get(correlationContextHeader)
	if  == "" {
		return 
	}

	 := strings.Split(, ",")
	 := make([]label.KeyValue, 0, len())
	for ,  := range  {
		 := strings.Split(, ";")
		if len() < 1 {
			continue
		}
		 := strings.Split([0], "=")
		if len() < 2 {
			continue
		}
		,  := url.QueryUnescape([0])
		if  != nil {
			continue
		}
		 := strings.TrimSpace()
		,  := url.QueryUnescape([1])
		if  != nil {
			continue
		}
		 := strings.TrimSpace()
TODO (skaris): properties defiend https://w3c.github.io/correlation-context/, are currently just put as part of the value.
		var  strings.Builder
		.WriteString()
		for ,  := range [1:] {
			.WriteRune(';')
			.WriteString()
		}

		 = append(, label.String(, .String()))
	}

Only update the context if valid values were found
		return ContextWithMap(, NewMap(MapUpdate{
			MultiKV: ,
		}))
	}

	return 
}
GetAllKeys implements HTTPPropagator.