mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-20 03:30:02 +02:00
Add OTLP trace
This commit is contained in:
parent
f1112a4fbb
commit
69ddc81a81
@ -1,4 +1,9 @@
|
||||
# Example
|
||||
Here is a collection of OpenTelemtry-Go examples showcasing basic functionality.
|
||||
|
||||
## OTLP
|
||||
This example demonstrates how to export trace and metric data from an
|
||||
application using OpenTelemetry's own wire protocal OTLP.
|
||||
|
||||
## HTTP
|
||||
This is a simple example that demonstrates tracing http request from client to server. The example
|
||||
|
8
example/otlp/go.mod
Normal file
8
example/otlp/go.mod
Normal file
@ -0,0 +1,8 @@
|
||||
module github.com/wtong98/otel-test
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/open-telemetry/opentelemetry-collector v0.3.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp v0.6.0 // indirect
|
||||
)
|
1264
example/otlp/go.sum
Normal file
1264
example/otlp/go.sum
Normal file
File diff suppressed because it is too large
Load Diff
62
example/otlp/main.go
Normal file
62
example/otlp/main.go
Normal file
@ -0,0 +1,62 @@
|
||||
// dummy application for testing opentelemetry Go agent + collector
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
// "github.com/open-telemetry/opentelemetry-collector/translator/conventions"
|
||||
|
||||
"go.opentelemetry.io/otel/api/global"
|
||||
// "go.opentelemetry.io/otel/api/kv"
|
||||
// "go.opentelemetry.io/otel/api/metric"
|
||||
"go.opentelemetry.io/otel/exporters/otlp"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
// tracestdout "go.opentelemetry.io/otel/exporters/trace/stdout"
|
||||
)
|
||||
|
||||
func initExporter() {
|
||||
exp, err := otlp.NewExporter(
|
||||
otlp.WithInsecure(),
|
||||
otlp.WithAddress("localhost:55680"),
|
||||
)
|
||||
// exp, err := tracestdout.NewExporter(tracestdout.Options{PrettyPrint: true})
|
||||
handleErr(err, "Failed to create exporter: $v")
|
||||
|
||||
// defer handleErr(exp.Stop(), "Failed to stop exporter: %v")
|
||||
|
||||
provider, err := sdktrace.NewProvider(
|
||||
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
||||
sdktrace.WithSyncer(exp),
|
||||
)
|
||||
handleErr(err, "Failed to create trace provider: %v")
|
||||
|
||||
global.SetTraceProvider(provider)
|
||||
}
|
||||
|
||||
func main() {
|
||||
initExporter()
|
||||
tracer := global.Tracer("mage-sense")
|
||||
|
||||
ctx, span := tracer.Start(context.Background(), "Archmage-Overlord")
|
||||
for i := 0; i < 10; i++ {
|
||||
_, innerSpan := tracer.Start(ctx, fmt.Sprintf("Minion-%d", i))
|
||||
log.Println("Minions hard at work, scribing...")
|
||||
<-time.After(time.Second)
|
||||
innerSpan.End()
|
||||
}
|
||||
|
||||
span.End()
|
||||
<-time.After(time.Second)
|
||||
|
||||
log.Println("Spell-scroll scribed!")
|
||||
}
|
||||
|
||||
func handleErr(err error, message string) {
|
||||
if err != nil {
|
||||
log.Fatalf(message, err)
|
||||
}
|
||||
}
|
28
example/preview.html
Normal file
28
example/preview.html
Normal file
@ -0,0 +1,28 @@
|
||||
<h1 id="example">Example</h1>
|
||||
<h2 id="http">HTTP</h2>
|
||||
<p>This is a simple example that demonstrates tracing http request from client to server. The example shows key aspects of tracing such as:</p>
|
||||
<ul>
|
||||
<li>Root Span (on Client)</li>
|
||||
<li>Child Span (on Client)</li>
|
||||
<li>Child Span from a Remote Parent (on Server)</li>
|
||||
<li>SpanContext Propagation (from Client to Server)</li>
|
||||
<li>Span Events</li>
|
||||
<li>Span Attributes</li>
|
||||
</ul>
|
||||
<p>Example uses - open-telemetry SDK as trace instrumentation provider, - httptrace plugin to facilitate tracing http request on client and server - http trace_context propagation to propagate SpanContext on the wire. - stdout exporter to print information about spans in the terminal</p>
|
||||
<h3 id="how-to-run">How to run?</h3>
|
||||
<h4 id="prequisites">Prequisites</h4>
|
||||
<ul>
|
||||
<li>go 1.13 installed</li>
|
||||
<li>GOPATH is configured.</li>
|
||||
</ul>
|
||||
<h4 id="download-git-repo">1 Download git repo</h4>
|
||||
<pre><code>GO111MODULE="" go get -d go.opentelemetry.io/otel</code></pre>
|
||||
<h4 id="start-server">2 Start Server</h4>
|
||||
<pre><code>cd $GOPATH/src/go.opentelemetry.io/otel/example/http/
|
||||
go run ./server/server.go</code></pre>
|
||||
<h4 id="start-client">3 Start Client</h4>
|
||||
<pre><code>cd $GOPATH/src/go.opentelemetry.io/otel/example/http/
|
||||
go run ./client/client.go</code></pre>
|
||||
<h4 id="check-traces-in-stdout">4 Check traces in stdout</h4>
|
||||
<p>The spans should be visible in stdout in the order that they were exported.</p>
|
Loading…
x
Reference in New Issue
Block a user