Files
madmin-go/opentelemetry.go
M AlveeandGitHub 47606f6238 distributed job framework (#557)
* feat: add DistJobType enum and sync distjob status types

Adds an iota-based DistJobType enum (mirrors the HealingDriveReason/
OfflineReason pattern already used in heal-commands.go) so distributed
job identity is a typed wire value instead of a bare string, per review
feedback on the eos distributed-job framework PR.

Also syncs DistJobLeaderStatus/DistJobNodeStatus field shapes to match
the current server-side JSON response (JobID/JobType replace JobName;
drops the unused Generation/AssignedSets/top-level CurrentBucket
fields that were never populated by the server).

* fix: bump vulnerable prometheus dep

- Add msgp generation (//msgp:tag json + //go:generate msgp) for
  DistJobLeaderStatus/DistJobNodeStatus, matching the pattern used by
  every other *-commands.go file in this package.
- Add unit tests for ListDistJobStatuses (success path, job-filter
  query param, DistJobTypeUnknown no-filter case, non-200 error path).
- Bump github.com/prometheus/prometheus v0.304.1 -> v0.312.0 to clear
  3 of the 4 govulncheck findings on this branch (GO-2026-5710,
  GO-2026-5381, GO-2026-5264, all fixed by that version). The 4th,
  GO-2026-5662, has no fix published upstream yet and also currently
  fails govulncheck on main independent of this branch.

* fix: pin go:generate directives to go.mod tool versions

Consolidate distjob-commands.go's separate //msgp:tag json directive
into the //go:generate msgp -d "tag json" flag, matching net-stats.go's
style (review feedback, PR #557).

Change every //go:generate msgp / //go:generate stringer directive
(31 occurrences, 30 files) to //go:generate go tool msgp / go tool
stringer, so generation always resolves the exact version pinned in
go.mod's tool block instead of whatever binary happens to be on PATH.
This was the root cause of the earlier stringer-drift CI failure.
Drop the now-unnecessary `go install -v tool` step from lint.yml.
2026-07-01 05:10:23 -04:00

196 lines
5.3 KiB
Go

// Copyright (c) 2015-2025 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package madmin
import (
"context"
"crypto/rsa"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/minio/madmin-go/v4/estream"
)
//msgp:replace TraceType with:uint64
//go:generate go tool msgp -d clearomitted -d "timezone utc" $GOFILE
// HTTPFilter defines parameters for filtering traces based on incoming http request properties
type HTTPFilter struct {
Func string `json:"funcFilter"`
UserAgent string `json:"userAgent"`
Header map[string]string `json:"header"`
}
// ServiceTelemetryOpts is a request to add following types to tracing.
type ServiceTelemetryOpts struct {
// Types to add to tracing.
Types TraceType `json:"types"`
// Public cert to encrypt stream.
PubCert []byte
// Sample rate to set for this filter.
// If <=0 or >=1 no sampling will be performed
// and all hits will be traced.
SampleRate float64 `json:"sampleRate"`
// Disable sampling and only do tracing when a trace id is set on incoming request.
ParentOnly bool `json:"parentOnly"`
// Tag adds a `custom.tag` field to all traces triggered by this.
TagKV map[string]string `json:"tags"`
// On incoming HTTP types, only trigger if substring is in request.
HTTPFilter HTTPFilter `json:"httpFilter"`
}
//msgp:ignore ServiceTelemetry
// ServiceTelemetry holds http telemetry spans, serialized and compressed.
type ServiceTelemetry struct {
SpanMZ []byte // Serialized and Compressed spans.
Err error // Any error that occurred
}
// ServiceTelemetryStream - gets raw stream for service telemetry.
func (adm AdminClient) ServiceTelemetryStream(ctx context.Context, opts ServiceTelemetryOpts) (io.ReadCloser, error) {
bopts, err := json.Marshal(opts)
if err != nil {
return nil, err
}
reqData := requestData{
relPath: adminAPIPrefix + "/telemetry",
content: bopts,
}
// Execute GET to call trace handler
resp, err := adm.executeMethod(ctx, http.MethodPost, reqData)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, httpRespToErrorResponse(resp)
}
return resp.Body, nil
}
// ServiceTelemetry - perform trace request and return individual packages.
// If options contains a public key the private key must be provided.
// If context is canceled the function will return.
func (adm AdminClient) ServiceTelemetry(ctx context.Context, opts ServiceTelemetryOpts, dst chan<- ServiceTelemetry, pk *rsa.PrivateKey) {
defer close(dst)
resp, err := adm.ServiceTelemetryStream(ctx, opts)
if err != nil {
dst <- ServiceTelemetry{Err: err}
return
}
dec, err := estream.NewReader(resp)
if err != nil {
dst <- ServiceTelemetry{Err: err}
return
}
if pk != nil {
dec.SetPrivateKey(pk)
}
for {
st, err := dec.NextStream()
if err != nil {
dst <- ServiceTelemetry{Err: err}
return
}
if ctx.Err() != nil {
return
}
block, err := io.ReadAll(st)
if err == nil && len(block) == 0 {
// Ignore 0 sized blocks.
continue
}
if ctx.Err() != nil {
return
}
select {
case <-ctx.Done():
return
case dst <- ServiceTelemetry{SpanMZ: block, Err: err}:
if err != nil {
return
}
}
}
}
// ParseTraceType - given a slice of trace types, returns OR'd tracetypes counterpart
func ParseTraceType(typeSlice []string) TraceType {
var traceType TraceType
for _, t := range typeSlice {
x := TraceAll
v := TraceType(1)
for x != 0 {
if strings.EqualFold(v.String(), t) {
traceType |= v
}
v <<= 1
x >>= 1
}
}
if traceType == 0 {
traceType = TraceS3
}
return traceType
}
// ParseSampleRate converts a sample rate from string format to float64.
func ParseSampleRate(s string) (float64, error) {
// Parse "x/y" entries.
if strings.ContainsRune(s, '/') {
split := strings.Split(s, "/")
if len(split) != 2 {
return 0, fmt.Errorf("invalid sample rate (%s)", s)
}
nom, err := strconv.ParseFloat(strings.TrimSpace(split[0]), 64)
if err != nil {
return 0, fmt.Errorf("invalid sample rate (%s)", s)
}
det, err := strconv.ParseFloat(strings.TrimSpace(split[1]), 64)
if err != nil || det <= 0 {
return 0, fmt.Errorf("invalid sample rate (%s)", s)
}
return nom / det, nil
}
// Parse percentage
if strings.ContainsRune(s, '%') {
split := strings.Split(strings.TrimSpace(s), "%")
if len(split) != 2 {
return 0, fmt.Errorf("invalid sample rate (%s)", s)
}
pct, err := strconv.ParseFloat(strings.TrimSpace(split[0]), 64)
if err != nil || pct <= 0 {
return 0, fmt.Errorf("invalid sample rate (%s)", s)
}
return pct / 100, nil
}
// Neither a fraction nor a percentage. So we treat s as a floating-point number.
return strconv.ParseFloat(strings.TrimSpace(s), 64)
}