1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-03-17 20:57:51 +02:00

Finish OTLP example

This commit is contained in:
wtong98 2020-05-27 10:06:33 -05:00
parent a4cf6e1d69
commit 80d1eb98ed
5 changed files with 127 additions and 40 deletions

View File

@ -3,7 +3,83 @@ 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.
application using OpenTelemetry's own wire protocal OTLP. We will also walk
you through configuring a collector to accept OTLP exports.
### How to run?
#### Prequisites
- go >=1.14 installed
- `GOPATH` is configured
- (optional) OpenTelemetry collector is available
#### Start the Application
An example application is included in `example/otlp`. It simulates the process
of scribing a spell scroll (e.g. in [D&D](https://roll20.net/compendium/dnd5e/Spell%20Scroll#content)).
The application has been instrumented and exports both trace and metric data
via OTLP to any listening receiver. To run it:
```sh
go get -d go.opentelemetry.io/otel
cd $GOPATH/go.opentelemetry.io/otel/example/otlp
go run main.go
```
The application is currently configured to transmit exported data to
`localhost:55680`. See [main.go](example/otlp/main.go) for full details.
Note, if you don't have a receiver configured to take in metric data, the
application will complain about being unable to connect.
#### (optional) Configure the Collector
Follow the instructions [on the
website](https://opentelemetry.io/docs/collector/about/) to install a working
instance of the collector. This example assumes you have the collector installed
locally.
To configure the collector to accept OTLP traffic from our application,
ensure that it has the following configs:
```yaml
receivers:
otlp:
endpoint: 0.0.0.0:55680 # listens to localhost:55680
# potentially other receivers
service:
pipelines:
traces:
receivers:
- otlp
# potentially other receivers
processors: # whatever processors you need
exporters: # wherever you want your data to go
metrics:
receivers:
-otlp
# potentially other receivers
processors: etc
exporters: etc
# other services
```
An example config has been provided at
[example-otlp-config.yaml](example/otlp/example-otlp-config.yaml).
Then to run:
```sh
./[YOUR_COLLECTOR_BINARY] --config [PATH_TO_CONFIG]
```
If you use the example config, it's set to export to `stdout`. If you run
the collector on the same machine as the example application, you should
see trace and metric outputs from the collector.
## HTTP
This is a simple example that demonstrates tracing http request from client to server. The example

View File

@ -0,0 +1,29 @@
extensions:
health_check:
receivers:
otlp:
endpoint: 0.0.0.0:55680
processors:
batch:
queued_retry:
exporters:
logging:
loglevel: debug
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch, queued_retry]
exporters: [logging]
metrics:
receivers: [otlp]
exporters: [logging]
extensions: [health_check]

View File

@ -4,5 +4,6 @@ go 1.14
require (
github.com/open-telemetry/opentelemetry-collector v0.3.0 // indirect
go.opentelemetry.io/otel/exporters/otlp v0.6.0 // indirect
go.opentelemetry.io/otel v0.6.0
go.opentelemetry.io/otel/exporters/otlp v0.6.0
)

View File

@ -1,4 +1,5 @@
// dummy application for testing opentelemetry Go agent + collector
// Example application showcasing opentelemetry Go using the OTLP wire
// protocal
package main
@ -17,8 +18,9 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// TODO: basic documentation
func initProvider() {
// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers.
func initProvider() (*otlp.Exporter, *push.Controller) {
exp, err := otlp.NewExporter(
otlp.WithInsecure(),
otlp.WithAddress("localhost:55680"),
@ -27,7 +29,7 @@ func initProvider() {
traceProvider, err := sdktrace.NewProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithSyncer(exp),
sdktrace.WithSyncer(exp),
)
handleErr(err, "Failed to create trace provider: %v")
@ -35,17 +37,20 @@ func initProvider() {
simple.NewWithExactDistribution(),
exp,
push.WithStateful(true),
push.WithPeriod(time.Duration(5) * time.Second),
push.WithPeriod(2*time.Second),
)
global.SetTraceProvider(traceProvider)
global.SetMeterProvider(pusher.Provider())
pusher.Start()
return exp, pusher
}
func main() {
initProvider()
exp, pusher := initProvider()
defer exp.Stop()
defer pusher.Stop() // pushes any last exports to the receiver
tracer := global.Tracer("mage-sense")
meter := global.Meter("mage-read")
@ -56,6 +61,7 @@ func main() {
kv.String("priority", "Ultra"),
}
// Observer metric example
oneMetricCB := func(_ context.Context, result metric.Float64ObserverResult) {
result.Observe(1, commonLabels...)
}
@ -63,16 +69,19 @@ func main() {
metric.WithDescription("A ValueObserver set to 1.0"),
)
// Recorder metric example
valuerecorder := metric.Must(meter).
NewFloat64ValueRecorder("scrying.glass.two").
Bind(commonLabels...)
defer valuerecorder.Unbind()
ctx, span := tracer.Start(context.Background(), "Archmage-Overlord")
// work begins
ctx, span := tracer.Start(context.Background(), "Archmage-Overlord-Inspection")
for i := 0; i < 10; i++ {
_, innerSpan := tracer.Start(ctx, fmt.Sprintf("Minion-%d", i))
log.Println("Minions hard at work, scribing...")
valuerecorder.Record(ctx, float64(i) * 1.5)
valuerecorder.Record(ctx, float64(i)*1.5)
<-time.After(time.Second)
innerSpan.End()
}
@ -80,7 +89,7 @@ func main() {
span.End()
<-time.After(time.Second)
log.Println("Spell-scroll scribed!")
log.Println("Spell-scroll scribed!")
}
func handleErr(err error, message string) {

View File

@ -1,28 +0,0 @@
<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=&quot;&quot; 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>