-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add benchmarks and use stdout tracer in examples
Signed-off-by: yakumioto <[email protected]>
- Loading branch information
Showing
6 changed files
with
194 additions
and
43 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (c) 2024 yakumioto <[email protected]> | ||
* All rights reserved. | ||
*/ | ||
|
||
package otelslog | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"log/slog" | ||
"testing" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
) | ||
|
||
func setUpBenchmarkTracer() { | ||
exporter := tracetest.NewInMemoryExporter() | ||
traceProvider := trace.NewTracerProvider(trace.WithSyncer(exporter)) | ||
otel.SetTracerProvider(traceProvider) | ||
} | ||
|
||
func BenchmarkTextSlog(b *testing.B) { | ||
buf := bytes.NewBuffer(nil) | ||
slog.SetDefault(slog.New( | ||
slog.NewTextHandler(buf, nil), | ||
), | ||
) | ||
setUpBenchmarkTracer() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
tracer := otel.Tracer("benchmark") | ||
_, span := tracer.Start(ctx, "benchmark") | ||
defer span.End() | ||
slog.InfoContext(ctx, "hello, world") | ||
} | ||
} | ||
|
||
func BenchmarkJSONSlog(b *testing.B) { | ||
buf := bytes.NewBuffer(nil) | ||
slog.SetDefault(slog.New( | ||
slog.NewJSONHandler(buf, nil), | ||
), | ||
) | ||
|
||
setUpBenchmarkTracer() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
tracer := otel.Tracer("benchmark") | ||
_, span := tracer.Start(ctx, "benchmark") | ||
defer span.End() | ||
slog.InfoContext(ctx, "hello, world") | ||
} | ||
} | ||
|
||
func BenchmarkJSONOtelSlogWithAttr(b *testing.B) { | ||
buf := bytes.NewBuffer(nil) | ||
slog.SetDefault(slog.New( | ||
NewHandler( | ||
slog.NewJSONHandler(buf, nil), | ||
), | ||
)) | ||
setUpBenchmarkTracer() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
span := NewSpanContext("span") | ||
defer span.End() | ||
slog.InfoContext(ctx, "hello, world", "trace", span) | ||
} | ||
} | ||
func BenchmarkTextOtelSlogWithAttr(b *testing.B) { | ||
buf := bytes.NewBuffer(nil) | ||
slog.SetDefault(slog.New( | ||
NewHandler( | ||
slog.NewTextHandler(buf, nil), | ||
), | ||
)) | ||
setUpBenchmarkTracer() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
span := NewSpanContext("span") | ||
defer span.End() | ||
slog.InfoContext(ctx, "hello, world", "trace", span) | ||
} | ||
} | ||
func BenchmarkJSONOtelSlogWithContext(b *testing.B) { | ||
buf := bytes.NewBuffer(nil) | ||
slog.SetDefault(slog.New( | ||
NewHandler( | ||
slog.NewJSONHandler(buf, nil), | ||
), | ||
)) | ||
setUpBenchmarkTracer() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
spanCtx := NewMustSpanContextWithContext(ctx, "span") | ||
defer spanCtx.Done() | ||
slog.InfoContext(spanCtx, "hello, world") | ||
} | ||
} | ||
func BenchmarkTextOtelSlogWithContext(b *testing.B) { | ||
buf := bytes.NewBuffer(nil) | ||
slog.SetDefault(slog.New( | ||
NewHandler( | ||
slog.NewTextHandler(buf, nil), | ||
), | ||
)) | ||
setUpBenchmarkTracer() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
spanCtx := NewMustSpanContextWithContext(ctx, "span") | ||
defer spanCtx.Done() | ||
slog.InfoContext(spanCtx, "hello, world") | ||
} | ||
} |
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