You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-12-01 23:12:29 +02:00
Golang opentelemetry-go draft API (incomplete) (#9)
* Work in progress from https://github.com/lightstep/opentelemetry-golang-prototype * Renames * Rename * Finish rename * Rename packages * README
This commit is contained in:
committed by
rghetia
parent
1429272864
commit
e17f4468a6
78
example/example.go
Normal file
78
example/example.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/open-telemetry/opentelemetry-go/api/log"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/metric"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/stats"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/tag"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/trace"
|
||||
|
||||
"github.com/open-telemetry/opentelemetry-go/exporter/loader"
|
||||
)
|
||||
|
||||
var (
|
||||
tracer = trace.GlobalTracer().
|
||||
WithComponent("example").
|
||||
WithResources(
|
||||
tag.New("whatevs").String("yesss"),
|
||||
)
|
||||
|
||||
fooKey = tag.New("ex.com/foo", tag.WithDescription("A Foo var"))
|
||||
barKey = tag.New("ex.com/bar", tag.WithDescription("A Bar var"))
|
||||
lemonsKey = tag.New("ex.com/lemons", tag.WithDescription("A Lemons var"))
|
||||
anotherKey = tag.New("ex.com/another")
|
||||
|
||||
oneMetric = metric.NewFloat64Gauge("ex.com/one",
|
||||
metric.WithKeys(fooKey, barKey, lemonsKey),
|
||||
metric.WithDescription("A gauge set to 1.0"),
|
||||
)
|
||||
|
||||
measureTwo = tag.NewMeasure("ex.com/two")
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
ctx = tag.NewContext(ctx,
|
||||
tag.Insert(fooKey.String("foo1")),
|
||||
tag.Insert(barKey.String("bar1")),
|
||||
)
|
||||
|
||||
gauge := oneMetric.Gauge(
|
||||
fooKey.Value(ctx),
|
||||
barKey.Value(ctx),
|
||||
lemonsKey.Int(10),
|
||||
)
|
||||
|
||||
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {
|
||||
|
||||
trace.SetError(ctx, true)
|
||||
|
||||
log.Log(ctx, "Nice operation!", tag.New("bogons").Int(100))
|
||||
|
||||
trace.Active(ctx).SetAttributes(anotherKey.String("yes"))
|
||||
|
||||
gauge.Set(ctx, 1)
|
||||
|
||||
return tracer.WithSpan(
|
||||
ctx,
|
||||
"Sub operation...",
|
||||
func(ctx context.Context) error {
|
||||
trace.Active(ctx).SetAttribute(lemonsKey.String("five"))
|
||||
|
||||
log.Logf(ctx, "Format schmormat %d!", 100)
|
||||
|
||||
stats.Record(ctx, measureTwo.M(1.3))
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
loader.Flush()
|
||||
}
|
||||
57
example/http/client/client.go
Normal file
57
example/http/client/client.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/open-telemetry/opentelemetry-go/api/tag"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/trace"
|
||||
"github.com/open-telemetry/opentelemetry-go/plugin/httptrace"
|
||||
|
||||
_ "github.com/open-telemetry/opentelemetry-go/exporter/loader"
|
||||
)
|
||||
|
||||
var (
|
||||
tracer = trace.GlobalTracer().
|
||||
WithService("client").
|
||||
WithComponent("main").
|
||||
WithResources(
|
||||
tag.New("whatevs").String("yesss"),
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := http.DefaultClient
|
||||
|
||||
ctx := tag.NewContext(context.Background(),
|
||||
tag.Insert(tag.New("username").String("donuts")),
|
||||
)
|
||||
|
||||
var body []byte
|
||||
|
||||
err := tracer.WithSpan(ctx, "say hello",
|
||||
func(ctx context.Context) error {
|
||||
req, _ := http.NewRequest("GET", "http://localhost:7777/hello", nil)
|
||||
|
||||
ctx, req, inj := httptrace.W3C(ctx, req)
|
||||
|
||||
trace.Inject(ctx, inj)
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
body, err = ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s", body)
|
||||
}
|
||||
7
example/http/server/modd.conf
Normal file
7
example/http/server/modd.conf
Normal file
@@ -0,0 +1,7 @@
|
||||
# A basic modd.conf file for Go development.
|
||||
|
||||
# Run go test on ALL modules on startup, and subsequently only on modules
|
||||
# containing changes.
|
||||
server.go {
|
||||
daemon +sigterm: go run server.go
|
||||
}
|
||||
49
example/http/server/server.go
Normal file
49
example/http/server/server.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/open-telemetry/opentelemetry-go/api/core"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/log"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/tag"
|
||||
"github.com/open-telemetry/opentelemetry-go/api/trace"
|
||||
"github.com/open-telemetry/opentelemetry-go/plugin/httptrace"
|
||||
|
||||
_ "github.com/open-telemetry/opentelemetry-go/exporter/loader"
|
||||
)
|
||||
|
||||
var (
|
||||
tracer = trace.GlobalTracer().
|
||||
WithService("server").
|
||||
WithComponent("main").
|
||||
WithResources(
|
||||
tag.New("whatevs").String("nooooo"),
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
helloHandler := func(w http.ResponseWriter, req *http.Request) {
|
||||
attrs, tags, spanCtx := httptrace.Extract(req)
|
||||
|
||||
req = req.WithContext(tag.WithMap(req.Context(), tag.NewMap(core.KeyValue{}, tags, core.Mutator{}, nil)))
|
||||
|
||||
ctx, span := tracer.Start(
|
||||
req.Context(),
|
||||
"hello",
|
||||
trace.WithAttributes(attrs...),
|
||||
trace.ChildOf(spanCtx),
|
||||
)
|
||||
defer span.Finish()
|
||||
|
||||
log.Log(ctx, "handling this...")
|
||||
|
||||
io.WriteString(w, "Hello, world!\n")
|
||||
}
|
||||
|
||||
http.HandleFunc("/hello", helloHandler)
|
||||
err := http.ListenAndServe(":7777", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user