otelinit

package module
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 3 Imported by: 0

README

otelinit

import "github.com/induzo/gocom/monitoring/otelinit/v2"

This package allows you to init and enable tracing in your app

Index

func InitTraceProvider

func InitTraceProvider(ctx context.Context, serviceName string, options ...trace.ProviderOptionFunc) (func() error, error)
Example (Collector)

Initialize a otel trace provider with a collector

{
	ctx := context.Background()

	shutdown, err := otelinit.InitTraceProvider(
		ctx,
		"simple-gohttp",
		trace.WithGRPCTraceExporter(
			ctx,
			otlptracegrpc.WithEndpoint(fmt.Sprintf("%s:%d", "otlp.nr-data.net", 4317)),
			otlptracegrpc.WithHeaders(map[string]string{"api-key": "123"}),
			otlptracegrpc.WithCompressor("gzip"),
		),
	)
	if err != nil {
		log.Println("failed to initialize opentelemetry")

		return
	}

	defer func() {
		if errS := shutdown(); errS != nil {
			log.Println("failed to shutdown")
		}
	}()

	fmt.Println(err)

}
Output
<nil>

Example (Discard Traces)

Initialize a otel trace provider and discard traces useful for dev

{
	ctx := context.Background()

	shutdown, err := otelinit.InitTraceProvider(
		ctx,
		"simple-gohttp",
		trace.WithWriterTraceExporter(io.Discard),
	)
	if err != nil {
		log.Println("failed to initialize opentelemetry")

		return
	}

	defer func() {
		if errS := shutdown(); errS != nil {
			log.Println("failed to shutdown")
		}
	}()

	fmt.Println(err)

}
Output
<nil>

func Start

func Start(ctx context.Context, conf *Config) ([]func(ctx context.Context) error, error)
Example (Collector)

Init and start an otel trace and metric provider with a collector

{
	ctx := context.Background()

	shutdowns, err := otelinit.Start(ctx, &otelinit.Config{
		AppName:       "simple-gohttp",
		Host:          "otlp.nr-data.net",
		Port:          4317,
		APIKey:        "123",
		IsSecure:      true,
		EnableMetrics: true,
	})
	if err != nil {
		log.Println("failed to start opentelemetry")

		return
	}

	for _, s := range shutdowns {
		shutdown := s
		defer func() {
			if errS := shutdown(ctx); errS != nil {
				log.Println("failed to shutdown")
			}
		}()
	}

	fmt.Println(err)

}
Output
<nil>

Example (Collector_trace_only)

Init and start an otel trace provider with a collector only

{
	ctx := context.Background()

	shutdowns, err := otelinit.Start(ctx, &otelinit.Config{
		AppName:       "simple-gohttp",
		Host:          "otlp.nr-data.net",
		Port:          4317,
		APIKey:        "123",
		IsSecure:      true,
		EnableMetrics: false,
	})
	if err != nil {
		log.Println("failed to start opentelemetry")

		return
	}

	for _, s := range shutdowns {
		shutdown := s
		defer func() {
			if errS := shutdown(ctx); errS != nil {
				log.Println("failed to shutdown")
			}
		}()
	}

	fmt.Println(err)

}
Output
<nil>

func StartTrace

func StartTrace(ctx context.Context, conf *Config) (func(ctx context.Context) error, error)
Example (Collector)

Init and start an otel trace provider with a collector

{
	ctx := context.Background()

	shutdown, err := otelinit.StartTrace(ctx, &otelinit.Config{
		AppName:  "simple-gohttp",
		Host:     "otlp.nr-data.net",
		Port:     4317,
		APIKey:   "123",
		IsSecure: true,
	})
	if err != nil {
		log.Println("failed to start opentelemetry")

		return
	}

	defer func() {
		if errS := shutdown(ctx); errS != nil {
			log.Println("failed to shutdown trace")
		}
	}()

	fmt.Println(err)

}
Output
<nil>

type Config

Config allow you to set opentelemetry trace and metric, the variable of EnableMetric only used when Start()

type Config struct {
    AppName       string
    Host          string
    Port          int
    APIKey        string
    IsSecure      bool
    EnableMetrics bool
}

