Skip to content

Commit

Permalink
fix(pyroscope.java): infer service_name label if possible (#6184)
Browse files Browse the repository at this point in the history
* fix(pyroscope.java): infer service_name label if possible

* Do not mutate target

* update changelog, fix scrape infer

* fix

* fix
  • Loading branch information
korniltsev authored Jan 29, 2024
1 parent 49bfc29 commit fe89779
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Main (unreleased)
- `mimir.rules.kubernetes` has a new `prometheus_http_prefix` argument to configure
the HTTP endpoint on which to connect to Mimir's API. (@hainenber)

- `service_name` label is inferred from discovery meta labels in `pyroscope.java` (@korniltsev)

### Bugfixes

- Fix an issue in `remote.s3` where the exported content of an object would be an empty string if `remote.s3` failed to fully retrieve
Expand Down
4 changes: 4 additions & 0 deletions component/pyroscope/java/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (p *profilingLoop) push(jfrBytes []byte, startTime time.Time, endTime time.
for _, l := range jfrpprofPyroscope.Labels(target, profiles.JFREvent, req.Metric, "", spyName) {
ls.Set(l.Name, l.Value)
}
if ls.Get(labelServiceName) == "" {
ls.Set(labelServiceName, inferServiceName(target))
}

profile, err := req.Profile.MarshalVT()
if err != nil {
_ = l.Log("msg", "failed to marshal profile", "err", err)
Expand Down
35 changes: 35 additions & 0 deletions component/pyroscope/java/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package java

import (
"fmt"

"github.com/grafana/agent/component/discovery"
)

const (
labelServiceName = "service_name"
labelServiceNameK8s = "__meta_kubernetes_pod_annotation_pyroscope_io_service_name"
)

func inferServiceName(target discovery.Target) string {
k8sServiceName := target[labelServiceNameK8s]
if k8sServiceName != "" {
return k8sServiceName
}
k8sNamespace := target["__meta_kubernetes_namespace"]
k8sContainer := target["__meta_kubernetes_pod_container_name"]
if k8sNamespace != "" && k8sContainer != "" {
return fmt.Sprintf("java/%s/%s", k8sNamespace, k8sContainer)
}
dockerContainer := target["__meta_docker_container_name"]
if dockerContainer != "" {
return dockerContainer
}
if swarmService := target["__meta_dockerswarm_container_label_service_name"]; swarmService != "" {
return swarmService
}
if swarmService := target["__meta_dockerswarm_service_name"]; swarmService != "" {
return swarmService
}
return "unspecified"
}
6 changes: 6 additions & 0 deletions component/pyroscope/scrape/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,11 @@ func inferServiceName(lset labels.Labels) string {
if dockerContainer != "" {
return dockerContainer
}
if swarmService := lset.Get("__meta_dockerswarm_container_label_service_name"); swarmService != "" {
return swarmService
}
if swarmService := lset.Get("__meta_dockerswarm_service_name"); swarmService != "" {
return swarmService
}
return "unspecified"
}
18 changes: 18 additions & 0 deletions docs/sources/flow/reference/components/pyroscope.java.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ The `asprof` binary runs with root permissions.
If you change the `tmp_dir` configuration to something other than `/tmp`, then you must ensure that the
directory is only writable by root.
{{% /admonition %}}

#### `targets` argument

The special `__process_pid__` label _must always_ be present and corresponds to the
process PID that is used for profiling.

Labels starting with a double underscore (`__`) are treated as _internal_, and are removed prior to scraping.

The special label `service_name` is required and must always be present.
If it is not specified, `pyroscope.scrape` will attempt to infer it from
either of the following sources, in this order:
1. `__meta_kubernetes_pod_annotation_pyroscope_io_service_name` which is a `pyroscope.io/service_name` pod annotation.
2. `__meta_kubernetes_namespace` and `__meta_kubernetes_pod_container_name`
3. `__meta_docker_container_name`
4. `__meta_dockerswarm_container_label_service_name` or `__meta_dockerswarm_service_name`

If `service_name` is not specified and could not be inferred, then it is set to `unspecified`.

## Blocks

The following blocks are supported inside the definition of
Expand Down
1 change: 1 addition & 0 deletions docs/sources/flow/reference/components/pyroscope.scrape.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ either of the following sources, in this order:
1. `__meta_kubernetes_pod_annotation_pyroscope_io_service_name` which is a `pyroscope.io/service_name` pod annotation.
2. `__meta_kubernetes_namespace` and `__meta_kubernetes_pod_container_name`
3. `__meta_docker_container_name`
4. `__meta_dockerswarm_container_label_service_name` or `__meta_dockerswarm_service_name`

If `service_name` is not specified and could not be inferred, then it is set to `unspecified`.

Expand Down

0 comments on commit fe89779

Please sign in to comment.