Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Feb 6, 2025
0 parents commit ca60d38
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ARG NODE_VERSION=23.6
ARG ALPINE_VERSION=3.21
ARG GOLANG_VERSION=1.23
ARG JAEGERUI_VERSION=v1.65.0

FROM scratch AS jaegerui-src
ARG JAEGERUI_REPO=https://github.com/jaegertracing/jaeger-ui.git
ARG JAEGERUI_VERSION
ADD ${JAEGERUI_REPO}#${JAEGERUI_VERSION} /

FROM --platform=$BUILDPLATFORM node:${NODE_VERSION}-alpine${ALPINE_VERSION} AS builder
WORKDIR /work/jaeger-ui
COPY --from=jaegerui-src / .
RUN npm install
WORKDIR /work/jaeger-ui/packages/jaeger-ui
RUN NODE_ENVIRONMENT=production npm run build
# failed to find a way to avoid legacy build
RUN rm build/static/*-legacy* && rm build/static/*.png

FROM scratch AS jaegerui
COPY --from=builder /work/jaeger-ui/packages/jaeger-ui/build /

FROM alpine AS compressor
RUN --mount=target=/in,from=jaegerui <<EOT
set -ex
mkdir -p /out
cp -a /in/. /out
cd /out
find . -type f -exec sh -c 'gzip -9 -c "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;
# stop
EOT

FROM scratch AS jaegerui-compressed
COPY --from=compressor /out /
43 changes: 43 additions & 0 deletions cmd/jaeger-ui-rest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"flag"
"log"
"net"
"os"

"github.com/pkg/errors"
jaegerui "github.com/tonistiigi/jaeger-ui-rest"
)

func main() {
if err := run(); err != nil {
log.Printf("Error: %+v", err)
os.Exit(1)
}
}

func run() error {
var opt struct {
addr string
}

flag.StringVar(&opt.addr, "addr", "127.0.0.1:0", "address to listen on")
flag.Parse()

args := flag.Args()
if len(args) > 0 {
return errors.Errorf("unknown arguments: %v", args)
}

s := jaegerui.NewServer(jaegerui.Config{})

ln, err := net.Listen("tcp", opt.addr)
if err != nil {
return errors.Wrapf(err, "failed to listen on %s", opt.addr)
}

log.Printf("Listening on %s", ln.Addr().String())

return s.Serve(ln)
}
42 changes: 42 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jaegerui

import (
"bytes"
"encoding/json"
"slices"
)

type Menu struct {
Label string `json:"label"`
Items []MenuItem `json:"items"`
}

type MenuItem struct {
Label string `json:"label"`
URL string `json:"url"`
}

type Config struct {
Dependencies struct {
MenuEnabled bool `json:"menuEnabled"`
} `json:"dependencies"`
Monitor struct {
MenuEnabled bool `json:"menuEnabled"`
} `json:"monitor"`
ArchiveEnabled bool `json:"archiveEnabled"`
Menu []Menu `json:"menu"`
}

func (cfg Config) Inject(name string, dt []byte) ([]byte, bool) {
if name != "index.html" {
return dt, false
}

cfgData, err := json.Marshal(cfg)
if err != nil {
return dt, false
}

dt = bytes.Replace(dt, []byte("const JAEGER_CONFIG = DEFAULT_CONFIG;"), slices.Concat([]byte(`const JAEGER_CONFIG = `), cfgData, []byte(`;`)), 1)
return dt, true
}
117 changes: 117 additions & 0 deletions decompress/decompress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package decompress

import (
"bytes"
"compress/gzip"
"io"
"io/fs"
"path/filepath"
"sync"
)

type decompressFS struct {
fs.FS
mu sync.Mutex
data map[string][]byte
inject Injector
}

type Injector interface {
Inject(name string, dt []byte) ([]byte, bool)
}

func NewFS(fsys fs.FS, injector Injector) fs.FS {
return &decompressFS{
FS: fsys,
data: make(map[string][]byte),
inject: injector,
}
}

func (d *decompressFS) Open(name string) (fs.File, error) {
name = filepath.Clean(name)

f, err := d.FS.Open(name)
if err != nil {
return nil, err
}

d.mu.Lock()
defer d.mu.Unlock()

dt, ok := d.data[name]
if ok {
return &staticFile{
Reader: bytes.NewReader(dt),
f: f,
}, nil
}

fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}

if fi.IsDir() {
return f, nil
}

gzReader, err := gzip.NewReader(f)
if err != nil {
f.Close()
return nil, err
}

buf := &bytes.Buffer{}
if _, err := io.Copy(buf, gzReader); err != nil {
f.Close()
return nil, err
}

dt = buf.Bytes()
if d.inject != nil {
newdt, ok := d.inject.Inject(name, dt)
if ok {
dt = newdt
}
}

d.data[name] = dt

return &staticFile{
Reader: bytes.NewReader(dt),
f: f,
}, nil
}

type staticFile struct {
*bytes.Reader
f fs.File
}

func (s *staticFile) Stat() (fs.FileInfo, error) {
fi, err := s.f.Stat()
if err != nil {
return nil, err
}
return &fileInfo{
FileInfo: fi,
size: int64(s.Len()),
}, nil
}

func (s *staticFile) Close() error {
return s.f.Close()
}

type fileInfo struct {
fs.FileInfo
size int64
}

func (f *fileInfo) Size() int64 {
return f.size
}

var _ fs.File = &staticFile{}
3 changes: 3 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target "public" {
output = ["./public"]
}
17 changes: 17 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jaegerui

import (
"embed"
"io/fs"
"net/http"

"github.com/tonistiigi/jaeger-ui-rest/decompress"
)

//go:embed public
var staticFiles embed.FS

func FS(cfg Config) http.FileSystem {
files, _ := fs.Sub(staticFiles, "public")
return http.FS(decompress.NewFS(files, cfg))
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/tonistiigi/jaeger-ui-rest

go 1.23.1

require github.com/pkg/errors v0.9.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Binary file added public/index.html
Binary file not shown.
Binary file added public/static/favicon-BxcVf0am.ico
Binary file not shown.
Binary file added public/static/index-BVZ6r69N.js
Binary file not shown.
Binary file added public/static/index-Cox7RLGO.css
Binary file not shown.
Binary file added public/static/jaeger-logo-CNZsoUdk.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### jaeger-ui-rest

[Jaeger UI](https://github.com/jaegertracing/jaeger-ui) server with only the UI component and simple REST API for loading pregenerated JSON traces.
Loading

0 comments on commit ca60d38

Please sign in to comment.