1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-04-23 11:58:56 +02:00
Yoshi Yamaguchi 9ed97980e2 Add Stackdriver Trace exporter for trace. (#160)
* Add Stackdriver Trace exporter for trace.

TODOs for future work is:

* to replace bundler.Bundler

* to add proper tests for the exporter

* to move the exporter to proper repository once it will be created.

* Change to use functions for the exporter initialization instead of
passing option struct directly.
This fix is aliging the same fix for Jaeger (#146, #161)

* Change Option struct to be function type

* Change the original Option struct to be private

* Add line comments to maxMessageEventsPerSpan to leave it for future implementation

* Fix unnessesary expressions specified by `make precommit`

Left errors by `make precommit` in experimental/bridge/opentracing.

* Ran make precommit

* Add new line at EOF

* WIP: Start implementing BatchSpanExporter interfaces

* Change to use RegisterSpanProcessor to register bsp

* Change function names to fit current implementation of sdk

* Removed google.golang.org/api/support/bundler and implement ssp and bsp

* Change spanProcessor as a member of Exporter.

* Fix option names used for BatchSpanProcessor initialization.

* Change Exporter.Shutdown just to unregister spanProcessor.

* Removed copyright statements of OpenCensus.

* Fix small typo and EOF new line

* Fix interfaces of ExportSpan/ExportSpans to meet SpanSyncer/SpanBatcher

* Change to follow context.Context passed in ExportSpan/ExportSpans

* Fix Stackdriver Exporter to hold sync.Once to lock when it is registered and
unregistered.
2019-10-08 23:33:19 -07:00

96 lines
2.4 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 main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"net/http"
"time"
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/key"
"go.opentelemetry.io/api/tag"
"go.opentelemetry.io/api/trace"
"go.opentelemetry.io/exporter/trace/stackdriver"
"go.opentelemetry.io/plugin/httptrace"
sdktrace "go.opentelemetry.io/sdk/trace"
)
func initTracer() {
// Register SDK as trace provider.
sdktrace.Register()
projectID := os.Getenv("PROJECT_ID")
// Create Stackdriver exporter to be able to retrieve
// the collected spans.
exporter, err := stackdriver.NewExporter(
stackdriver.WithProjectID(projectID),
)
if err != nil {
log.Fatal(err)
}
exporter.RegisterSimpleSpanProcessor()
// For the demonstration, use sdktrace.AlwaysSample sampler to sample all traces.
// In a production application, use sdktrace.ProbabilitySampler with a desired probability.
sdktrace.ApplyConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()})
}
func main() {
initTracer()
client := http.DefaultClient
ctx := tag.NewContext(context.Background(),
tag.Insert(key.New("username").String("donuts")),
)
var body []byte
err := trace.GlobalTracer().WithSpan(ctx, "say hello",
func(ctx context.Context) error {
req, _ := http.NewRequest("GET", "http://localhost:7777/hello", nil)
ctx, req = httptrace.W3C(ctx, req)
httptrace.Inject(ctx, req)
fmt.Printf("Sending request...\n")
res, err := client.Do(req)
if err != nil {
panic(err)
}
body, err = ioutil.ReadAll(res.Body)
res.Body.Close()
trace.CurrentSpan(ctx).SetStatus(codes.OK)
return err
})
if err != nil {
panic(err)
}
fmt.Printf("Response Received: %s\n\n\n", body)
fmt.Printf("Waiting for few seconds to export spans ...\n\n")
time.Sleep(10 * time.Second)
fmt.Println("Check traces on Stackdriver Trace")
}