-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support exporting metrics in Prometheus format (#255)
- Loading branch information
1 parent
ee1b726
commit 7c42e21
Showing
23 changed files
with
214 additions
and
4 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Dependent images | ||
PROMETHEUS_IMAGE=quay.io/prometheus/prometheus:v2.54.1 | ||
|
||
# Prometheus | ||
PROMETHEUS_SERVICE_PORT=9090 | ||
PROMETHEUS_SERVICE_HOST=prometheus | ||
PROMETHEUS_ADDR=${PROMETHEUS_SERVICE_HOST}:${PROMETHEUS_SERVICE_PORT} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## Use opentelemetry-go-auto-instrumentation to report metrics | ||
|
||
The example shows how to use opentelemetry-go-auto-instrumentation to report metrics in prometheus format. | ||
|
||
## Do hybrid compilation with otel | ||
|
||
Do hybrid compilation with otel according to [README.md](../../demo/README.md). | ||
|
||
## Start OpenTelemetry Collector and Prometheus server | ||
|
||
You can use `docker-compose` to run the Prometheus server locally. | ||
|
||
```shell | ||
docker compose up --force-recreate --remove-orphans --detach | ||
``` | ||
|
||
## Run Application | ||
|
||
Set the endpoint and other otel-related environment variables according to | ||
the [document](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/) and execute the | ||
binary. | ||
|
||
```shell | ||
OTEL_METRICS_EXPORTER=prometheus OTEL_EXPORTER_PROMETHEUS_PORT=9464 ./metrics | ||
``` | ||
|
||
And you can request to the server to generate some metrics: | ||
|
||
```shell | ||
# For Golang GC metrics | ||
curl localhost:9000/gc-metrics | ||
# For Golang Memory metrics | ||
curl localhost:9000/mem-metrics | ||
``` | ||
|
||
## Query Metrics | ||
|
||
Open the http://localhost:9464/metrics to query metrics in local server: | ||
![metrics.png](metrics.png) | ||
|
||
|
||
Open the Prometheus UI at http://localhost:9090 to query metrics in prometheus server: | ||
![gc_count.png](gc_count.png) | ||
![mem_alloc.png](mem_alloc.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
x-default-logging: &logging | ||
driver: "json-file" | ||
options: | ||
max-size: "5m" | ||
max-file: "2" | ||
tag: "{{.Name}}" | ||
|
||
networks: | ||
default: | ||
name: opentelemetry-demo | ||
driver: bridge | ||
|
||
services: | ||
# Prometheus | ||
prometheus: | ||
image: ${PROMETHEUS_IMAGE} | ||
container_name: prometheus | ||
command: | ||
- --web.console.templates=/etc/prometheus/consoles | ||
- --web.console.libraries=/etc/prometheus/console_libraries | ||
- --storage.tsdb.retention.time=1h | ||
- --config.file=/etc/prometheus/prometheus-config.yaml | ||
- --storage.tsdb.path=/prometheus | ||
- --web.enable-lifecycle | ||
- --web.route-prefix=/ | ||
- --enable-feature=exemplar-storage | ||
- --enable-feature=otlp-write-receiver | ||
volumes: | ||
- ./src/prometheus/prometheus-config.yaml:/etc/prometheus/prometheus-config.yaml:ro | ||
deploy: | ||
resources: | ||
limits: | ||
memory: 300M | ||
restart: unless-stopped | ||
ports: | ||
- "${PROMETHEUS_SERVICE_PORT}:${PROMETHEUS_SERVICE_PORT}" | ||
logging: *logging | ||
extra_hosts: | ||
- "host.docker.internal:host-gateway" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module example/metrics | ||
|
||
go 1.22 | ||
|
||
replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ./../../.. | ||
|
||
replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ./../../../test/verifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2024 Alibaba Group Holding Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"net/http" | ||
"runtime" | ||
) | ||
|
||
func main() { | ||
http.Handle("/gc-metrics", http.HandlerFunc(gcMetrics)) | ||
http.Handle("/mem-metrics", http.HandlerFunc(memMetrics)) | ||
err := http.ListenAndServe("0.0.0.0:9000", nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func gcMetrics(w http.ResponseWriter, r *http.Request) { | ||
runtime.GC() | ||
w.Write([]byte("Get GC Metrics")) | ||
} | ||
|
||
func memMetrics(w http.ResponseWriter, r *http.Request) { | ||
var bytes []byte | ||
for i := 0; i < 10; i++ { | ||
bytes = append(bytes, make([]byte, 1024*1024)...) | ||
} | ||
w.Write([]byte("Get Memory Metrics")) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
example/metrics/prometheus-exporter/src/prometheus/prometheus-config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
global: | ||
evaluation_interval: 30s | ||
scrape_interval: 5s | ||
storage: | ||
tsdb: | ||
out_of_order_time_window: 30m | ||
scrape_configs: | ||
- job_name: metrics-scrape | ||
static_configs: | ||
- targets: | ||
- 'host.docker.internal:9464' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters