Skip to content

fix: rideshare tweaks #4075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
services:
us-east:
container_name: us-east
ports:
- 5000
environment:
Expand All @@ -12,6 +13,7 @@ services:
build:
context: .
eu-north:
container_name: eu-north
ports:
- 5000
environment:
Expand All @@ -22,6 +24,7 @@ services:
build:
context: .
ap-south:
container_name: ap-south
ports:
- 5000
environment:
Expand All @@ -41,6 +44,8 @@ services:
dockerfile: Dockerfile.load-generator
environment:
- PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
- OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://prometheus:9090/api/v1/otlp/v1/metrics
grafana:
image: grafana/grafana:latest
environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"time"
Expand All @@ -19,6 +20,7 @@ import (
otellogs "github.com/agoda-com/opentelemetry-logs-go"
sdklogs "github.com/agoda-com/opentelemetry-logs-go/sdk/logs"
otelpyroscope "github.com/grafana/otel-profiling-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
mmetric "go.opentelemetry.io/otel/metric"
Expand Down Expand Up @@ -85,6 +87,14 @@ func main() {

rideshare.Log.Print(context.Background(), "started ride-sharing app")

// Register Prometheus metrics handler
http.Handle("/metrics", promhttp.Handler())
go func() {
if err := http.ListenAndServe(":5001", nil); err != nil {
slog.Error("metrics server error", "error", err)
}
}()

http.Handle("/", otelhttp.NewHandler(http.HandlerFunc(index), "IndexHandler"))

http.Handle("/bike", otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ scrape_configs:
static_configs:
- targets:
- host.docker.internal:9099
- job_name: 'rideshare'
static_configs:
- targets: ['us-east:5000', 'eu-north:5000', 'ap-south:5000']
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"fmt"
"math/rand"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -99,7 +98,7 @@ func ReadConfig() Config {
PyroscopeBasicAuthUser: os.Getenv("PYROSCOPE_BASIC_AUTH_USER"),
PyroscopeBasicAuthPassword: os.Getenv("PYROSCOPE_BASIC_AUTH_PASSWORD"),

OTLPUrl: os.Getenv("OTLP_URL"),
OTLPUrl: getOTLPUrl(),
OTLPInsecure: os.Getenv("OTLP_INSECURE") == "1",
OTLPBasicAuthUser: os.Getenv("OTLP_BASIC_AUTH_USER"),
OTLPBasicAuthPassword: os.Getenv("OTLP_BASIC_AUTH_PASSWORD"),
Expand Down Expand Up @@ -137,6 +136,14 @@ func ReadConfig() Config {
return c
}

// getOTLPUrl returns the OTLP URL from environment variables, with OTEL_EXPORTER_OTLP_METRICS_ENDPOINT taking precedence
func getOTLPUrl() string {
if url := os.Getenv("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"); url != "" {
return url
}
return os.Getenv("OTLP_URL")
}

func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
Expand Down Expand Up @@ -253,65 +260,24 @@ type dimension struct {
getNextValue func() string
}

func staticList(input []string) func() string {
return func() string {
i := rand.Intn(len(input))
return input[i]
}
}

func init() {
// Configure Prometheus to use UTF-8 validation for metric names
model.NameEscapingScheme = model.ValueEncodingEscaping
}

func MeterProvider(c Config) (*sdkmetric.MeterProvider, error) {
// Default is 1m. Set to 3s for demonstrative purposes.
interval := 3 * time.Second

// Setup Prometheus metrics
fakeUtf8Metrics := []dimension{
{
label: "a_legacy_label",
getNextValue: staticList([]string{"legacy"}),
},
{
label: "label with space",
getNextValue: staticList([]string{"space"}),
},
{
label: "label with 📈",
getNextValue: staticList([]string{"metrics"}),
},
{
label: "label.with.spaß",
getNextValue: staticList([]string{"this_is_fun"}),
},
{
label: "instance",
getNextValue: staticList([]string{"instance"}),
},
{
label: "job",
getNextValue: staticList([]string{"job"}),
},
{
label: "site",
getNextValue: staticList([]string{"LA-EPI"}),
},
{
label: "room",
getNextValue: staticList([]string{`"Friends Don't Lie"`}),
},
}

dimensions := []string{}
for _, dim := range fakeUtf8Metrics {
dimensions = append(dimensions, dim.label)
// Initialize metrics
dimensions := []string{
"a_legacy_label",
"label with space",
"label with 📈",
"label.with.spaß",
"instance",
"job",
"site",
"room",
}

utf8Metric := promauto.NewCounterVec(prometheus.CounterOpts{
Name: "a.utf8.metric 🤘",
Name: "a_utf8_metric",
Help: "a utf8 metric with utf8 labels",
}, dimensions)

Expand All @@ -329,10 +295,15 @@ func MeterProvider(c Config) (*sdkmetric.MeterProvider, error) {
// Start the metrics update goroutine
go func() {
for {
labels := []string{}
for _, dim := range fakeUtf8Metrics {
value := dim.getNextValue()
labels = append(labels, value)
labels := []string{
"legacy",
"space",
"metrics",
"this_is_fun",
"instance",
"job",
"LA-EPI",
`"Friends Don't Lie"`,
}

utf8Metric.WithLabelValues(labels...).Inc()
Expand All @@ -342,6 +313,11 @@ func MeterProvider(c Config) (*sdkmetric.MeterProvider, error) {
time.Sleep(time.Second * 5)
}
}()
}

func MeterProvider(c Config) (*sdkmetric.MeterProvider, error) {
// Default is 1m. Set to 3s for demonstrative purposes.
interval := 3 * time.Second

if c.UseDebugMeterer {
// create stdout exporter, when no OTLP url is set
Expand All @@ -358,12 +334,22 @@ func MeterProvider(c Config) (*sdkmetric.MeterProvider, error) {
}

ctx := context.Background()
exp, err := otlpmetrichttp.New(ctx)
opts := []otlpmetrichttp.Option{otlpmetrichttp.WithEndpoint(c.OTLPUrl)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found another issue. I think this is the culprit causing this, since it overrides the OTEL_EXPORTER_OTLP_METRICS_ENDPOINT environment variable configuration:

2025/04/23 07:47:43 failed to upload metrics: Post "http:///api/v1/otlp/v1/metrics": http: no Host in request URL

After the changes, try to run the example to see if the otlp metrics added in this PR #3905 are still available.

Copy link
Contributor

@marcsanmi marcsanmi Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still seeing the error above when running the example with docker-compose up --build. Could you please take another look? @shelldandy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be working properly now, please take another look @marcsanmi

if c.OTLPInsecure {
opts = append(opts, otlpmetrichttp.WithInsecure())
}
if c.OTLPBasicAuthUser != "" {
opts = append(opts, otlpmetrichttp.WithHeaders(map[string]string{
"Authorization": "Basic " + basicAuth(c.OTLPBasicAuthUser, c.OTLPBasicAuthPassword),
}))
}

exp, err := otlpmetrichttp.New(ctx, opts...)
if err != nil {
return nil, err
}

// Create a new tracer provider with a batch span processor and the otlp exporter.
// Create a new meter provider with a periodic reader and the otlp exporter
mp := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(newResource(c)),
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exp,
Expand Down
Loading