Documentation
¶
Index ¶
- func HTTPHandler(handler http.HandlerFunc, operation string) http.HandlerFunc
- func HTTPMiddleware(handler http.Handler, operation string) http.Handler
- func InitTracer(ctx context.Context, cfg Config) (*sdktrace.TracerProvider, error)
- func InitTracerFromYamlConfig(ctx context.Context, config string) (*sdktrace.TracerProvider, error)
- func NewTransport(base http.RoundTripper) http.RoundTripper
- type Config
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTTPHandler ¶
func HTTPHandler(handler http.HandlerFunc, operation string) http.HandlerFunc
HTTPHandler wraps an HTTP handler function with OpenTelemetry tracing It automatically creates spans for incoming HTTP requests and propagates trace context
func HTTPMiddleware ¶
HTTPMiddleware wraps an HTTP handler with OpenTelemetry tracing It automatically creates spans for incoming HTTP requests and propagates trace context
Example ¶
ExampleHTTPMiddleware demonstrates how to use HTTP middleware for tracing
package main
import (
"net/http"
"github.com/meshery/meshkit/tracing"
)
func main() {
// Your existing HTTP handler
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello, World!"))
})
// Wrap with tracing middleware
tracedHandler := tracing.HTTPMiddleware(handler, "my-api")
// Use the wrapped handler with your server
_ = tracedHandler // In practice, use with http.ListenAndServe or your router
}
func InitTracer ¶
InitTracer initializes and configures the global OpenTelemetry trace provider It sets up OTLP gRPC exporter, resource attributes, and W3C trace context propagation
Example ¶
ExampleInitTracer demonstrates how to initialize OpenTelemetry tracing
package main
import (
"context"
"fmt"
"log"
"github.com/meshery/meshkit/tracing"
)
func main() {
ctx := context.Background()
// Configure tracer
cfg := tracing.Config{
ServiceName: "meshery-server",
ServiceVersion: "v0.6.0",
Environment: "development",
Endpoint: "localhost:4317",
Insecure: true, // Use true for local development, false for production
}
// Initialize tracer
tp, err := tracing.InitTracer(ctx, cfg)
if err != nil {
log.Fatalf("Failed to initialize tracer: %v", err)
}
// Ensure proper shutdown
defer func() {
if err := tp.Shutdown(ctx); err != nil {
log.Printf("Error shutting down tracer: %v", err)
}
}()
fmt.Println("Tracer initialized successfully")
}
Output: Tracer initialized successfully
func InitTracerFromYamlConfig ¶ added in v0.8.57
func NewTransport ¶
func NewTransport(base http.RoundTripper) http.RoundTripper
NewTransport creates an HTTP transport instrumented with OpenTelemetry Use this when making HTTP client requests to propagate trace context
Example ¶
ExampleNewTransport demonstrates how to use instrumented HTTP client
package main
import (
"net/http"
"github.com/meshery/meshkit/tracing"
)
func main() {
// Create HTTP client with tracing
client := &http.Client{
Transport: tracing.NewTransport(nil),
}
// Use the client as normal - trace context will be automatically propagated
_, _ = client.Get("http://example.com")
}
Types ¶
type Config ¶
type Config struct {
// ServiceName is the name of the service being traced
ServiceName string `yaml:"service_name" json:"service_name"`
// ServiceVersion is the version of the service
ServiceVersion string `yaml:"service_version" json:"service_version"`
// Environment is the deployment environment (e.g., "production", "staging", "development")
Environment string `yaml:"environment" json:"environment"`
// Endpoint is the OTLP collector endpoint (e.g., "localhost:4317")
Endpoint string `yaml:"endpoint" json:"endpoint"`
// Insecure determines whether to use an insecure connection (no TLS)
Insecure bool `yaml:"insecure" json:"insecure"`
}
Config holds the configuration parameters for tracing