generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca5e074
commit 0ae7996
Showing
6 changed files
with
115 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,71 @@ | ||
package otelroundtripper | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
"log" | ||
"math/rand" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" | ||
"go.opentelemetry.io/otel/metric/global" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.10.0" | ||
) | ||
|
||
func InstallExportPipeline(ctx context.Context) func() { | ||
// Print with a JSON encoder that indents with two spaces. | ||
enc := json.NewEncoder(os.Stdout) | ||
enc.SetIndent("", " ") | ||
exporter, err := stdoutmetric.New(stdoutmetric.WithEncoder(enc)) | ||
if err != nil { | ||
log.Fatalf("creating stdoutmetric exporter: %v", err) | ||
} | ||
|
||
// Register the exporter with an SDK via a periodic reader. | ||
sdk := metric.NewMeterProvider( | ||
metric.WithReader(metric.NewPeriodicReader(exporter)), | ||
) | ||
|
||
global.SetMeterProvider(sdk) | ||
|
||
return func() { | ||
if err := sdk.Shutdown(ctx); err != nil { | ||
log.Fatalf("stopping sdk: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func Example() { | ||
ctx := context.Background() | ||
|
||
// Registers a meter Provider globally. | ||
cleanup := InstallExportPipeline(ctx) | ||
defer cleanup() | ||
|
||
client := http.Client{ | ||
Transport: New( | ||
WithMeter(global.MeterProvider().Meter("otel-round-tripper")), | ||
WithAttributes( | ||
semconv.ServiceNameKey.String("otel-round-tripper"), | ||
), | ||
), | ||
} | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
for i := 0; i < 10; i++ { | ||
// Add a random sleep duration so you will see the metrics in the console | ||
url := "https://httpstat.us/200?sleep=" + strconv.Itoa(rand.Intn(1000)+1000) //nolint:gosec | ||
|
||
log.Printf("GET: %s", url) | ||
response, err := client.Get(url) | ||
if err != nil { | ||
log.Panicf("cannot perform http request: %v", err) | ||
} | ||
|
||
_ = response.Body.Close() | ||
} | ||
} | ||
//import ( | ||
// "context" | ||
// "encoding/json" | ||
// "go.opentelemetry.io/otel/sdk/metric" | ||
// "log" | ||
// "math/rand" | ||
// "net/http" | ||
// "os" | ||
// "strconv" | ||
// "time" | ||
// | ||
// "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" | ||
// "go.opentelemetry.io/otel/metric/global" | ||
// semconv "go.opentelemetry.io/otel/semconv/v1.18.0" | ||
//) | ||
// | ||
//func InstallExportPipeline(ctx context.Context) func() { | ||
// // Print with a JSON encoder that indents with two spaces. | ||
// enc := json.NewEncoder(os.Stdout) | ||
// enc.SetIndent("", " ") | ||
// exporter, err := stdoutmetric.New(stdoutmetric.WithEncoder(enc)) | ||
// if err != nil { | ||
// log.Fatalf("creating stdoutmetric exporter: %v", err) | ||
// } | ||
// | ||
// // Register the exporter with an SDK via a periodic reader. | ||
// sdk := metric.NewMeterProvider( | ||
// metric.WithReader(metric.NewPeriodicReader(exporter)), | ||
// ) | ||
// | ||
// global.SetMeterProvider(sdk) | ||
// | ||
// return func() { | ||
// if err := sdk.Shutdown(ctx); err != nil { | ||
// log.Fatalf("stopping sdk: %v", err) | ||
// } | ||
// } | ||
//} | ||
// | ||
//func Example() { | ||
// ctx := context.Background() | ||
// | ||
// // Registers a meter Provider globally. | ||
// cleanup := InstallExportPipeline(ctx) | ||
// defer cleanup() | ||
// | ||
// client := http.Client{ | ||
// Transport: New( | ||
// WithMeter(global.MeterProvider().Meter("otel-round-tripper")), | ||
// WithAttributes( | ||
// semconv.ServiceNameKey.String("otel-round-tripper"), | ||
// ), | ||
// ), | ||
// } | ||
// | ||
// rand.Seed(time.Now().UnixNano()) | ||
// for i := 0; i < 10; i++ { | ||
// // Add a random sleep duration so that we will see the metrics in the console | ||
// url := "https://httpstat.us/200?sleep=" + strconv.Itoa(rand.Intn(1000)+1000) //nolint:gosec | ||
// | ||
// log.Printf("GET: %s", url) | ||
// response, err := client.Get(url) | ||
// if err != nil { | ||
// log.Panicf("cannot perform http request: %v", err) | ||
// } | ||
// | ||
// _ = response.Body.Close() | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
module github.com/NdoleStudio/go-otelroundtripper | ||
|
||
go 1.17 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.8.1 | ||
go.opentelemetry.io/otel v1.11.2 | ||
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0 | ||
go.opentelemetry.io/otel/metric v0.34.0 | ||
go.opentelemetry.io/otel/sdk/metric v0.34.0 | ||
github.com/stretchr/testify v1.8.2 | ||
go.opentelemetry.io/otel v1.14.0 | ||
go.opentelemetry.io/otel/metric v0.37.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
go.opentelemetry.io/otel/sdk v1.11.2 // indirect | ||
go.opentelemetry.io/otel/trace v1.11.2 // indirect | ||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect | ||
go.opentelemetry.io/otel/trace v1.14.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters