github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/platform/diagnostic/exporter.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package diagnostic
    19  
    20  import (
    21  	"context"
    22  	"net/http"
    23  	"net/http/pprof"
    24  
    25  	"github.com/google/gops/agent"
    26  	"go.uber.org/zap"
    27  
    28  	"github.com/zntrio/harp/v2/pkg/sdk/log"
    29  )
    30  
    31  // Register adds diagnostic tools to main process.
    32  func Register(ctx context.Context, conf *Config, r *http.ServeMux) (func(), error) {
    33  	if conf.GOPS.Enabled {
    34  		// Start diagnostic handler
    35  		if conf.GOPS.RemoteURL != "" {
    36  			log.For(ctx).Info("Starting gops agent", zap.String("url", conf.GOPS.RemoteURL))
    37  
    38  			if err := agent.Listen(agent.Options{Addr: conf.GOPS.RemoteURL}); err != nil {
    39  				log.For(ctx).Error("Error on starting gops agent", zap.Error(err))
    40  			}
    41  		} else {
    42  			log.For(ctx).Info("Starting gops agent locally")
    43  
    44  			if err := agent.Listen(agent.Options{}); err != nil {
    45  				log.For(ctx).Error("Error on starting gops agent locally", zap.Error(err))
    46  			}
    47  		}
    48  	}
    49  
    50  	if conf.PProf.Enabled {
    51  		r.HandleFunc("/debug/pprof", pprof.Index)
    52  		r.HandleFunc("/debug/cmdline", pprof.Cmdline)
    53  		r.HandleFunc("/debug/profile", pprof.Profile)
    54  		r.HandleFunc("/debug/symbol", pprof.Symbol)
    55  		r.HandleFunc("/debug/trace", pprof.Trace)
    56  		r.Handle("/debug/goroutine", pprof.Handler("goroutine"))
    57  		r.Handle("/debug/heap", pprof.Handler("heap"))
    58  		r.Handle("/debug/threadcreate", pprof.Handler("threadcreate"))
    59  		r.Handle("/debug/block", pprof.Handler("block"))
    60  	}
    61  
    62  	// No error
    63  	return func() {
    64  		agent.Close()
    65  	}, nil
    66  }