summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/scala/com/yahoo/container/handler/observability/Graphviz.scala
blob: 6403b08f61c4902371a3f599eab04615c91c2c2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler.observability

import com.yahoo.text.Utf8
import com.yahoo.io.IOUtils
import java.io.{IOException, InputStreamReader, InputStream}
import com.google.common.io.ByteStreams

/**
 * @author tonytv
 */

object Graphviz {

  @throws(classOf[IOException])
  def runDot(outputType: String, graph: String) = {
    val process = Runtime.getRuntime.exec(Array("/bin/sh", "-c",  "unflatten -l7 | dot -T" + outputType))
    process.getOutputStream.write(Utf8.toBytes(graph))
    process.getOutputStream.close()

    val result = ByteStreams.toByteArray(process.getInputStream)
    process.waitFor() match {
      case 0 => result
      case 127 => throw new RuntimeException("Couldn't find dot, please ensure that Graphviz is installed.")
      case _ => throw new RuntimeException("Failed running dot: " + readString(process.getErrorStream))
    }
  }

  private def readString(inputStream: InputStream): String = {
    IOUtils.readAll(new InputStreamReader(inputStream, "UTF-8"))
  }

}