1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-24 03:47:19 +02:00
Edward Muller 063035e9e0 Add goimports and golangci-lint (#17)
goimports for import rewritting
golangci-lint as the configurable linting swiss army knife.

These tools are recorded in [tools.go](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module). This records
them as a dependency to make sure we're all using the same tool versions.

To make sure this project's tool's versions don't stomp over versions
from other projects, they are installed in ./.tools, which is
.gitignored.

goimports was run and fixed up a single file:
plugin/httptrace/httptrace.go

I prefer to group local packages below external packages, hence the use
of goimports -local option.

.golangci.yml contains nothing but an incomplete set of defaults ATM.
I expect those to change over time though. ;-)

To use, run:

$ make precommit

Fixes #15
2019-06-18 17:09:49 -07:00

103 lines
2.5 KiB
Go

// Copyright 2019, 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 httptrace
import (
"encoding/binary"
"net/http"
"github.com/lightstep/tracecontext.go"
"github.com/lightstep/tracecontext.go/tracestate"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/tag"
)
const (
Vendor = "ot"
)
type (
hinjector struct {
*http.Request
}
)
var (
HostKey = tag.New("http.host")
URLKey = tag.New("http.url")
encoding = binary.BigEndian
)
// Returns the Attributes, Context Tags, and SpanContext that were encoded by Inject.
func Extract(req *http.Request) ([]core.KeyValue, []core.KeyValue, core.SpanContext) {
tc, err := tracecontext.FromHeaders(req.Header)
if err != nil {
return nil, nil, core.SpanContext{}
}
var sc core.SpanContext
sc.SpanID = encoding.Uint64(tc.TraceParent.SpanID[0:8])
sc.TraceIDHigh = encoding.Uint64(tc.TraceParent.TraceID[0:8])
sc.TraceIDLow = encoding.Uint64(tc.TraceParent.TraceID[8:16])
attrs := []core.KeyValue{
URLKey.String(req.URL.String()),
// Etc.
}
var tags []core.KeyValue
for _, ts := range tc.TraceState {
if ts.Vendor != Vendor {
continue
}
// TODO: max-hops, type conversion questions answered,
// case-conversion questions.
tags = append(tags, tag.New(ts.Tenant).String(ts.Value))
}
return attrs, tags, sc
}
func (h hinjector) Inject(sc core.SpanContext, tags tag.Map) {
var tc tracecontext.TraceContext
var sid [8]byte
var tid [16]byte
encoding.PutUint64(sid[0:8], sc.SpanID)
encoding.PutUint64(tid[0:8], sc.TraceIDHigh)
encoding.PutUint64(tid[8:16], sc.TraceIDLow)
tc.TraceParent.Version = tracecontext.Version
tc.TraceParent.TraceID = tid
tc.TraceParent.SpanID = sid
tc.TraceParent.Flags.Recorded = true // Note: not implemented.
tags.Foreach(func(kv core.KeyValue) bool {
// TODO: implement MaxHops
tc.TraceState = append(tc.TraceState, tracestate.Member{
Vendor: Vendor,
Tenant: kv.Key.Name(),
Value: kv.Value.Emit(),
})
return true
})
tc.SetHeaders(h.Header)
}