Skip to content

Add runtime field flag in documentation files #2415

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

Merged
merged 11 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 7 additions & 1 deletion internal/docs/exported_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type fieldsTableRecord struct {
aType string
unit string
metricType string
runtime bool
}

var escaper = strings.NewReplacer("*", "\\*", "{", "\\{", "}", "\\}", "<", "\\<", ">", "\\>")
Expand Down Expand Up @@ -74,10 +75,14 @@ func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord)
builder.WriteString("\n")
for _, c := range collected {
description := strings.TrimSpace(strings.ReplaceAll(c.description, "\n", " "))
builder.WriteString(fmt.Sprintf("| %s | %s | %s |",
builder.WriteString(fmt.Sprintf("| %s | %s | %s",
escaper.Replace(c.name),
escaper.Replace(description),
c.aType))
if c.runtime {
builder.WriteString(" (runtime)")
}
builder.WriteString(" |")
if unitsPresent {
builder.WriteString(fmt.Sprintf(" %s |", c.unit))
}
Expand Down Expand Up @@ -130,6 +135,7 @@ func visitFields(namePrefix string, f fields.FieldDefinition, records []fieldsTa
aType: f.Type,
unit: f.Unit,
metricType: f.MetricType,
runtime: f.Runtime.IsEnabled(),
})

for _, multiField := range f.MultiFields {
Expand Down
79 changes: 1 addition & 78 deletions internal/fields/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strings"

"gopkg.in/yaml.v3"

"github.com/elastic/elastic-package/internal/common"
)

// FieldDefinition describes a single field with its properties.
Expand All @@ -34,6 +32,7 @@ type FieldDefinition struct {
Fields FieldDefinitions `yaml:"fields,omitempty"`
MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"`
Reusable *ReusableConfig `yaml:"reusable,omitempty"`
Runtime runtimeField `yaml:"runtime"`

// disallowAtTopLevel transfers the reusability config from parent groups to nested fields.
// It is negated respect to Reusable.TopLevel, so it is disabled by default.
Expand All @@ -44,82 +43,6 @@ type ReusableConfig struct {
TopLevel bool `yaml:"top_level"`
}

func (orig *FieldDefinition) Update(fd FieldDefinition) {
Copy link
Contributor Author

@mrodm mrodm Feb 17, 2025

Choose a reason for hiding this comment

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

I didn't find any usages of this Update function in the elastic-package code base.

It looks like that it was removed in this PR #1335:
https://github.com/elastic/elastic-package/pull/1335/files#diff-77cc155ee037ac620b1e188302f60fdf736637a25e5021e76dbad83594edd09cL133

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, this used to be neccesary 🤔 But good if we don't need it anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least, that function is not called anymore...

if fd.Name != "" {
orig.Name = fd.Name
}
if fd.Description != "" {
orig.Description = fd.Description
}
if fd.Type != "" {
orig.Type = fd.Type
}
if fd.ObjectType != "" {
orig.ObjectType = fd.ObjectType
}
if fd.Value != "" {
orig.Value = fd.Value
}
if len(fd.AllowedValues) > 0 {
orig.AllowedValues = fd.AllowedValues
}
if len(fd.ExpectedValues) > 0 {
orig.ExpectedValues = fd.ExpectedValues
}
if fd.Pattern != "" {
orig.Pattern = fd.Pattern
}
if fd.Unit != "" {
orig.Unit = fd.Unit
}
if fd.MetricType != "" {
orig.MetricType = fd.MetricType
}
if fd.External != "" {
orig.External = fd.External
}
if fd.Index != nil {
orig.Index = fd.Index
}
if fd.DocValues != nil {
orig.DocValues = fd.DocValues
}

if len(fd.Normalize) > 0 {
orig.Normalize = common.StringSlicesUnion(orig.Normalize, fd.Normalize)
}

if len(fd.Fields) > 0 {
orig.Fields = updateFields(orig.Fields, fd.Fields)
}

if len(fd.MultiFields) > 0 {
orig.MultiFields = updateFields(orig.MultiFields, fd.MultiFields)
}
}

func updateFields(origFields, fields []FieldDefinition) []FieldDefinition {
// When a subfield the same name exists, update it. When not, append it.
updatedFields := make([]FieldDefinition, len(origFields))
copy(updatedFields, origFields)
for _, newField := range fields {
found := false
for i, origField := range origFields {
if origField.Name != newField.Name {
continue
}

found = true
updatedFields[i].Update(newField)
break
}
if !found {
updatedFields = append(updatedFields, newField)
}
}
return updatedFields
}

// FieldDefinitions is an array of FieldDefinition, this can be unmarshalled from
// a yaml list or a yaml map.
type FieldDefinitions []FieldDefinition
Expand Down
174 changes: 0 additions & 174 deletions internal/fields/model_test.go
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed test (file) related to the Update function.

This file was deleted.

56 changes: 56 additions & 0 deletions internal/fields/runtime_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package fields

import (
"strconv"

"gopkg.in/yaml.v3"
)

// Code based on the definition of Runtime Field in package-spec
// https://github.com/elastic/package-spec/blob/964c4a69e024cc464c4808720ba0db9f001a82a7/code/go/internal/validator/semantic/types.go#L26
type runtimeField struct {
enabled bool
script string
}

// Ensure runtimeField implements yaml.Unmarshaler.
var _ yaml.Unmarshaler = new(runtimeField)

func (r *runtimeField) IsEnabled() bool {
if r.enabled {
return true
}
if r.script != "" {
return true
}
return false
}

func (r runtimeField) String() string {
if r.script != "" {
return r.script
}
return strconv.FormatBool(r.enabled)
}

func (r *runtimeField) unmarshalString(text string) error {
value, err := strconv.ParseBool(text)
if err == nil {
r.enabled = value
return nil
}

// JSONSchema already checks about the type of this field (e.g. int or float)
r.enabled = true
r.script = text
return nil
}

// UnmarshalYAML implements the yaml.Marshaler interface for runtime.
func (r *runtimeField) UnmarshalYAML(value *yaml.Node) error {
return r.unmarshalString(value.Value)
}
2 changes: 1 addition & 1 deletion internal/testrunner/runners/system/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ func (p *pipelineTrace) UnmarshalJSON(d []byte) error {
case string:
*p = append(*p, v)
case []any:
// asume it is going to be an array of strings
// assume it is going to be an array of strings
for _, value := range v {
*p = append(*p, fmt.Sprint(value))
}
Expand Down