mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-16 10:19:23 +02:00
574463c9ef
* Drop entry from correlation map Entry used to contain stuff like TTL, but right now the notion of entry was dropped from the spec. * Compute exact size of the correlations map The map will be immutable, so spend some more time to ensure that we will not unnecessarily waste some memory on a basically read-only map. * Allow dropping keys from correlations map This is to follow the spec. Before this change it was possible in an awkward way by using Foreach to gather and filter the key-value pairs, and then calling NewMap with a MultiKV MapUpdate. * Document the correlation package * Add missing license blurbs in correlation package * Add tests for deleting items in correlations * Factor out getting map size and test it This is an implementation detail that can't be tested in a black-box manner. * Fix copyright dates * Simplify/disambiguate keySet function parameters/return values * Fix typo in Apply docs * Fix test names * Explain the nonsense of dropping keys from new map
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
// 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.
|
|
|
|
package correlation
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
)
|
|
|
|
type correlationsType struct{}
|
|
|
|
var correlationsKey = &correlationsType{}
|
|
|
|
// WithMap returns a context with the Map entered into it.
|
|
func WithMap(ctx context.Context, m Map) context.Context {
|
|
return context.WithValue(ctx, correlationsKey, m)
|
|
}
|
|
|
|
// NewContext returns a context with the map from passed context
|
|
// updated with the passed key-value pairs.
|
|
func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context {
|
|
return WithMap(ctx, FromContext(ctx).Apply(MapUpdate{
|
|
MultiKV: keyvalues,
|
|
}))
|
|
}
|
|
|
|
// FromContext gets the current Map from a Context.
|
|
func FromContext(ctx context.Context) Map {
|
|
if m, ok := ctx.Value(correlationsKey).(Map); ok {
|
|
return m
|
|
}
|
|
return NewEmptyMap()
|
|
}
|