logsinst

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 13 Imported by: 0

README

Logs Instrumentation

Simple example

func main() {
	ctx := context.Background()

	// Create resource.
	res, err := newResource()
	if err != nil {
		panic(err)
	}

	// Create a logger provider.
	loggerProvider, err := newLoggerProvider(ctx, res)
	if err != nil {
		panic(err)
	}

	// Create a hook using Log Bridging.
	hook := otellogrus.NewHook("logNameBla",
		otellogrus.WithLoggerProvider(loggerProvider))
	logrus.AddHook(hook)
	logrus.SetFormatter(&logrus.JSONFormatter{})
	//hook.Fire(logrus.NewEntry(logrusLogger))

	// Set the LogBridge as the global logger provider.
	global.SetLoggerProvider(loggerProvider)

	// Use the Logrus logger as usual.
	logrus.Info("WKWKWKWKWKWK")
}

func newResource() (*resource.Resource, error) {
	return resource.Merge(resource.Default(),
		resource.NewWithAttributes(semconv.SchemaURL,
			semconv.ServiceName("my-bolobolo"),
			semconv.ServiceVersion("0.1.0"),
		))
}

func newLoggerProvider(ctx context.Context, res *resource.Resource) (*log.LoggerProvider, error) {
	opts := []otlploghttp.Option{
		otlploghttp.WithEndpoint("127.0.0.1:4318"),
		otlploghttp.WithInsecure(),
		//otlploghttp.WithURLPath(),
	}

	exporter, err := otlploghttp.New(ctx, opts...)
	if err != nil {
		return nil, err
	}
	processor := log.NewBatchProcessor(exporter)
	provider := log.NewLoggerProvider(
		log.WithResource(res),
		log.WithProcessor(processor),
	)
	return provider, nil
}

Enabling Datadog Agent

docker run --cgroupns host \
    --pid host \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    -v /proc/:/host/proc/:ro \
    -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
    -p 127.0.0.1:8226:8226/tcp \
    -e DD_API_KEY=< YOUR API KEY HERE> \
    -e DD_APM_ENABLED=true \
    -e DD_SITE=us5.datadoghq.com \
    -e DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_HTTP_ENDPOINT=0.0.0.0:4318 \
    -e OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 \
    -p 4318:4318 \
    -e DD_LOGS_ENABLED=true \
    -e DD_OTLP_CONFIG_LOGS_ENABLED=true \
    -e DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL=true \
    -p 8125:8125/udp \
    gcr.io/datadoghq/agent:latest

Reference

  1. https://opentelemetry.io/docs/languages/go/instrumentation/#log-bridge (open telemetry instrumentation)
  2. https://opentelemetry.io/docs/specs/otel/logs/
  3. https://opentelemetry.io/docs/concepts/signals/logs/#log-record-exporter
  4. https://docs.datadoghq.com/opentelemetry/interoperability/otlp_ingest_in_the_agent/?tab=docker (datadog)
  5. https://www.datadoghq.com/blog/agent-otlp-log-ingestion/ (datadog)
  6. https://docs.datadoghq.com/agent/logs/?tab=tcpudp (datadog)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartAgent

func StartAgent(c Config)

StartAgent starts an opentelemetry agent.

func StopAgent

func StopAgent()

StopAgent shuts down the logger provider and all its processor in the order they were registered.

Types

type Config

type Config struct {
	// LoggerName represents the logger name while hook initiation.
	LoggerName  string `json:",default=go-one-logger"`
	ServiceName string `json:",default=go-one-service"`
	Endpoint    string `json:",optional"`
	Sampler     string `json:",default=1.0.0"`
	Batcher     string `json:",default=jaeger,options=jaeger|zipkin|otlpgrpc|otlphttp|file"`
	// OtlpHeaders represents the headers for OTLP gRPC or HTTP transport.
	// For example:
	//  uptrace-dsn: 'http://project2_secret_token@localhost:14317/2'
	OtlpHeaders map[string]string `json:",optional"`
	// OtlpHttpPath represents the path for OTLP HTTP transport. Default: /v1/logs
	OtlpHttpPath string `json:",optional"`
	// OtlpHttpSecure represents the scheme to use for OTLP HTTP transport.
	OtlpHttpSecure bool `json:",optional"`
	// Disabled indicates whether StartAgent starts the agent.
	Disabled bool `json:",optional"`
	// LoggerType indicates which log lib is used.
	LoggerType string `json:",default=logrus,options=logrus"`
}

A Config is an logsinst config.

Jump to

Keyboard shortcuts

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