Generated by gomarkdoc

Documentation

Overview

This package allows you to init and enable tracing in your app

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitTraceProvider

func InitTraceProvider(
	ctx context.Context,
	serviceName string,
	options ...trace.ProviderOptionFunc,
) (func() error, error)
Example (Collector)

Initialize a otel trace provider with a collector

ctx := context.Background()

shutdown, err := otelinit.InitTraceProvider(
	ctx,
	"simple-gohttp",
	trace.WithGRPCTraceExporter(
		ctx,
		otlptracegrpc.WithEndpoint(fmt.Sprintf("%s:%d", "otlp.nr-data.net", 4317)),
		otlptracegrpc.WithHeaders(map[string]string{"api-key": "123"}),
		otlptracegrpc.WithCompressor("gzip"),
	),
)
if err != nil {
	log.Println("failed to initialize opentelemetry")

	return
}

defer func() {
	if errS := shutdown(); errS != nil {
		log.Println("failed to shutdown")
	}
}()

fmt.Println(err)
Output:

<nil>
Example (DiscardTraces)

Initialize a otel trace provider and discard traces useful for dev

ctx := context.Background()

shutdown, err := otelinit.InitTraceProvider(
	ctx,
	"simple-gohttp",
	trace.WithWriterTraceExporter(io.Discard),
)
if err != nil {
	log.Println("failed to initialize opentelemetry")

	return
}

defer func() {
	if errS := shutdown(); errS != nil {
		log.Println("failed to shutdown")
	}
}()

fmt.Println(err)
Output:

<nil>

func Start

func Start(ctx context.Context, conf *Config) ([]func(ctx context.Context) error, error)
Example (Collector)

Init and start an otel trace and metric provider with a collector

ctx := context.Background()

shutdowns, err := otelinit.Start(ctx, &otelinit.Config{
	AppName:       "simple-gohttp",
	Host:          "otlp.nr-data.net",
	Port:          4317,
	APIKey:        "123",
	IsSecure:      true,
	EnableMetrics: true,
})
if err != nil {
	log.Println("failed to start opentelemetry")

	return
}

for _, s := range shutdowns {
	shutdown := s
	defer func() {
		if errS := shutdown(ctx); errS != nil {
			log.Println("failed to shutdown")
		}
	}()
}

fmt.Println(err)
Output:

<nil>
Example (Collector_trace_only)

Init and start an otel trace provider with a collector only

ctx := context.Background()

shutdowns, err := otelinit.Start(ctx, &otelinit.Config{
	AppName:       "simple-gohttp",
	Host:          "otlp.nr-data.net",
	Port:          4317,
	APIKey:        "123",
	IsSecure:      true,
	EnableMetrics: false,
})
if err != nil {
	log.Println("failed to start opentelemetry")

	return
}

for _, s := range shutdowns {
	shutdown := s
	defer func() {
		if errS := shutdown(ctx); errS != nil {
			log.Println("failed to shutdown")
		}
	}()
}

fmt.Println(err)
Output:

<nil>

func StartTrace

func StartTrace(ctx context.Context, conf *Config) (func(ctx context.Context) error, error)
Example (Collector)

Init and start an otel trace provider with a collector

ctx := context.Background()

shutdown, err := otelinit.StartTrace(ctx, &otelinit.Config{
	AppName:  "simple-gohttp",
	Host:     "otlp.nr-data.net",
	Port:     4317,
	APIKey:   "123",
	IsSecure: true,
})
if err != nil {
	log.Println("failed to start opentelemetry")

	return
}

defer func() {
	if errS := shutdown(ctx); errS != nil {
		log.Println("failed to shutdown trace")
	}
}()

fmt.Println(err)
Output:

<nil>

Types

type Config

type Config struct {
	AppName       string
	Host          string
	Port          int
	APIKey        string
	IsSecure      bool
	EnableMetrics bool
}

Config allow you to set opentelemetry trace and metric, the variable of EnableMetric only used when Start()

Directories

Path Synopsis
This package allows you to init and enable tracing in your app
This package allows you to init and enable tracing in your app

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL