Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Few more minor changes for the status page:
Browse files Browse the repository at this point in the history
- Factor out the conf to string conversion into a utility function and use that everywhere.
- surfacers: Use the inferred surfacer type for SurfacerInfo objects.

PiperOrigin-RevId: 225272663
  • Loading branch information
manugarg committed Dec 13, 2018
1 parent 09f22be commit 1447a67
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
11 changes: 2 additions & 9 deletions probes/probes.go
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/google/cloudprober/targets/lameduck"
targetspb "github.com/google/cloudprober/targets/proto"
"github.com/google/cloudprober/validators"
"github.com/google/cloudprober/web/formatutils"
)

const (
Expand Down Expand Up @@ -200,14 +201,6 @@ func Init(probeProtobufs []*configpb.ProbeDef, globalTargetsOpts *targetspb.Glob
latencyDistLB = fmt.Sprintf("%v", opts.LatencyDist.Data().LowerBounds)
}

// If probeConf supports String() function, use it for status page.
probeConfStr := ""
if msg, ok := probeConf.(proto.Message); ok {
probeConfStr = proto.MarshalTextString(msg)
} else if stringer, ok := probeConf.(fmt.Stringer); ok {
probeConfStr = stringer.String()
}

probes[p.GetName()] = &ProbeInfo{
Probe: probe,
Name: p.GetName(),
Expand All @@ -217,7 +210,7 @@ func Init(probeProtobufs []*configpb.ProbeDef, globalTargetsOpts *targetspb.Glob
TargetsDesc: p.Targets.String(),
LatencyDistLB: latencyDistLB,
LatencyUnit: opts.LatencyUnit.String(),
ProbeConf: probeConfStr,
ProbeConf: formatutils.ConfToString(probeConf),
}
}

Expand Down
17 changes: 8 additions & 9 deletions servers/servers.go
Expand Up @@ -19,7 +19,6 @@ package servers

import (
"context"
"fmt"
"html/template"

"github.com/google/cloudprober/logger"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/google/cloudprober/servers/http"
configpb "github.com/google/cloudprober/servers/proto"
"github.com/google/cloudprober/servers/udp"
"github.com/google/cloudprober/web/formatutils"
)

const (
Expand Down Expand Up @@ -82,30 +82,29 @@ func Init(initCtx context.Context, serverDefs []*configpb.ServerDef) (servers []
if err != nil {
return
}

var conf interface{}
var server Server

switch serverDef.GetType() {
case configpb.ServerDef_HTTP:
server, err = http.New(initCtx, serverDef.GetHttpServer(), l)
conf = serverDef.GetHttpServer()
case configpb.ServerDef_UDP:
server, err = udp.New(initCtx, serverDef.GetUdpServer(), l)
conf = serverDef.GetUdpServer()
case configpb.ServerDef_GRPC:
server, err = grpc.New(initCtx, serverDef.GetGrpcServer(), l)
conf = serverDef.GetGrpcServer()
}
if err != nil {
return
}

confStr := ""
if serverDef.GetServer() != nil {
if stringer, ok := serverDef.GetServer().(fmt.Stringer); ok {
confStr = stringer.String()
}
}

servers = append(servers, &ServerInfo{
Server: server,
Type: serverDef.GetType().String(),
Conf: confStr,
Conf: formatutils.ConfToString(conf),
})
}
return
Expand Down
12 changes: 3 additions & 9 deletions surfacers/surfacers.go
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/google/cloudprober/surfacers/postgres"
"github.com/google/cloudprober/surfacers/prometheus"
"github.com/google/cloudprober/surfacers/stackdriver"
"github.com/google/cloudprober/web/formatutils"

surfacerpb "github.com/google/cloudprober/surfacers/proto"
surfacerspb "github.com/google/cloudprober/surfacers/proto"
Expand Down Expand Up @@ -185,18 +186,11 @@ func Init(sDefs []*surfacerpb.SurfacerDef) ([]*SurfacerInfo, error) {
return nil, err
}

confStr := ""
if conf != nil {
if stringer, ok := conf.(fmt.Stringer); ok {
confStr = stringer.String()
}
}

result = append(result, &SurfacerInfo{
Surfacer: s,
Type: sDef.GetType().String(),
Type: sType.String(),
Name: sDef.GetName(),
Conf: confStr,
Conf: formatutils.ConfToString(conf),
})
}
return result, nil
Expand Down
34 changes: 34 additions & 0 deletions web/formatutils/formatutils.go
@@ -0,0 +1,34 @@
// Copyright 2018 Google Inc.
//
// 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
//
// http://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 formatutils provides web related utils for the cloudprober
// sub-packages.
package formatutils

import (
"fmt"

"github.com/golang/protobuf/proto"
)

// ConfToString tries to convert the given conf object into a string.
func ConfToString(conf interface{}) string {
if msg, ok := conf.(proto.Message); ok {
return proto.MarshalTextString(msg)
}
if stringer, ok := conf.(fmt.Stringer); ok {
return stringer.String()
}
return ""
}

1 comment on commit 1447a67

@manugarg
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixes #182.

Please sign in to comment.