Skip to content

Instantly share code, notes, and snippets.

@srikanthccv
Created January 27, 2024 15:26
Show Gist options
  • Save srikanthccv/d046e2b677a5c03c29782cc9f8e179f1 to your computer and use it in GitHub Desktop.
Save srikanthccv/d046e2b677a5c03c29782cc9f8e179f1 to your computer and use it in GitHub Desktop.
Test script
package main
import (
"context"
"errors"
"math/rand"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)
// setupOTelSDK bootstraps the OpenTelemetry pipeline.
// If it does not return an error, make sure to call shutdown for proper cleanup.
func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shutdown func(context.Context) error, err error) {
var shutdownFuncs []func(context.Context) error
// shutdown calls cleanup functions registered via shutdownFuncs.
// The errors from the calls are joined.
// Each registered cleanup will be invoked once.
shutdown = func(ctx context.Context) error {
var err error
for _, fn := range shutdownFuncs {
err = errors.Join(err, fn(ctx))
}
shutdownFuncs = nil
return err
}
// handleErr calls shutdown for cleanup and makes sure that all errors are returned.
handleErr := func(inErr error) {
err = errors.Join(inErr, shutdown(ctx))
}
// Set up resource.
res, err := newResource(serviceName, serviceVersion)
if err != nil {
handleErr(err)
return
}
// Set up propagator.
prop := newPropagator()
otel.SetTextMapPropagator(prop)
// Set up trace provider.
tracerProvider, err := newTraceProvider(res)
if err != nil {
handleErr(err)
return
}
shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown)
otel.SetTracerProvider(tracerProvider)
// Set up meter provider.
meterProvider, err := newMeterProvider(res)
if err != nil {
handleErr(err)
return
}
shutdownFuncs = append(shutdownFuncs, meterProvider.Shutdown)
otel.SetMeterProvider(meterProvider)
return
}
func newResource(serviceName, serviceVersion string) (*resource.Resource, error) {
x := resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceName(serviceName),
semconv.ServiceVersion(serviceVersion),
)
return x, nil
}
func newPropagator() propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
)
}
func newTraceProvider(res *resource.Resource) (*trace.TracerProvider, error) {
traceExporter, err := otlptracegrpc.New(context.TODO())
if err != nil {
return nil, err
}
traceProvider := trace.NewTracerProvider(
trace.WithBatcher(traceExporter,
// Default is 5s. Set to 1s for demonstrative purposes.
trace.WithBatchTimeout(10*time.Second)),
trace.WithResource(res),
)
return traceProvider, nil
}
func newMeterProvider(res *resource.Resource) (*metric.MeterProvider, error) {
metricExporter, err := otlpmetricgrpc.New(context.TODO())
if err != nil {
return nil, err
}
meterProvider := metric.NewMeterProvider(
metric.WithResource(res),
metric.WithReader(metric.NewPeriodicReader(metricExporter,
// Default is 1m. Set to 3s for demonstrative purposes.
metric.WithInterval(10*time.Second))),
)
return meterProvider, nil
}
// SELECT quantiles(0.42, 0.69, 0.75, 0.9, 0.95, 0.99)(numberTagMap['recordTime'])
// FROM signoz_traces.signoz_index_v2
// WHERE (numberTagMap['recordTime']) != 0
// SELECT arrayMap(a -> round(a, 2), (quantilesDDSketchMerge(0.01, 0.42, 0.69, 0.75, 0.9, 0.95, 0.99)(sketch)))
// FROM signoz_metrics.exp_hist;
func main() {
shutdown, err := setupOTelSDK(context.Background(), "exp", "0.0.1")
if err != nil {
panic(err)
}
defer func() {
if err := shutdown(context.Background()); err != nil {
panic(err)
}
}()
meterProvider := otel.GetMeterProvider()
meter := meterProvider.Meter("exp")
tracerProvider := otel.GetTracerProvider()
tracer := tracerProvider.Tracer("exp")
hWithBuckets, err := meter.Int64Histogram("exp_hist")
if err != nil {
panic(err)
}
for {
recordTimes := []int{}
recordTimes = append(recordTimes, rand.Intn(1000))
recordTimes = append(recordTimes, rand.Intn(100000))
recordTimes = append(recordTimes, rand.Intn(1000000))
recordTimes = append(recordTimes, rand.Intn(10000000))
recordTimes = append(recordTimes, rand.Intn(100000000))
recordTimes = append(recordTimes, rand.Intn(1000000000))
recordTimes = append(recordTimes, rand.Intn(10000000000))
for _, recordTime := range recordTimes {
hWithBuckets.Record(context.Background(), int64(recordTime))
commonAttrs := []attribute.KeyValue{
attribute.Int("recordTime", recordTime),
}
_, span := tracer.Start(
context.Background(),
"CollectorExporter-Example",
)
span.SetAttributes(commonAttrs...)
span.End()
}
time.Sleep(10 * time.Millisecond)
}
}
@srikanthccv
Copy link
Author

OTEL_EXPORTER_OTLP_INSECURE=true OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION=base2_exponential_bucket_histogram  go run exp_hist.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment