utils

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package utils provides utility functions for the application. It mostly contains utils for the runtime and network.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPServerStatus

func HTTPServerStatus(code int) (codes.Code, string)

HTTPServerStatus returns a span status code and message for an HTTP status code value returned by a server. Status codes in the 400-499 range are not returned as errors.

func ServerRequestMetrics

func ServerRequestMetrics(server string, req *http.Request) []attribute.KeyValue

ServerRequestMetrics returns metric attributes for an HTTP request received by a server.

The server must be the primary server name if it is known. For example this would be the ServerName directive (https://httpd.apache.org/docs/2.4/mod/core.html#servername) for an Apache server, and the server_name directive (http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name) for an nginx server. More generically, the primary server name would be the host header value that matches the default virtual host of an HTTP server. It should include the host identifier and if a port is used to route to the server that port identifier should be included as an appropriate port suffix.

If the primary server name is not known, server should be an empty string. The req Host will be used to determine the server instead.

The following attributes are always returned: "http.method", "http.scheme", "net.host.name". The following attributes are returned if they related values are defined in req: "net.host.port".

Types

type Caller

type Caller struct {
	// FilePath is the absolute file path of the caller
	FilePath string
	// LineNumber is the line number of the caller
	LineNumber int
	// FunctionName is the name of the function of the caller
	FunctionName string
	// The “namespace” within which code.function
	Namespace string
}

Caller represents the caller information of a function.

func GetCallerName

func GetCallerName(numofSkippedFrames int) Caller

GetCallerName retrieves caller information from the call stack.

This function obtains the caller's function name, file path, and line number by inspecting the runtime call stack. The number of frames to skip is specified by numofSkippedFrames, allowing it to identify a specific caller level. If the function details are available, they are extracted; otherwise, a default empty string is used for the function name.

Returns a Caller struct containing:

  • FilePath: the absolute path of the file where the caller is located.
  • LineNumber: the line number within the file.
  • FunctionName: the name of the caller function.

func (Caller) LogAttributes

func (c Caller) LogAttributes() []slog.Attr

LogAttributes returns the caller attributes in a structured slog format.

func (Caller) MarshalJSON

func (c Caller) MarshalJSON() ([]byte, error)

Custom MarshalJSON method to dynamically set JSON field names

func (Caller) SpanAttributes

func (c Caller) SpanAttributes() []otel_attribute.KeyValue

SpanAttributes returns the caller attributes in a structured OpenTelemetry format.

func (Caller) String

func (c Caller) String() string

String returns a string representation of the Caller struct.

Jump to

Keyboard shortcuts

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