1// Copyright 2019, OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package httptrace
16
17import (
18	"context"
19	"net/http"
20
21	"go.opentelemetry.io/otel/api/core"
22	"go.opentelemetry.io/otel/api/key"
23	"go.opentelemetry.io/otel/api/propagators"
24)
25
26const (
27	Vendor = "ot"
28)
29
30var (
31	HostKey = key.New("http.host")
32	URLKey  = key.New("http.url")
33
34	propagator = propagators.TraceContext{}
35)
36
37// Returns the Attributes, Context Entries, and SpanContext that were encoded by Inject.
38func Extract(ctx context.Context, req *http.Request) ([]core.KeyValue, []core.KeyValue, core.SpanContext) {
39	sc, correlationCtx := propagator.Extract(ctx, req.Header)
40
41	attrs := []core.KeyValue{
42		URLKey.String(req.URL.String()),
43		// Etc.
44	}
45
46	var correlationCtxKVs []core.KeyValue
47	correlationCtx.Foreach(func(kv core.KeyValue) bool {
48		correlationCtxKVs = append(correlationCtxKVs, kv)
49		return true
50	})
51
52	return attrs, correlationCtxKVs, sc
53}
54
55func Inject(ctx context.Context, req *http.Request) {
56	propagator.Inject(ctx, req.Header)
57}
58