2020-02-03 22:07:53 +01:00
|
|
|
// Copyright 2020, 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.
|
|
|
|
|
|
2020-01-28 19:13:46 +01:00
|
|
|
package correlation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type correlationsType struct{}
|
|
|
|
|
|
|
|
|
|
var correlationsKey = &correlationsType{}
|
|
|
|
|
|
2020-02-27 18:10:52 +01:00
|
|
|
// ContextWithMap returns a context with the Map entered into it.
|
|
|
|
|
func ContextWithMap(ctx context.Context, m Map) context.Context {
|
2020-01-28 19:13:46 +01:00
|
|
|
return context.WithValue(ctx, correlationsKey, m)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 22:07:53 +01:00
|
|
|
// NewContext returns a context with the map from passed context
|
|
|
|
|
// updated with the passed key-value pairs.
|
2020-01-28 19:13:46 +01:00
|
|
|
func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context {
|
2020-02-27 18:10:52 +01:00
|
|
|
return ContextWithMap(ctx, MapFromContext(ctx).Apply(MapUpdate{
|
2020-01-28 19:13:46 +01:00
|
|
|
MultiKV: keyvalues,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 18:10:52 +01:00
|
|
|
// MapFromContext gets the current Map from a Context.
|
|
|
|
|
func MapFromContext(ctx context.Context) Map {
|
2020-01-28 19:13:46 +01:00
|
|
|
if m, ok := ctx.Value(correlationsKey).(Map); ok {
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
return NewEmptyMap()
|
|
|
|
|
}
|