Skip to content

Commit

Permalink
add malloc_info metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Kosuke Morimoto <[email protected]>
  • Loading branch information
kmrmt committed Aug 29, 2023
1 parent e91f061 commit c07dcd6
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 9 deletions.
24 changes: 17 additions & 7 deletions internal/core/malloc/malloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,36 @@ type Sizes struct {
Unsorted Size `xml:"unsorted"`
}

type Memory struct {
type Total struct {
Type string `xml:"fast,attr"`
Count int `xml:"count,attr"`
Size int `xml:"size,attr"`
}

type System struct {
Type string `xml:"fast,attr"`
Size int `xml:"size,attr"`
}

type Aspace struct {
Type string `xml:"fast,attr"`
Size int `xml:"size,attr"`
}

type Heap struct {
Nr string `xml:"nr,attr"`
Sizes Sizes `xml:"sizes"`
Total []*Memory `xml:"total"`
System []*Memory `xml:"system"`
Aspace []*Memory `xml:"aspace"`
Total []*Total `xml:"total"`
System []*System `xml:"system"`
Aspace []*Aspace `xml:"aspace"`
}

type MallocInfo struct {
Version string `xml:"version,attr"`
Heap []*Heap `xml:"heap"`
Total []*Memory `xml:"total"`
System []*Memory `xml:"system"`
Aspace []*Memory `xml:"aspace"`
Total []*Total `xml:"total"`
System []*System `xml:"system"`
Aspace []*Aspace `xml:"aspace"`
}

func convert(body string) (*MallocInfo, error) {
Expand Down
2 changes: 0 additions & 2 deletions internal/core/malloc/malloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func Test_convert(t *testing.T) {
if err := checkFunc(test.want, gotM, err); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}
Expand Down Expand Up @@ -136,7 +135,6 @@ func TestGetMallocInfo(t *testing.T) {
if err := checkFunc(test.want, got, err); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}
22 changes: 22 additions & 0 deletions internal/errors/malloc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright (C) 2019-2023 vdaas.org vald team <[email protected]>
//
// 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
//
// https://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 errors provides error types and function
package errors

var ErrUnexpectedReturnCode = func(ret int) error {
return Errorf("unexpected return code: %d", ret)

Check warning on line 21 in internal/errors/malloc.go

View check run for this annotation

Codecov / codecov/patch

internal/errors/malloc.go#L20-L21

Added lines #L20 - L21 were not covered by tests
}
242 changes: 242 additions & 0 deletions internal/observability/metrics/mem/malloc/malloc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// Copyright (C) 2019-2023 vdaas.org vald team <[email protected]>
//
// 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
//
// https://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 malloc

import (
"context"

"github.com/vdaas/vald/internal/core/malloc"
"github.com/vdaas/vald/internal/observability/metrics"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/view"
)

const (
totalFastCountMetricsName = "total_fast_count"
totalFastCountMetricsDescription = "Total fast count"

totalFastSizeMetricsName = "total_fast_size"
totalFastSizeMetricsDescription = "Total fast size"

totalRestCountMetricsName = "total_rest_count"
totalRestCountMetricsDescription = "Total rest count"

totalRestSizeMetricsName = "total_rest_size"
totalRestSizeMetricsDescription = "Total rest size"

systemCurrentSizeMetricsName = "system_current_size"
systemCurrentSizeMetricsDescription = "System current size"

systemMaxSizeMetricsName = "system_max_size"
systemMaxSizeMetricsDescription = "System max size"

aspaceTotalSizeMetricsName = "aspace_total_size"
aspaceTotalSizeMetricsDescription = "Aspace total size"

aspaceMprotectSizeMetricsName = "aspace_mprotect_size"
aspaceMprotectSizeMetricsDescription = "Aspace mprotect size"
)

type mallocMetrics struct{}

func New() metrics.Metric {
return &mallocMetrics{}
}

func (m *mallocMetrics) View() ([]*metrics.View, error) {
totalFastCount, err := view.New(
view.MatchInstrumentName(totalFastCountMetricsName),
view.WithSetDescription(totalFastCountMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

totalFastSize, err := view.New(
view.MatchInstrumentName(totalFastSizeMetricsName),
view.WithSetDescription(totalFastSizeMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

totalRestCount, err := view.New(
view.MatchInstrumentName(totalRestCountMetricsName),
view.WithSetDescription(totalRestCountMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

totalRestSize, err := view.New(
view.MatchInstrumentName(totalRestSizeMetricsName),
view.WithSetDescription(totalRestSizeMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

systemCurrentSize, err := view.New(
view.MatchInstrumentName(systemCurrentSizeMetricsName),
view.WithSetDescription(systemCurrentSizeMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

systemMaxSize, err := view.New(
view.MatchInstrumentName(systemMaxSizeMetricsName),
view.WithSetDescription(systemMaxSizeMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

aspaceTotalSize, err := view.New(
view.MatchInstrumentName(aspaceTotalSizeMetricsName),
view.WithSetDescription(aspaceTotalSizeMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

aspaceMprotectSize, err := view.New(
view.MatchInstrumentName(aspaceMprotectSizeMetricsName),
view.WithSetDescription(aspaceMprotectSizeMetricsDescription),
view.WithSetAggregation(aggregation.LastValue{}),
)
if err != nil {
return nil, err
}

return []*metrics.View{
&totalFastCount,
&totalFastSize,
&totalRestCount,
&totalRestSize,
&systemCurrentSize,
&systemMaxSize,
&aspaceTotalSize,
&aspaceMprotectSize,
}, nil
}

func (mm *mallocMetrics) Register(m metrics.Meter) error {
totalFastCount, err := m.AsyncInt64().Gauge(
totalFastCountMetricsName,
metrics.WithDescription(totalFastCountMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

totalFastSize, err := m.AsyncInt64().Gauge(
totalFastSizeMetricsName,
metrics.WithDescription(totalFastSizeMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

totalRestCount, err := m.AsyncInt64().Gauge(
totalRestCountMetricsName,
metrics.WithDescription(totalRestCountMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

totalRestSize, err := m.AsyncInt64().Gauge(
totalRestSizeMetricsName,
metrics.WithDescription(totalRestSizeMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

systemCurrentSize, err := m.AsyncInt64().Gauge(
systemCurrentSizeMetricsName,
metrics.WithDescription(systemCurrentSizeMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

systemMaxSize, err := m.AsyncInt64().Gauge(
systemMaxSizeMetricsName,
metrics.WithDescription(systemMaxSizeMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

aspaceTotalSize, err := m.AsyncInt64().Gauge(
aspaceTotalSizeMetricsName,
metrics.WithDescription(aspaceTotalSizeMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

aspaceMprotectSize, err := m.AsyncInt64().Gauge(
aspaceMprotectSizeMetricsName,
metrics.WithDescription(aspaceMprotectSizeMetricsDescription),
metrics.WithUnit(metrics.Dimensionless),
)
if err != nil {
return err
}

return m.RegisterCallback(
[]metrics.AsynchronousInstrument{
totalFastCount,
totalFastSize,
totalRestCount,
totalRestSize,
systemCurrentSize,
systemMaxSize,
aspaceTotalSize,
aspaceMprotectSize,
},
func(ctx context.Context) {
mallocInfo, err := malloc.GetMallocInfo()
if err != nil {
return
}

totalFastCount.Observe(ctx, int64(mallocInfo.Total[0].Count))
totalFastSize.Observe(ctx, int64(mallocInfo.Total[0].Size))
totalRestCount.Observe(ctx, int64(mallocInfo.Total[1].Count))
totalRestSize.Observe(ctx, int64(mallocInfo.Total[1].Size))
systemCurrentSize.Observe(ctx, int64(mallocInfo.System[0].Size))
systemMaxSize.Observe(ctx, int64(mallocInfo.System[1].Size))
aspaceTotalSize.Observe(ctx, int64(mallocInfo.Aspace[0].Size))
aspaceMprotectSize.Observe(ctx, int64(mallocInfo.Aspace[1].Size))
},
)
}
2 changes: 2 additions & 0 deletions pkg/agent/core/ngt/usecase/agentd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/vdaas/vald/internal/observability"
ngtmetrics "github.com/vdaas/vald/internal/observability/metrics/agent/core/ngt"
infometrics "github.com/vdaas/vald/internal/observability/metrics/info"
mallocmetrics "github.com/vdaas/vald/internal/observability/metrics/mem/malloc"
"github.com/vdaas/vald/internal/runner"
"github.com/vdaas/vald/internal/safety"
"github.com/vdaas/vald/internal/servers/server"
Expand Down Expand Up @@ -97,6 +98,7 @@ func New(cfg *config.Data) (r runner.Runner, err error) {
cfg.Observability,
ngtmetrics.New(ngt),
infometrics.New("agent_core_ngt_info", "Agent NGT info", *cfg.NGT),
mallocmetrics.New(),

Check warning on line 101 in pkg/agent/core/ngt/usecase/agentd.go

View check run for this annotation

Codecov / codecov/patch

pkg/agent/core/ngt/usecase/agentd.go#L101

Added line #L101 was not covered by tests
)
if err != nil {
return nil, err
Expand Down

0 comments on commit c07dcd6

Please sign in to comment.