gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/metricsviz/metricsviz_publish.go (about)

     1  // Copyright 2024 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metricsviz
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"time"
    24  )
    25  
    26  // publishHTMLFn is the function to use to publish HTML.
    27  // Can be stubbed out.
    28  var publishHTMLFn = publishHTML
    29  
    30  // publishHTML publishes the HTML contents to a sane file location and
    31  // writes the path to the logger.
    32  func publishHTML(ctx context.Context, logFn func(format string, args ...any), htmlOptions HTMLOptions, html string) error {
    33  	// We don't use the test's temporary directory here because it is deleted at
    34  	// the end of the test, but we want to keep the HTML around later for
    35  	// viewing. So we just use a new temporary directory in `/tmp` here.
    36  	const metricsDirRoot = "/tmp/gvisor_metrics"
    37  
    38  	if err := os.MkdirAll(metricsDirRoot, 0755); err != nil {
    39  		return fmt.Errorf("failed to create metrics directory %q: %w", metricsDirRoot, err)
    40  	}
    41  	dirName := htmlOptions.Title
    42  	if firstSlash := strings.Index(htmlOptions.Title, "/"); firstSlash != -1 {
    43  		dirName = htmlOptions.Title[:firstSlash]
    44  	}
    45  	benchmarkDir := filepath.Join(metricsDirRoot, slugify(dirName))
    46  	if err := os.MkdirAll(benchmarkDir, 0755); err != nil {
    47  		return fmt.Errorf("failed to create benchmark directory %q: %w", benchmarkDir, err)
    48  	}
    49  	htmlPath := filepath.Join(benchmarkDir, fmt.Sprintf("charts.%s-%s.%s.html", htmlOptions.When.Format(time.DateOnly), htmlOptions.When.Format(time.TimeOnly), slugify(htmlOptions.Title)))
    50  	if err := os.WriteFile(htmlPath, []byte(html), 0644); err != nil {
    51  		return fmt.Errorf("failed to write HTML to %q: %w", htmlPath, err)
    52  	}
    53  	if err := os.Chmod(htmlPath, 0644); err != nil {
    54  		return fmt.Errorf("failed to chmod %q: %w", htmlPath, err)
    55  	}
    56  	if htmlOptions.ContainerName == "" {
    57  		logFn("******** METRICS CHARTS: file://%s ********", htmlPath)
    58  	} else {
    59  		logFn("******** METRICS CHARTS (%s): file://%s ********", htmlOptions.ContainerName, htmlPath)
    60  	}
    61  	return nil
    62  }