### Basic HttpRouter Usage Example Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/julienschmidt/httprouter/README.md Demonstrates setting up a basic HTTP router with GET routes for the root path and a path with a named parameter. Includes handler functions for each route and starts the HTTP server. ```go package main import ( "fmt" "net/http" "log" "github.com/julienschmidt/httprouter" ) func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "Welcome!\n") } func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) } func main() { router := httprouter.New() router.GET("/", Index) router.GET("/hello/:name", Hello) log.Fatal(http.ListenAndServe(":8080", router)) } ``` -------------------------------- ### Basic Go-Chi Router Setup Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/go-chi/chi/v5/README.md A simple example of initializing a Go-Chi router with basic middleware and a single GET route. ```go package main import ( "net/http" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) func main() { r := chi.NewRouter() r.Use(middleware.Logger) r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("welcome")) }) http.ListenAndServe(":3000", r) } ``` -------------------------------- ### Install Objx Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/stretchr/objx/README.md Use `go get` to install the Objx package. ```bash go get github.com/stretchr/objx ``` -------------------------------- ### Install Afero Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/spf13/afero/README.md Install the Afero library using go get. ```bash go get github.com/spf13/afero ``` -------------------------------- ### Install go-str2duration Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/xhit/go-str2duration/v2/README.md Use go get to download and install the package. ```bash go get github.com/xhit/go-str2duration/v2 ``` -------------------------------- ### Consul API Client Example Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/hashicorp/consul/api/README.md Example demonstrating how to create a Consul client, interact with the KV API to put and get a key-value pair. Ensure Consul is installed and running. ```go package main import ( "fmt" capi "github.com/hashicorp/consul/api" ) func main() { // Get a new client client, err := capi.NewClient(capi.DefaultConfig()) if err != nil { panic(err) } // Get a handle to the KV API kv := client.KV() // PUT a new KV pair p := &capi.KVPair{Key: "REDIS_MAXCLIENTS", Value: []byte("1000")} _, err = kv.Put(p, nil) if err != nil { panic(err) } // Lookup the pair pair, _, err := kv.Get("REDIS_MAXCLIENTS", nil) if err != nil { panic(err) } fmt.Printf("KV: %v %s\n", pair.Key, pair.Value) } ``` -------------------------------- ### Install ULID Library Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/oklog/ulid/README.md Use 'go get' to install the ULID library. ```shell go get github.com/oklog/ulid ``` -------------------------------- ### Install go-codec Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/hashicorp/go-msgpack/codec/README.md Install the go-codec library using the go get command. ```bash go get github.com/ugorji/go/codec ``` -------------------------------- ### Run MinIO Go Example Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/minio/minio-go/v7/README.md Commands to initialize a Go module, get dependencies, and run the FileUploader example. ```sh go mod init example/FileUploader go get github.com/minio/minio-go/v7 go get github.com/minio/minio-go/v7/pkg/credentials go run FileUploader.go ``` -------------------------------- ### Install go-version Library Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/hashicorp/go-version/README.md Install the go-version library using the standard Go get command. This is the initial step before using the library in your project. ```bash $ go get github.com/hashicorp/go-version ``` -------------------------------- ### Install etcd/clientv3 Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.etcd.io/etcd/client/v3/README.md Install the official Go etcd client using go get. ```bash go get go.etcd.io/etcd/client/v3 ``` -------------------------------- ### Install Kingpin Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/alecthomas/kingpin/v2/README.md Install the Kingpin library using the go get command. ```bash $ go get github.com/alecthomas/kingpin/v2 ``` -------------------------------- ### Install go-colorable Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/mattn/go-colorable/README.md Install the go-colorable package using the go get command. ```bash $ go get github.com/mattn/go-colorable ``` -------------------------------- ### Install bitpack Library Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/parquet-go/bitpack/README.md Use 'go get' to install the bitpack library. This command fetches and installs the library and its dependencies. ```bash go get github.com/parquet-go/bitpack ``` -------------------------------- ### Install json-iterator/go Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/json-iterator/go/README.md Use the go get command to install the library. ```bash go get github.com/json-iterator/go ``` -------------------------------- ### Install OpenCensus Go Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.opencensus.io/README.md Use `go get` to install the OpenCensus Go library. Vendoring or a dependency management tool is recommended. ```bash $ go get -u go.opencensus.io ``` -------------------------------- ### Install Swift Go Library Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/ncw/swift/README.md Use 'go get' to install the Swift library for your Go projects. ```go go get github.com/ncw/swift ``` -------------------------------- ### Install MinIO Go Client SDK Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/minio/minio-go/v7/README.md Use 'go get' to download and install the MinIO Go client SDK for your project. ```sh go get github.com/minio/minio-go/v7 ``` -------------------------------- ### Install xid Package Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/rs/xid/README.md Use 'go get' to install the xid package. This command fetches and installs the package and its dependencies. ```go go get github.com/rs/xid ``` -------------------------------- ### Install Golang Color Library Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/fatih/color/README.md Use 'go get' to install the color library. This command fetches and installs the specified package. ```go go get github.com/fatih/color ``` -------------------------------- ### Install go.yaml.in/yaml/v3 Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.yaml.in/yaml/v3/README.md Use 'go get' to install the package. The import path is go.yaml.in/yaml/v3. ```bash go get go.yaml.in/yaml/v3 ``` -------------------------------- ### Complete go-grpc-channelz Integration Example Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/rantav/go-grpc-channelz/README.md A comprehensive example demonstrating the integration of go-grpc-channelz, including gRPC server setup, channelz service and handler registration, and HTTP listener launch. ```go import ( "google.golang.org/grpc" channelzservice "google.golang.org/grpc/channelz/service" channelz "github.com/rantav/go-grpc-channelz" ) grpcServer := grpc.NewServer() // Register the channelz handler http.Handle("/", channelz.CreateHandler("/foo", grpcBindAddress)) // Register the channelz gRPC service to grpcServer so that we can query it for this service. channelzservice.RegisterChannelzServiceToServer(grpcServer) // Listen and serve HTTP for the default serve mux adminListener, err := net.Listen("tcp", ":8081") if err != nil { log.Fatal(err) } go http.Serve(adminListener, nil) ``` -------------------------------- ### Install koanf Core and Providers/Parsers Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/knadh/koanf/v2/README.md Installs the core koanf library and specific providers and parsers using go get. Ensure to install only the modules you need. ```shell # Install the core. go get -u github.com/knadh/koanf/v2 # Install the necessary Provider(s). # Available: file, env/v2, posflag, basicflag, confmap, rawbytes, # structs, fs, s3, appconfig/v2, consul/v2, etcd/v2, vault/v2, parameterstore/v2 # eg: go get -u github.com/knadh/koanf/providers/s3 # eg: go get -u github.com/knadh/koanf/providers/consul/v2 go get -u github.com/knadh/koanf/providers/file # Install the necessary Parser(s). # Available: toml, toml/v2, json, yaml, huml, dotenv, hcl, hjson, nestedtext # go get -u github.com/knadh/koanf/parsers/$parser go get -u github.com/knadh/koanf/parsers/toml ``` -------------------------------- ### Install copystructure Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/mitchellh/copystructure/README.md Use `go get` to install the copystructure library for use in your Go projects. ```bash $ go get github.com/mitchellh/copystructure ``` -------------------------------- ### Install go-redis/v8 Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/go-redis/redis/v8/README.md Install the go-redis/v8 library using go get. Ensure you have modules support enabled in your Go project. ```shell go get github.com/go-redis/redis/v8 ``` -------------------------------- ### HA Samples Configuration Source: https://github.com/cortexproject/cortex/blob/master/docs/configuration/config-file-reference.md Configure handling of samples from HA Prometheus setups, including enabling for all users and tracking specific labels. ```yaml # Flag to enable, for all users, handling of samples with external labels # identifying replicas in an HA Prometheus setup. # CLI flag: -distributor.ha-tracker.enable-for-all-users [accept_ha_samples: | default = false] # [Experimental] Flag to enable handling of samples with mixed external labels # identifying replicas in an HA Prometheus setup. Supported only if # -distributor.ha-tracker.enable-for-all-users is true. # CLI flag: -experimental.distributor.ha-tracker.mixed-ha-samples [accept_mixed_ha_samples: | default = false] # Prometheus label to look for in samples to identify a Prometheus HA cluster. # CLI flag: -distributor.ha-tracker.cluster [ha_cluster_label: | default = "cluster"] # Prometheus label to look for in samples to identify a Prometheus HA replica. # CLI flag: -distributor.ha-tracker.replica [ha_replica_label: | default = "__replica__"] ``` -------------------------------- ### Setup and Run Demo Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/axiomhq/hyperloglog/Contributing.md Clone the repository, navigate to the demo directory, and run the provided Go demo application. ```bash git clone https://github.com/axiomhq/hyperloglog.git cd hyperloglog cd demo go run hyperloglog_demo.go ``` -------------------------------- ### Build All Examples Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/minio/minio-go/v7/CLAUDE.md Build all example programs included in the repository. ```bash make examples ``` -------------------------------- ### Quickstart: Basic Redis Operations Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/go-redis/redis/v8/README.md Demonstrates how to connect to a Redis server, set a key-value pair, and retrieve its value. Handles cases where a key does not exist. ```go import ( "context" "github.com/go-redis/redis/v8" "fmt" ) var ctx = context.Background() func ExampleClient() { rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) err := rdb.Set(ctx, "key", "value", 0).Err() if err != nil { panic(err) } val, err := rdb.Get(ctx, "key").Result() if err != nil { panic(err) } fmt.Println("key", val) val2, err := rdb.Get(ctx, "key2").Result() if err == redis.Nil { fmt.Println("key2 does not exist") } else if err != nil { panic(err) } else { fmt.Println("key2", val2) } // Output: key value // key2 does not exist } ``` -------------------------------- ### Install gobwas/glob Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/gobwas/glob/readme.md Use 'go get' to install the library. ```shell go get github.com/gobwas/glob ``` -------------------------------- ### Build Specific Example Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/minio/minio-go/v7/CLAUDE.md Build a specific example program by navigating to its directory and running go build. ```bash cd examples/s3 && go build -mod=mod putobject.go ``` -------------------------------- ### Authenticate and List Containers with Swift Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/ncw/swift/README.md Example demonstrating how to create a Swift connection, authenticate, and list all containers. Ensure your credentials and authentication URL are correctly set. ```go import "github.com/ncw/swift" // Create a connection c := swift.Connection{ UserName: "user", ApiKey: "key", AuthUrl: "auth_url", Domain: "domain", // Name of the domain (v3 auth only) Tenant: "tenant", // Name of the tenant (v2 auth only) } // Authenticate err := c.Authenticate() if err != nil { panic(err) } // List all the containers containers, err := c.ContainerNames(nil) fmt.Println(containers) // etc... ``` -------------------------------- ### Install gobreaker Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/sony/gobreaker/README.md Use 'go get' to install the gobreaker library. ```bash go get github.com/sony/gobreaker ``` -------------------------------- ### Full Example of a Mux-Based Server in Go Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/gorilla/mux/README.md Provides a complete, runnable example of a basic HTTP server using the Gorilla Mux router. This snippet demonstrates setting up routes and starting the server. ```go package main import ( "net/http" "log" "github.com/gorilla/mux" ) func YourHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Gorilla!\n")) } func main() { r := mux.NewRouter() // Routes consist of a path and a handler function. r.HandleFunc("/", YourHandler) // Bind to a port and pass our router in log.Fatal(http.ListenAndServe(":8000", r)) } ``` -------------------------------- ### Install Telebot Source: https://github.com/cortexproject/cortex/blob/master/vendor/gopkg.in/telebot.v3/README.md Install the Telebot v3 package using go get. ```bash go get -u gopkg.in/telebot.v3 ``` -------------------------------- ### Install otgrpc package Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/opentracing-contrib/go-grpc/README.md Use go get to install the otgrpc package. ```shell go get github.com/opentracing-contrib/go-grpc ``` -------------------------------- ### Install Go Tools and Run Tests Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.opencensus.io/CONTRIBUTING.md Install necessary development tools for the project, which only needs to be done once. Subsequently, run the project's test suite using the `make` command. ```bash $ make install-tools # Only first time. $ make ``` -------------------------------- ### RPC Client Setup Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/hashicorp/go-msgpack/codec/README.md Set up an RPC client to connect to a server and create a new client with a specified codec. ```go conn, err = net.Dial("tcp", "localhost:5555") rpcCodec := codec.GoRpc.ClientCodec(conn, h) //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h) client := rpc.NewClientWithCodec(rpcCodec) ``` -------------------------------- ### Run the Promrus Example Program Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/weaveworks/promrus/README.md Commands to set up dependencies and run the Go program that uses the promrus hook. ```bash $ go get -u github.com/golang/dep/cmd/dep $ dep ensure $ go run main.go ``` -------------------------------- ### Get Label Names Source: https://github.com/cortexproject/cortex/blob/master/docs/api/_index.md Get label names of ingested series. Starting from release v1.18.0, Cortex by default honors the `start` and `end` request parameters. Requires authentication. ```APIDOC ## Get Label Names ### Description Get label names of ingested series. Starting from release v1.18.0, Cortex by default honors the `start` and `end` request parameters and fetches label names from either ingester, store gateway or both. The special case is that if `start` param is not specified, Cortex currently fetches labels from data stored in the ingesters. ### Method GET, POST ### Endpoint /api/v1/labels ### Notes Requires authentication. ``` -------------------------------- ### Install Latest Jaeger Client via go get Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/uber/jaeger-client-go/README.md Install the latest version of the Jaeger client from master using 'go get'. Be aware of potential build errors due to incompatible dependencies; 'make install' may help resolve them. ```shell go get -u github.com/uber/jaeger-client-go/ cd $GOPATH/src/github.com/uber/jaeger-client-go/ git submodule update --init --recursive make install ``` -------------------------------- ### Get Label Values Source: https://github.com/cortexproject/cortex/blob/master/docs/api/_index.md Get label values for a given label name. Starting from release v1.18.0, Cortex by default honors the `start` and `end` request parameters. Requires authentication. ```APIDOC ## Get Label Values ### Description Get label values for a given label name. Starting from release v1.18.0, Cortex by default honors the `start` and `end` request parameters and fetches label values from either ingester, store gateway or both. The special case is that if `start` param is not specified, Cortex currently fetches label values from data stored in the ingesters. ### Method GET ### Endpoint /api/v1/label/{name}/values ### Parameters #### Path Parameters - **name** (string) - Required - The name of the label for which to retrieve values. ### Notes Requires authentication. ``` -------------------------------- ### RPC Server Setup Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/hashicorp/go-msgpack/codec/README.md Set up an RPC server that accepts connections and serves RPC requests using a codec. ```go go func() { for { conn, err := listener.Accept() rpcCodec := codec.GoRpc.ServerCodec(conn, h) //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h) rpc.ServeCodec(rpcCodec) } }() ``` -------------------------------- ### Initialize MinIO Backend Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/spf13/afero/README.md Set up an Afero filesystem for MinIO object storage, which is S3-compatible. Connects to a specified endpoint and bucket. ```go import "github.com/cpyun/afero-minio" minioFs := miniofs.NewMinioFs(ctx, "minio://endpoint/bucket") ``` -------------------------------- ### Install multierr Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.uber.org/multierr/README.md Use go get to install the latest version of the multierr package. ```bash go get -u go.uber.org/multierr@latest ``` -------------------------------- ### Install IAM Go Client Library Source: https://github.com/cortexproject/cortex/blob/master/vendor/cloud.google.com/go/iam/README.md Use this command to install the IAM Go client library. Ensure you have Go installed and configured. ```bash go get cloud.google.com/go/iam ``` -------------------------------- ### Install go.uber.org/atomic Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.uber.org/atomic/README.md Use 'go get' to install the latest version of the atomic package. ```shell $ go get -u go.uber.org/atomic@v1 ``` -------------------------------- ### Install and Run Local Go Doc Site Source: https://github.com/cortexproject/cortex/blob/master/vendor/go.opentelemetry.io/otel/CONTRIBUTING.md Installs and runs a local Go documentation site. Ensure you have Go installed and configured. ```sh go install golang.org/x/pkgsite/cmd/pkgsite@latest pkgsite ``` -------------------------------- ### Install OTLP Prometheus Translator Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/prometheus/otlptranslator/README.md Install the library using the go get command. ```bash go get github.com/prometheus/otlptranslator ``` -------------------------------- ### Install lz4 package Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/pierrec/lz4/v4/README.md Install the lz4 package using the go get command. ```bash go get github.com/pierrec/lz4/v4 ``` -------------------------------- ### Install Backoff Package Source: https://github.com/cortexproject/cortex/blob/master/vendor/github.com/jpillora/backoff/README.md Use 'go get' to install the backoff package. This command fetches and installs the specified package and its dependencies. ```bash go get -v github.com/jpillora/backoff ```